要获取可执行应用的列表(Launcher点中间键后进入看到的那个),就需要用到PackageManager。
首先,使用PackageManager解析列表
PackageManager pm = getPackageManager(); // 解析所有可执行的应用 Intent it = new Intent(Intent.ACTION_MAIN); it.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> lai = pm.queryIntentActivities(it, 0);
然后,就可以使用for循环获取信息。循环前面记得判断非空。
for(ResolveInfo l : lai){ ActivityInfo ai = l.activityInfo; ApplicationInfo api = ai.applicationInfo; // ComponentName可以用来启动该应用 ComponentName c = new ComponentName(ai.applicationInfo.packageName,ai.name); // 相关处理 。。。 }
最后,就可以通过Intent调用相应Activity
// 两种方式都可以执行 // 1、使用ACTION_MAIN it = new Intent(Intent.ACTION_MAIN); it.addCategory(Intent.CATEGORY_DEFAULT); it.setPackage(lai.get(0).activityInfo.applicationInfo.packageName); // 2、使用ACTION_VIEW与之前保存的ComponentName it = new Intent(Intent.ACTION_VIEW); it.setComponent(c); // 最后启动 startActivity(it);