【问题标题】:Intent for install app from app gallery or playstore从应用程序库或 Playstore 安装应用程序的意图
【发布时间】:2020-06-24 16:36:04
【问题描述】:

我想从应用程序库或 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 的华为设备?

【问题讨论】:

  • 我觉得很好。您是否尝试过并且有任何错误?理想情况下,如果应用程序库支持,您只需使用 market:// URI 方案。
  • 避免!!运算符,activity?.finish() 更好,从不抛出。

标签: java android kotlin huawei-mobile-services appgallery


【解决方案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);

            }
        }
    }

【讨论】:

    【解决方案2】:

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

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

    华为应用市场应该是:

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

    “market://com.huawei.appmarket.applink?appId=” + appid
    

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多