0

我想从应用程序库或 Playstore 安装配套应用程序,无论是在华为还是非华为设备上可用。

今天我打开playstore的工作代码是:

val appId = "com.my.app.i.want.to.install"
val installAppMarketIntent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId))
val installAppUrlIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+appId))
try {
    activity.startActivity(installAppMarketIntent)
}catch (anfe: android.content.ActivityNotFoundException){
    activity.startActivity(installAppUrlIntent)
}finally {
    activity!!.finish()
}

我应该为华为设备添加验证吗?或者这个意图将适用于带有 AppGallery 的华为设备?

4

2 回答 2

1

对于科特林

 val uri = Uri.parse("market://details?id=" + getPackageName())
                val goToMarket = Intent(Intent.ACTION_VIEW, uri)
                goToMarket.addFlags(
                    Intent.FLAG_ACTIVITY_NO_HISTORY or
                            Intent.FLAG_ACTIVITY_NEW_DOCUMENT or
                            Intent.FLAG_ACTIVITY_MULTIPLE_TASK
                )
                try {
                    startActivity(goToMarket)
                } catch (e: ActivityNotFoundException) {
                    startActivity(
                        Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())
                        )
                    )
                }

对于 Java

  public void openPlayStore(Context context) {
        // you can also use BuildConfig.APPLICATION_ID
        String appId = context.getPackageName();
        Intent playIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId));
        final List<ResolveInfo> otherApps = context.getPackageManager().queryIntentActivities(playIntent, 0);
        for (ResolveInfo otherApp : otherApps) {
            // look for Google Play application
            if (otherApp.activityInfo.applicationInfo.packageName
                    .equals("com.android.vending")) {
                ActivityInfo otherAppActivity = otherApp.activityInfo;
                ComponentName componentName = new ComponentName(
                        otherAppActivity.applicationInfo.packageName,
                        otherAppActivity.name
                );
               playIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               playIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                playIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                playIntent.setComponent(componentName);
                context.startActivity(playIntent);

            }
        }
    }

于 2020-06-24T18:47:11.450 回答
1

是的,您应该为华为设备添加验证。

("market://details?id=" + appId)意图是针对 Google Play。

华为 AppGallery 应该是:

“appmarket://details?id=”+ pkgName(pkgName 是应用的包名)

或者

“market://com.huawei.appmarket.applink?appId=” + appid
于 2020-06-28T08:17:48.747 回答