Android Developers sometimes need to start another application in one application. For example, launcher loads the list of all installed programs and can start another application when you click the icon.
Generally, after knowing the package name and mainactivity name of another application, we can start the application directly using the following code:
Intent intent = new intent (intent. action_main); <br/> intent. addcategory (intent. category_launcher); <br/> componentname Cn = new componentname (packagename, classname); <br/> intent. setcomponent (CN); <br/> startactivity (intent );
But more often, we generally do not know the class name of the activity to be started by the application, but only the package name. We can use the resolveinfo class to obtain the class name to start acitivty.
The following is the implementation code:
Private void openapp (string packagename) {<br/> packageinfo Pi = getpackagemanager (). getpackageinfo (packagename, 0); <br/> intent resolveintent = new intent (intent. action_main, null); <br/> resolveintent. addcategory (intent. category_launcher); <br/> resolveintent. setpackage (Pi. packagename); <br/> List <resolveinfo> apps = PM. queryintentactivities (resolveintent, 0); <br/> resolveinfo rI = apps. iterator (). n EXT (); <br/> If (Ri! = NULL) {<br/> string packagename = Ri. activityinfo. packagename; <br/> string classname = Ri. activityinfo. name; <br/> intent = new intent (intent. action_main); <br/> intent. addcategory (intent. category_launcher); <br/> componentname Cn = new componentname (packagename, classname); <br/> intent. setcomponent (CN); <br/> startactivity (intent); <br/>}< br/>}
PS: This may be helpful to anyone who wants to launch the launcher ~