Android starts another program in one program

Source: Internet
Author: User

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.