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!