方法一:
Intent intent = new Intent();
intent.setAction("com.lzy.example.MAIN"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent);注:在要啓動的應用程序主入口中添加
<intent-filter>
<action android:name="com.lzy.example.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
// ComponentName
方法二:(如果要啓動的Activity不是主Activity,需要在AndroidManifest添加exported=true)
Intent intent1 = new Intent();intent1.setComponent(new ComponentName("com.lzy.example",
"com.lzy.example.MainActivity"));
Bundle bundle = new Bundle();
bundle.putString("abc", "哈哈哈哈哈");
intent1.putExtras(bundle);
startActivity(intent1);
/** * 判斷相對應的APP是否存在 * * @param context * @param packageName(包名) * @return */ private boolean isAvilible(Context context, String packageName) throws Exception { PackageManager packageManager = context.getPackageManager(); //獲取手機系統的所有APP包名,然後進行一一比較 List<PackageInfo> pinfo = packageManager.getInstalledPackages(0); for (int i = 0; i < pinfo.size(); i++) { if (((PackageInfo) pinfo.get(i)).packageName.equalsIgnoreCase(packageName)) return true; } return false; } /** * 判斷我們的手機中是否存在我們要的這個action * * @param context * @param action * @return */ private boolean isActionSupport(Context context,String action){ final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfo.size() > 0) { return true; } return false; }