Notification bar - when clicked, how to open file explorer with specified location

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I did a major update of my question because some misunderstandings.

So, I have a notification. And when I click that notification, I want the file explorer app (third party app of course) opens. No matter whuch app is, if there's more than on file explorers, it should prompt the "open with" - and then open /sdcard/folder (if possible)

My notification it's here on Listar.class

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);         Notification notification = new Notification(R.drawable.ic_menu_save, msg, System.currentTimeMillis());         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Open.class), 0);         notification.setLatestEventInfo(this, filename+" downloaded", "Click to open folder", contentIntent);         manager.notify(1, notification); 

and my Open class it's here:

public class Open extends Activity {      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          try{         Intent intent = new Intent("org.openintents.action.PICK_FILE");         startActivityForResult(intent, 1);     }      catch(Exception e)     {         Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();         Log.v("EX",e.toString());     }     }  } 

This opens oi file manager (without "open with" - i didnt chose default app) and to /sdcard, that's why i need your help.

回答1:

Intent toLaunch = new Intent(); toLaunch.setAction(android.content.Intent.ACTION_VIEW); toLaunch.setDataAndType(Uri.fromFile(new File("file_path")), MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpeg"));  // you can also change jpeg to other types 

and instead of:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Open.class), 0); 

write:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, toLaunch , 0); 


回答2:

Notifications are designed to launch activities. In your case, you are launching Listar.class. That class should do the action you require, such as opening / displaying a file.

http://developer.android.com/reference/android/app/PendingIntent.html should describe the purpose of the PendingIntent that you have constructed.



回答3:

If you wanting to find out if a specific intent exists (e.g. the package is installed) you can use something like this...

/**  * Checks the system to see if the passed intent package exists.  *   * @param pIntent  *            intent to be checked  * @return true if the package exists  */ private final boolean checkIfPackageExists( final Intent pIntent ) {     // Build package manager     final PackageManager packageManager = this.getPackageManager();     final List<ResolveInfo> list = packageManager.queryIntentActivities( pIntent, PackageManager.MATCH_DEFAULT_ONLY );      return list.size() > 0; } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!