public static boolean isAPPInstall(Ability ability, String bundleName) {
try {
ability.getBundleManager().isApplicationEnabled(bundleName);
return true;
} catch (java.lang.IllegalArgumentException ex) {
return false;
}
}
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)
.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();
}
}
亲测可用,有问题留言哈~
|