(a) through the package name, class name
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:
[Java]View Plaincopy
- 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:
[Java]View Plaincopy
- 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 activity[form of the app we're looking for: 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);
- }
- }
(ii) by action
(c) Transmission parameters: http://www.aboutit.cn/post/195
Launch another app