通过应用名获取包名
/**
* 通过应用名获取包名
*
* @param name 应用名
* @return 包名
*/
public static String getPackageNameByAppName(Context context, @NonNull String name) {
try {
if (TextUtils.isEmpty(name)) {
return null;
}
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
for (PackageInfo packageInfo : installedPackages) {
String appName = packageInfo.applicationInfo.loadLabel(packageManager).toString();
if (name.equals(appName)) {
return packageInfo.packageName;
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取手机中的所有应用的信息
/**
* 获取手机中的所有应用的信息
* @param context context
*/
public void getInstalledApplications(Context context) {
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
for (PackageInfo packageInfo : installedPackages) {
String packageName = packageInfo.packageName;
String versionName = packageInfo.versionName;
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
String appName = applicationInfo.loadLabel(packageManager).toString();
Drawable icon = applicationInfo.loadIcon(packageManager);
int flags = applicationInfo.flags;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
} else {
}
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
} else {
}
}
}
启动App
/**
* 启动app
*
* @param context context
* @param appPackageName 应用包名
*/
public static void startAPP(Context context, String appPackageName) {
try {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
context.startActivity(intent);
} catch (Exception e) {
ToastUtil.showDebugToast(context, "没有安装");
}
}