条件:知道 包名 和程序的主入口 Activity
代码如下:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
条件:只知道 包名
代码如下:
/**
* 打开app
*
* @param packageName 程序包名
* @throws NameNotFoundException
*/
private void openApp(String packageName) throws NameNotFoundException {
PackageManager pm = mCurActivity.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
String pkgName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(pkgName, className);
intent.setComponent(cn);
mCurActivity.startActivity(intent);
}
}
相比下,情况二比较常用。