Start your own Android Application
- The first parameter is the package name of the application, and the last parameter is the main activity name of the application.
- Intent intent = new intent ();
- Intent. setcomponent (New componentname ("com. droidnova. Android. Games. vortex ",
- "Com. droidnova. Android. Games. vortex. vortex "));
- Startactivity (intent );
Start the application that comes with the System
- Intent intent = new intent ();
- Intent. setcomponent (New componentname ("com. Android. Calendar", "com. Android. Calendar. launchactivity "));
- 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 );
}
}