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     }

 

posted @ 2014-01-23 16:50  wesley.du  阅读(2121)  评论(0编辑  收藏  举报