Bootstrap

android 通过包名打开应用程序,Android通过包名打开第三方应用

importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.content.pm.PackageInfo;importandroid.content.pm.PackageManager;importandroid.content.pm.ResolveInfo;importandroid.widget.Toast;importjava.util.List;public classJump2AppUtil {privateJump2AppUtil() {

}public static voidJump2App(Context context, String packagename) {//通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等

PackageInfo packageinfo = null;try{

packageinfo= context.getPackageManager().getPackageInfo(packagename, 0);

}catch(PackageManager.NameNotFoundException e) {

e.printStackTrace();

}catch(Exception ex) {

ex.printStackTrace();

}if (packageinfo == null) {

Toast.makeText(context,"还未集成该模块,敬请期待", Toast.LENGTH_SHORT).show();return;

}//创建一个类别为CATEGORY_LAUNCHER的该包名的Intent

Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);

resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);

resolveIntent.setPackage(packageinfo.packageName);//通过getPackageManager()的queryIntentActivities方法遍历

List resolveinfoList =context.getPackageManager()

.queryIntentActivities(resolveIntent,0);

ResolveInfo resolveinfo=resolveinfoList.iterator().next();if (resolveinfo != null) {//packagename = 参数packname

String packageName =resolveinfo.activityInfo.packageName;//这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]

String className =resolveinfo.activityInfo.name;//LAUNCHER Intent

Intent intent = newIntent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置ComponentName参数1:packagename参数2:MainActivity路径

ComponentName cn = newComponentName(packageName, className);

intent.setComponent(cn);

context.startActivity(intent);

}

}

}

;