Development sometimes requires that another application be launched in one application, such as a list of installed programs that launcher loads, and another app can be launched when the icon is clicked. In general, we know that another app's package name and mainactivity name can be started directly from the following code:
Intent Intent = new Intent (intent.action_main);
Intent.addcategory (Intent.category_launcher);
ComponentName cn = New ComponentName (PackageName, className);
Intent.setcomponent (CN);
StartActivity (Intent);
But more often, we do not know the target application's Startup activity class name, but only know the package name, then we need to do this, I encapsulated into a method, as follows:
private void Dostartapplicationwithpackagename (String packagename) {
Get this app details through the package name, including activities, services, Versioncode, name, and more
PackageInfo packageinfo = null;
try {
PackageInfo = Getpackagemanager (). Getpackageinfo (PackageName, 0);
} catch (Namenotfoundexception e) {
E.printstacktrace ();
}
if (PackageInfo = = null) {
Return
}
Create a intent of the package name that is a category Category_launcher
Intent resolveintent = new Intent (intent.action_main, NULL);
Resolveintent.addcategory (Intent.category_launcher);
Resolveintent.setpackage (Packageinfo.packagename);
Traverse through the Queryintentactivities method of the Getpackagemanager ()
list<resolveinfo> resolveinfolist = Getpackagemanager ()
. Queryintentactivities (resolveintent, 0);
ResolveInfo ResolveInfo = Resolveinfolist.iterator (). Next ();
if (resolveinfo! = null) {
PackageName = parameter Packname
String PackageName = resolveinfo.activityInfo.packageName;
This is the launcher of the app we're looking for. Activity[Organization: Packagename.mainactivityname]
String className = resolveinfo.activityInfo.name;
LAUNCHER Intent
Intent Intent = new Intent (intent.action_main);
Intent.addcategory (Intent.category_launcher);
Set componentname parameter 1:packagename parameter 2:mainactivity path
ComponentName cn = New ComponentName (PackageName, className);
Intent.setcomponent (CN);
StartActivity (Intent);
}
}
Android launches another app in one app by package name or class name