做过launcher的朋友都知道,有一些应用是有多个入口的。我们需要把所有入口都显示在桌面上。
如何获取桌面应用程序
/**
* 获取能在桌面上使用得app
*
* @return
*/
public static List<ResolveInfo> getIntentActivity() {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setAction(Intent.ACTION_MAIN);
PackageManager manager = MyApplication.getContext().getApplicationContext().getPackageManager();
List<ResolveInfo> resolveInfos = manager.queryIntentActivities(intent, 0);
return resolveInfos;
}
如何获取桌面图标 包名 应用名字以及入口Activity(入口activity是关键)
Drawable drawable = info.loadIcon(packageManager);//图标
String name = info.loadLabel(packageManager).toString();//应用名
String packageName = info.activityInfo.packageName;//包名
String targetActivity = info.activityInfo.name;//入口activity 绝对路径(不是targetActivity)
如何打开第三方应用
平常我们只通过包名打开的话 永远只能打开第一个入口activity
//这里我们不用这个
public static void startAppByPackageName(String packageName) {
PackageManager packageManager = MyApplication.getContext().getApplicationContext().getPackageManager();
if (checkPackInfo(packageName)) {
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
MyApplication.getContext().getApplicationContext().startActivity(intent);
} else {
MyApplication.showToast(MyApplication.getContext().getApplicationContext().getString(R.string.str_not_install));
}
}
想打开不同的入口用这个
public static void startTargetActivity(String packageName, String targetActivity) {
Intent intent = new Intent();
ComponentName cn = new ComponentName(packageName, targetActivity);
try {
intent.setComponent(cn);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//别忘了这个
//18.03.23更新
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); //launcher系统launcher会添加这个
MyApplication.getContext().getApplicationContext().startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
MyApplication.showToast(MyApplication.getContext().getApplicationContext().getString(R.string.str_not_install));
}
}
网友评论