在应用中打开别人的应用程序方法

在做项目过程中,经常会用到要在应用中打开另外的应用程序,碰到这样的需求,处理方法有两种:

方法一:

必须要已经知道应用程序的包名和主页面类名

1 Intent it = new Intent(Intent.ACTION_MAIN);
2
3 it.addCategory(Intent.CATEGORY_LAUNCHER);
4 ComponentName cn = new ComponentName("com.ajie",
5 "com.ajie.LoadActivity");
6 it.setComponent(cn);
7 startActivity(it);

方法二:

知道应用程序包名(如果不知道应用程序包名,请反编译应用程序),不知道主页面类名

 1 private void openApp(String packageName) {
2 try {
3 PackageManager pm = getPackageManager();
4 PackageInfo pi = pm.getPackageInfo(packageName, 0);
5
6 Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
7 resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
8 resolveIntent.setPackage(pi.packageName);
9
10 List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
11
12 ResolveInfo ri = apps.iterator().next();
13 if (ri != null) {
14 packageName = ri.activityInfo.packageName;
15 String className = ri.activityInfo.name;
16
17 Intent intent = new Intent(Intent.ACTION_MAIN);
18 intent.addCategory(Intent.CATEGORY_LAUNCHER);
19
20 ComponentName cn = new ComponentName(packageName, className);
21
22 intent.setComponent(cn);
23 startActivity(intent);
24 }
25 } catch (NameNotFoundException e) {
26 Toast.makeText(getBaseContext(), "启动失败", Toast.LENGTH_LONG).show();
27 e.printStackTrace();
28 }
29 }

你可能感兴趣的:(应用程序)