Start another program in Android development _ Program

Source: Internet
Author: User

Start your own Android Application

  1. The first parameter is the package name of the application, and the last parameter is the main activity name of the application.
  2. Intent intent = new intent ();
  3. Intent. setcomponent (New componentname ("com. droidnova. Android. Games. vortex ",
  4. "Com. droidnova. Android. Games. vortex. vortex "));
  5. Startactivity (intent );


Start the application that comes with the System

  1. Intent intent = new intent ();
  2. Intent. setcomponent (New componentname ("com. Android. Calendar", "com. Android. Calendar. launchactivity "));
  3. Startactivity (intent );

Start another program without knowing activityname

Ttp: // hi.baidu.com/xiaofanqing/item/6fd724f7c5bb6dce531c26b7

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 );
Intent. addcategory (intent. category_launcher );
Componentname Cn = new componentname (packagename, classname );
Intent. setcomponent (CN );
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 ){
Packageinfo Pi = getpackagemanager (). getpackageinfo (packagename, 0 );

Intent resolveintent = new intent (intent. action_main, null );
Resolveintent. addcategory (intent. category_launcher );
Resolveintent. setpackage (PI. packagename );

List <resolveinfo> apps = PM. queryintentactivities (resolveintent, 0 );

Resolveinfo rI = apps. iterator (). Next ();
If (Ri! = NULL ){
String packagename = Ri. activityinfo. packagename;
String classname = Ri. activityinfo. Name;

Intent intent = new intent (intent. action_main );
Intent. addcategory (intent. category_launcher );

Componentname Cn = new componentname (packagename, classname );

Intent. setcomponent (CN );
Startactivity (intent );
}
}

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.