Open third-party application from intention

I am working on an application, I am going to open other applications. The only problem is that I do not know how to access third-party applications. I plan to use the intention. Can you refer to it using only the package name, or do you need the intention of the main activity. Are there any simple ways to find the right intention, and then refer to it.

+2
source share
1 answer

I am working on an application, I am going to open other applications.

I interpret this as meaning that you are creating a launcher, akin to what is on home screens.

Can you refer to it using only the package name, or do you need the intention of the main activity.

Launchers use ACTION_MAIN / CATEGORY_LAUNCHER Intent .

Are there any simple ways to find the right intention, and then refer to it.

Use the PackageManager to find all possible ACTION_MAIN / CATEGORY_LAUNCHER actions on the device, and then display them to the user to choose from. You can then create a suitable Intent to launch your specific selection.

Here is an example project that implements a launcher.

To find a list of things that can be started, this sample application uses:

 PackageManager pm=getPackageManager(); Intent main=new Intent(Intent.ACTION_MAIN, null); main.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0); 

And here is the real launch logic, based on the user clicking one of these "triggered" in ListActivity :

  @Override protected void onListItemClick(ListView l, View v, int position, long id) { ResolveInfo launchable=adapter.getItem(position); ActivityInfo activity=launchable.activityInfo; ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name); Intent i=new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); i.setComponent(name); startActivity(i); } 
+2
source

Source: https://habr.com/ru/post/1416561/


All Articles