05.鸿蒙HarmonyOS卡片 打开指定应用或华为应用市场对应App详情页

/**
 * 判断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();
        }
    }

亲测可用,有问题留言哈~

上一篇:直播预告丨“Hello Ability:从页面跳转开始”周三晚不见不散


下一篇:route中的to.matched的运用