其他分享
首页 > 其他分享> > 05.鸿蒙HarmonyOS卡片 打开指定应用或华为应用市场对应App详情页

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();
        }
    }

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

标签:bundleName,ability,05,App,HarmonyOS,intent,new,Intent,operation
来源: https://blog.csdn.net/jingbin_/article/details/120708918