Android starts an application that only knows the package name. android package name

Source: Internet
Author: User

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 );}}}

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.