/**
* 判断app是否安装
*
* @param bundleName 包名
*/
public static boolean isAPPInstall(Ability ability, String bundleName) {
try {
ability.getBundleManager().isApplicationEnabled(bundleName);
return true;
} catch (java.lang.IllegalArgumentException ex) {
return false;
}
}
/**
* 已经安装直接打开,没有安装打开应用市场
*
* @param bundleName 包名
*/
public static void launchApp(Ability ability, String bundleName) {
try {
if (isAPPInstall(ability, bundleName)) {
// 已经安装
Intent intent = new Intent();
Set<String> entities = new HashSet<>();
entities.add("android.intent.category.LAUNCHER");
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(bundleName)
// 打开的哪个activity,例如
.withAbilityName("com.example.jingbin.cloudreader.ui.LoadingActivity")
.withAction("android.intent.action.MAIN")
.withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
.withEntities(entities)
.build();
intent.setOperation(operation);
ability.startAbility(intent);
} else {
// 没有安装
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withUri(Uri.parse("market://details?id=" + bundleName))
.withAction("android.intent.action.VIEW")
.withBundleName("com.huawei.appmarket")
.withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
.build();
intent.setOperation(operation);
ability.startAbility(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
亲测可用,有问题留言哈~