Android starts an application that only knows the package name. android package name
We know that to start an application, we need to know the name of the application to start the Activity, but the ACTION parameter to start the Activity. In this way, we can start the application through startActivity (Intent), for example:
/** The package name of the started application is com. xx. xx. The application portal is com. xx. xx. testLaunchActivity **/Intent intent = new Intent (); intent. setComponent (new ComponentName ("com. xx. xx "," com. xx. xx. testLaunchActivity "); intent. setAction (Intent. ACTION_VIEW); startActivity (intent );
So if we don't know how to start the Activity of the application, how can we only?
The Android SDK has the following API: public abstract Intent getLaunchIntentForPackage (String packageName ). If packageName exists, Intent is returned. startActivity (Intent) can start the application; otherwise, nullimport android. app. Activity is returned;
Public class Main extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. test_main );
/** Package Manager */PackageManager packageManager = getPackageManager (); Intent intent = new Intent ();
/** Get Intent */
Intent = packageManager. getLaunchIntentForPackage ("com. xx. xx"); // com. xx. xx is the package name we obtained if (intent! = Null) {startActivity (intent );}}}