最近要用到从一个应用调用另一个应用的Activity,我封装了一个方法,直接调用这个方法就可以,前提是要知道要调用应用的包名。
以下是实现方法的代码:
1 2 /** 3 * 根据包名调用app 4 * @param packageName 5 */ 6 public void openApp(String packageName) { 7 PackageInfo pi; 8 try { 9 pi = getPackageManager().getPackageInfo(packageName, 0); 10 PackageManager pm = this.getPackageManager(); 11 Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); 12 resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); 13 resolveIntent.setPackage(pi.packageName); 14 List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0); 15 16 ResolveInfo ri = apps.iterator().next(); 17 if (ri != null ) { 18 String packageName2 = ri.activityInfo.packageName; 19 String className = ri.activityInfo.name; 20 21 Intent intent = new Intent(Intent.ACTION_MAIN); 22 intent.addCategory(Intent.CATEGORY_LAUNCHER); 23 24 ComponentName cn = new ComponentName(packageName2, className); 25 26 intent.setComponent(cn); 27 startActivity(intent); 28 } 29 }catch (NameNotFoundException e) { 30 31 Toast.makeText(MainActivity.this, "请先下载应用",Toast.LENGTH_LONG).show(); 32 } 33 } 34
直接调用这个方法就可以了:
注意这里的XXXXXX是要调用应用的包名(如:com.xxx.xxx)
openApp("XXXXXX");