1, know the name of the package to start and the activity to start:
Intent Intent = new Intent ();
/** the parameters of the following methods refer to the package name to start the application and the corresponding activity*/
ComponentName cpn= New ComponentName ("Com.ldm.demo", "com.ldm.demo.TextActivity");
Intent. SetComponent (CPN);
StartActivity (Intent);
2, only know the package name of the app:
Packagemanager Packagemanager = Getpackagemanager ();
Intent intent=new Intent ();
Intent =packagemanager.getlaunchintentforpackage ("PackageName");/**packagename refers to the package name to start the application */
if (intent==null) {
System.out.println ("APP not found!");
}
StartActivity (Intent);
}
}
3, know the package name to start and the activity app to start to start and pass the data:
- ComponentName componentname = new ComponentName ("Com.ldm.demo", "com.ldm.demo.TestActivity");
- Intent Intent = new Intent ();
- Bundle bundle = new bundle ();
- Bundle.putstring ("Data", data);
- Intent.putextras (bundle);
- Intent.setcomponent (componentname);
- StartActivity (Intent);
- 4, the case of unknown package name:
- /** Gets the package name of the app in the phone and returns a list collection **/
Public list<packageinfo> Getallapps () {
list<packageinfo> apps = new arraylist<packageinfo> ();
Packagemanager Packagemanager = This.getpackagemanager ();
/** get all apps in your phone * /
list<packageinfo> paklist = pmanager.getinstalledpackages (0);
for (int i = 0; i < paklist.size (); i++) {
PackageInfo Pak = (packageinfo) paklist.get (i);
/** Determine if a non-system preinstalled application (greater than 0 is pre-installed for the system, less than or equal to 0 for non-system applications) */
if ((Pak.applicationInfo.flags & Pak.applicationInfo.FLAG_SYSTEM) <= 0) {
Apps.add (Pak);
}
}
return apps;
}
Once you have the package name, you can start the app by getting the package name that you want to start:
Public void Launchapp () {
Packagemanager Packagemanager = This.getpackagemanager ();
list<packageinfo> packages = Getallapps ();
PackageInfo pa = null;
for (int i=0;i<packages.size (); i++) {
pa = packages.get (i);
/** Get the application name * /
String Applabel = Packagemanager.getapplicationlabel (pa.applicationinfo). toString ();
/** Get Package name * /
String apppackage = pa.packagename;
log.d ("+i, applabel+" "+apppackage");
}
Intent Intent = packagemanager.getlaunchintentforpackage ("Jp.co.johospace.jorte");
startactivity (Intent);
}
Several ways to launch other apps in Android