Android App code opened by package name

Source: Internet
Author: User

When the launcher is used, the user clicks the apk icon and needs to open this apk. There are two ways to start this apk:

First: Know the package name of the apk and its main Activity

// Help private ComponentName help_set; private final static String help_set_pack = "cn. abc. help "; private final static String help_set_name =" cn. abc. help. mainActivity ";/*** start an app * com -- ComponentName object, package name containing apk and main Activity name * param -- parameters to be passed to apk */private void startApp (ComponentName com, String param) {if (com! = Null) {PackageInfo packageInfo; try {packageInfo = getPackageManager (). getPackageInfo (com. getPackageName (), 0);} catch (NameNotFoundException e) {packageInfo = null; Toast. makeText (this, "not installed", Toast. LENGTH_SHORT ). show (); e. printStackTrace ();} try {Intent intent = new Intent (); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. setComponent (com); if (param! = Null) {Bundle bundle = new Bundle (); // create a Bundle object bundle. putString ("flag", param); // load data intent. putExtras (bundle); // insert Bundle into Intent} startActivity (intent);} catch (Exception e) {Toast. makeText (this, "Startup exception", Toast. LENGTH_SHORT ). show ();}}}


Second: only know the package name of the apk. This method is the most commonly used. After all, the apk to be started is not necessarily written by ourselves.

/** Start an app */public void startAPP (String appPackageName) {try {Intent intent = this. getPackageManager (). getLaunchIntentForPackage (appPackageName); startActivity (intent);} catch (Exception e) {Toast. makeText (this, "not installed", Toast. LENGTH_LONG ). show ();}}


In combination, the code of the second method is much simpler and more practical!


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.