從App啟動另一個App的方式
阿新 • • 發佈:2018-12-06
最安全方式
public static void startApp(Context context, String packageName) { try { PackageManager packageManager = context.getApplicationContext().getPackageManager(); Intent intent = packageManager.getLaunchIntentForPackage(packageName); if (intent == null) { LogUtils.d(packageName + ",應用不存在"); } else { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(intent); } } catch (Exception e) { e.printStackTrace(); } }
另外兩種方式,如果第三方app做個引數校驗,可能導致啟動失敗
Intent intent=new Intent();
//包名 包名+類名(全路徑)
intent.setClassName("com.linxcool", "com.linxcool.PlaneActivity");
startActivity(intent);
Intent intent = new Intent(); ComponentName comp = new ComponentName("com.linxcool","com.linxcool.PlaneActivity"); intent.setComponent(comp); intent.setAction("android.intent.action.MAIN"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);