Android通过包名启动应用程序

RT,贴代码:

 1 public void startAppSafe(String pkgName){
 2         PackageManager pm = getPackageManager();
 3         Intent in = new Intent(Intent.ACTION_MAIN);
 4         in.addCategory(Intent.CATEGORY_LAUNCHER);
 5         in.setPackage(pkgName);
 6         List<ResolveInfo> ri = pm.queryIntentActivities(in, 0);
 7         if (ri != null && ri.size() > 0) {
 8             Intent i = new Intent();
 9             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10             i.setClassName(pkgName, ri.get(0).activityInfo.name);
11             try {
12                 startActivity(i);
13             } catch (Exception e) {
14                 Toast.makeText(this,
15                         res.getString(R.string.activity_not_found),
16                         Toast.LENGTH_SHORT).show();
17             }
18         } else {
19             Toast.makeText(this, res.getString(R.string.activity_not_found),
20                     Toast.LENGTH_SHORT).show();
21         }
22     }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。