【问题标题】:How to open the Google Play Store directly from my Android application?如何直接从我的 Android 应用程序打开 Google Play 商店?
【发布时间】:2012-07-29 23:31:48
【问题描述】:

我已经使用以下代码打开了 Google Play 商店

Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename "));
startActivity(i);.

但它显示了一个完整的操作视图来选择选项(浏览器/播放商店)。我需要直接在 Play Store 中打开应用程序。

【问题讨论】:

标签: android android-intent google-play


【解决方案1】:

您可以使用market:// prefix 执行此操作。

Java

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

科特林

try {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: ActivityNotFoundException) {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName")))
}

我们在此处使用 try/catch 块,因为如果目标设备上未安装 Play 商店,则会抛出 Exception

注意:任何应用都可以注册为能够处理 market://details?id=<appId> Uri,如果您想专门针对 Google Play,请查看 Berťák 答案

【讨论】:

  • 如果你想重定向到所有开发者的应用,请使用market://search?q=pub:"+devNamehttp://play.google.com/store/search?q=pub:"+devName
  • 此解决方案不起作用,如果某些应用程序使用定义了“market://”方案的意图过滤器。请参阅我的回答如何打开 Google Play 且仅打开 Google Play 应用程序(如果 GP 不存在,则为网络浏览器)。 :-)
  • 对于使用 Gradle 构建系统的项目,appPackageName 实际上是 BuildConfig.APPLICATION_ID。无Context/Activity 依赖,降低内存泄漏风险。
  • 您仍然需要上下文来启动意图。 Context.startActivity()
  • 此解决方案假定有打开 Web 浏览器的意图。这并不总是正确的(就像在 Android TV 上一样)所以要小心。您可能想使用 intent.resolveActivity(getPackageManager()) 来确定要做什么。
【解决方案2】:

这里很多答案建议使用Uri.parse("market://details?id=" + appPackageName))打开Goog​​le Play,但我觉得其实不够

一些第三方应用程序可以使用自己定义的"market://" 方案的意图过滤器,因此它们可以处理提供的 Uri 而不是 Google Play(我在例如 SnapPea 应用程序中遇到过这种情况)。问题是“如何打开 Google Play 商店?”,所以我假设您不想打开任何其他应用程序。另请注意,例如应用评级仅与 GP Store 应用等相关...

要打开 Google Play 并且只打开 Google Play,我使用以下方法:

public static void openAppRating(Context context) {
    // you can also use BuildConfig.APPLICATION_ID
    String appId = context.getPackageName();
    Intent rateIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("market://details?id=" + appId));
    boolean marketFound = false;

    // find all applications able to handle our rateIntent
    final List<ResolveInfo> otherApps = context.getPackageManager()
        .queryIntentActivities(rateIntent, 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
                    );
            // make sure it does NOT open in the stack of your activity
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // task reparenting if needed
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            // if the Google Play was already open in a search result
            //  this make sure it still go to the app page you requested
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // this make sure only the Google Play app is allowed to
            // intercept the intent
            rateIntent.setComponent(componentName);
            context.startActivity(rateIntent);
            marketFound = true;
            break;

        }
    }

    // if GP not present on device, open web browser
    if (!marketFound) {
        Intent webIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://play.google.com/store/apps/details?id="+appId));
        context.startActivity(webIntent);
    }
}

重点是当 Google Play 以外的更多应用可以打开我们的 Intent 时,会跳过 app-chooser 对话框,直接启动 GP 应用。

更新: 有时它似乎只打开 GP 应用程序,而不打开应用程序的配置文件。正如 TrevorWiley 在他的评论中所建议的那样,Intent.FLAG_ACTIVITY_CLEAR_TOP 可以解决这个问题。 (我自己还没测试过……)

请参阅 this answer 了解 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 的作用。

【讨论】:

  • 虽然这很好,但当前的 Google Play 版本似乎也不可靠,如果您在 Google Play 上进入另一个应用程序页面然后触发此代码,它只会打开 Google Play 而不会转到您的应用程序。
  • @zoltish,我已将 Intent.FLAG_ACTIVITY_CLEAR_TOP 添加到标志中,这似乎解决了问题
  • 我用过 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 但不起作用。 Play商店没有任何新的实例打开
  • 如果您使用 rateIntent.setPackage("com.android.vending") 来确保 PlayStore 应用程序会处理这个意图,而不是所有这些代码,会发生什么?
  • @dum4ll3 我想你可以,但这段代码也隐式检查是否安装了 Google Play 应用程序。如果您不检查它,则需要捕获 ActivityNotFound
【解决方案3】:

转到Android Developer官方链接作为教程一步一步查看并从Play商店获取您的应用程序包的代码,如果存在或Play商店应用程序不存在,然后从Web浏览器打开应用程序。

Android 开发者官方链接

https://developer.android.com/distribute/tools/promote/linking.html

链接到应用程序页面

来自网站:https://play.google.com/store/apps/details?id=&lt;package_name&gt;

来自 Android 应用:market://details?id=&lt;package_name&gt;

链接到产品列表

来自网站:https://play.google.com/store/search?q=pub:&lt;publisher_name&gt;

来自 Android 应用:market://search?q=pub:&lt;publisher_name&gt;

链接到搜索结果

来自网站:https://play.google.com/store/search?q=&lt;search_query&gt;&amp;c=apps

来自 Android 应用:market://search?q=&lt;seach_query&gt;&amp;c=apps

【讨论】:

  • 不再推荐使用 market:// 前缀(查看您发布的链接)
  • @GregEnnis 您在哪里看到不再推荐使用 market:// 前缀?
  • @lok​​i 我认为关键是它不再被列为建议。如果您在该页面搜索单词market,您将找不到任何解决方案。我认为新方法是激发更通用的意图 developer.android.com/distribute/marketing-tools/… 。 Play Store 应用的更新版本可能对此 URI https://play.google.com/store/apps/details?id=com.example.android 有一个意图过滤器
【解决方案4】:

虽然 Eric 的回答是正确的,并且 Berťák 的代码也有效。我认为这两者结合得更优雅。

try {
    Intent appStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
    appStoreIntent.setPackage("com.android.vending");

    startActivity(appStoreIntent);
} catch (android.content.ActivityNotFoundException exception) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

通过使用setPackage,您可以强制设备使用 Play 商店。如果没有安装 Play 商店,Exception 将被捕获。

【讨论】:

  • 官方文档用https://play.google.com/store/apps/details?id=而不是market:怎么来的? developer.android.com/distribute/marketing-tools/… 仍然是一个全面而简短的答案。
  • 我试过 Berťák 的代码,它可以工作,但这个要简单得多。
【解决方案5】:

试试这个

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);

【讨论】:

  • 关于如何独立打开google play(不嵌入在同一个应用的新视图中)请查看我的回答。
【解决方案6】:

如果您真的想独立打开 Google Play(或任何其他应用),上述所有答案都会在同一应用的新视图中打开 Google Play:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");

// package name and activity
ComponentName comp = new ComponentName("com.android.vending",
                                       "com.google.android.finsky.activities.LaunchUrlHandlerActivity"); 
launchIntent.setComponent(comp);

// sample to open facebook app
launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana"));
startActivity(launchIntent);

重要的是实际独立打开google play或任何其他应用。

我所看到的大部分内容都使用其他答案的方法,这不是我所需要的,希望这对某人有所帮助。

问候。

【讨论】:

  • 什么是this.cordova?变量声明在哪里? callback 在哪里声明和定义?
  • 这是 Cordova 插件的一部分,我认为这实际上并不相关......您只需要一个 PackageManager 实例并以常规方式启动一个活动,但这是 Cordova 插件github.com/lampaa 我在这里改写了 github.com/code4jhon/org.apache.cordova.startapp
  • 我的意思很简单,这个代码并不是人们可以简单地移植到他们自己的应用程序中使用的东西。修剪脂肪,只留下核心方法将对未来的读者有用。
  • 是的,我明白了……现在我正在使用混合应用程序。无法真正测试完整的本机代码。但我认为这个想法是存在的。如果有机会,我会添加准确的原生行。
  • 希望这将使它成为@eric
【解决方案7】:

您可以检查是否安装了 Google Play Store 应用,如果是这样,您可以使用 "market://" 协议。 p>

final String my_package_name = "........."  // <- HERE YOUR PACKAGE NAME!!
String url = "";

try {
    //Check whether Google Play store is installed or not:
    this.getPackageManager().getPackageInfo("com.android.vending", 0);

    url = "market://details?id=" + my_package_name;
} catch ( final Exception e ) {
    url = "https://play.google.com/store/apps/details?id=" + my_package_name;
}


//Open the app page in Google Play store:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);

【讨论】:

  • 关于如何独立打开google play(不嵌入在同一个应用的新视图中)请查看我的回答。
【解决方案8】:

使用市场://

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));

【讨论】:

    【解决方案9】:

    由于 the official docs 使用 https:// 而不是 market://,这将 Eric 和 M3-n50 的答案与代码重用结合起来(不要重复自己):

    Intent intent = new Intent(Intent.ACTION_VIEW)
        .setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
    try {
        startActivity(new Intent(intent)
                      .setPackage("com.android.vending"));
    } catch (android.content.ActivityNotFoundException exception) {
        startActivity(intent);
    }
    

    如果 GPlay 应用存在,它会尝试使用它打开并回退到默认设置。

    【讨论】:

      【解决方案10】:

      科特林:

      扩展名:

      fun Activity.openAppInGooglePlay(){
      
      val appId = BuildConfig.APPLICATION_ID
      try {
          this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
      } catch (anfe: ActivityNotFoundException) {
          this.startActivity(
              Intent(
                  Intent.ACTION_VIEW,
                  Uri.parse("https://play.google.com/store/apps/details?id=$appId")
              )
          )
      }}
      

      方法:

          fun openAppInGooglePlay(activity:Activity){
      
              val appId = BuildConfig.APPLICATION_ID
              try {
                  activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
              } catch (anfe: ActivityNotFoundException) {
                  activity.startActivity(
                      Intent(
                          Intent.ACTION_VIEW,
                          Uri.parse("https://play.google.com/store/apps/details?id=$appId")
                      )
                  )
              }
          }
      

      【讨论】:

        【解决方案11】:

        你可以这样做:

        final Uri marketUri = Uri.parse("market://details?id=" + packageName);
        startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
        

        获取参考here:

        您还可以尝试此问题的已接受答案中描述的方法: Cannot determine whether Google play store is installed or not on Android device

        【讨论】:

        • 我已经尝试过使用此代码,这也显示了选择浏览器/播放商店的选项,因为我的设备已经安装了这两个应用程序(谷歌播放商店/浏览器)。
        • 关于如何独立打开google play(不嵌入在同一个应用的新视图中)请查看我的回答。
        【解决方案12】:

        晚会Official docs 来了。描述的代码是

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(
            "https://play.google.com/store/apps/details?id=com.example.android"));
        intent.setPackage("com.android.vending");
        startActivity(intent);
        

        在您配置此 Intent 时,将 "com.android.vending" 传递给 Intent.setPackage(),以便用户在 Google Play 商店应用而不是选择器中看到您应用的详细信息。 对于科特林

        val intent = Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse(
                    "https://play.google.com/store/apps/details?id=com.example.android")
            setPackage("com.android.vending")
        }
        startActivity(intent)
        

        如果您使用 Google Play Instant 发布了免安装应用,则可以按以下方式启动该应用:

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri.Builder uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
            .buildUpon()
            .appendQueryParameter("id", "com.example.android")
            .appendQueryParameter("launch", "true");
        
        // Optional parameters, such as referrer, are passed onto the launched
        // instant app. You can retrieve these parameters using
        // Activity.getIntent().getData().
        uriBuilder.appendQueryParameter("referrer", "exampleCampaignId");
        
        intent.setData(uriBuilder.build());
        intent.setPackage("com.android.vending");
        startActivity(intent);
        

        对于科特林

        val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
                .buildUpon()
                .appendQueryParameter("id", "com.example.android")
                .appendQueryParameter("launch", "true")
        
        // Optional parameters, such as referrer, are passed onto the launched
        // instant app. You can retrieve these parameters using Activity.intent.data.
        uriBuilder.appendQueryParameter("referrer", "exampleCampaignId")
        
        val intent = Intent(Intent.ACTION_VIEW).apply {
            data = uriBuilder.build()
            setPackage("com.android.vending")
        }
        startActivity(intent)
        

        【讨论】:

        • 我认为,这是错误的,至少,Uri.parse("https://play.google.com/store/apps/details?id=。在某些设备上,它会打开网络浏览器而不是 Play Market。
        • 所有代码均来自官方文档。此处还描述了答案代码中的链接以供快速参考。
        • @CoolMind 这样做的原因可能是因为这些设备具有旧版本的 Play 商店应用程序,它没有与该 URI 匹配的意图过滤器。
        • @tir38,也许是这样。可能他们没有 Google Play 服务或者没有获得授权,我不记得了。
        【解决方案13】:

        这个问题的一些答案已经过时了。

        根据this link,对我有用的是(在 2020 年)明确告诉意图跳过选择器并直接打开 Play 商店应用程序:

        “如果您想从 Android 应用链接到您的产品,请创建一个 打开 URL 的意图。当你配置这个意图时,通过 将“com.android.vending”放入 Intent.setPackage() 以便用户看到您的 在 Google Play 商店应用中而不是选择器中显示应用的详细信息。”

        这是我用来引导用户在 Google Play 中查看包含包名 com.google.android.apps.maps 的应用的 Kotlin 代码:

        val intent = Intent(Intent.ACTION_VIEW).apply {
                       data = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")
                       setPackage("com.android.vending")
                    }
                    startActivity(intent)
        

        希望对大家有所帮助!

        【讨论】:

          【解决方案14】:

          即用型解决方案:

          public class GoogleServicesUtils {
          
              public static void openAppInGooglePlay(Context context) {
                  final String appPackageName = context.getPackageName();
                  try {
                      context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                  } catch (android.content.ActivityNotFoundException e) { // if there is no Google Play on device
                      context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                  }
              }
          
          }
          

          根据 Eric 的回答。

          【讨论】:

          • 它对你有用吗?它会打开 Google Play 主页面,而不是我的应用页面。
          【解决方案15】:

          科特林

          fun openAppInPlayStore(appPackageName: String) {
              try {
                  startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
              } catch (exception: android.content.ActivityNotFoundException) {
                  startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
              }
          }
          

          【讨论】:

            【解决方案16】:

            如果您使用的是 Android,此链接将在 market:// 中自动打开应用程序,如果您使用的是 PC,则在浏览器中自动打开。

            https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
            

            【讨论】:

            • 实际上在我的任务中,有一个 webview,在 webview 中我必须加载任何 URL。但如果有打开任何Playstore url,它会显示打开Playstore 按钮。所以我需要在单击该按钮时打开应用程序。它对于任何应用程序都是动态的,那么我该如何管理?
            • 试试链接https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&amp;ddl=1&amp;pcampaignid=web_ddl_1
            【解决方案17】:

            如果您想从您的应用打开 Google Play 商店,请直接使用此命令:market://details?gotohome=com.yourAppName,它将打开您应用的 Google Play 商店页面。

            显示特定发布者的所有应用

            搜索在标题或描述上使用查询的应用

            参考:https://tricklio.com/market-details-gotohome-1/

            【讨论】:

              【解决方案18】:

              这是上面答案的最终代码,首先尝试使用 Google Play 商店应用程序打开应用程序,特别是 Play 商店,如果失败,它将使用网络版本启动操作视图: 感谢@Eric,@Jonathan Caballero

              public void goToPlayStore() {
                      String playStoreMarketUrl = "market://details?id=";
                      String playStoreWebUrl = "https://play.google.com/store/apps/details?id=";
                      String packageName = getActivity().getPackageName();
                      try {
                          Intent intent =  getActivity()
                                          .getPackageManager()
                                          .getLaunchIntentForPackage("com.android.vending");
                          if (intent != null) {
                              ComponentName androidComponent = new ComponentName("com.android.vending",
                                      "com.google.android.finsky.activities.LaunchUrlHandlerActivity");
                              intent.setComponent(androidComponent);
                              intent.setData(Uri.parse(playStoreMarketUrl + packageName));
                          } else {
                              intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreMarketUrl + packageName));
                          }
                          startActivity(intent);
                      } catch (ActivityNotFoundException e) {
                          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreWebUrl + packageName));
                          startActivity(intent);
                      }
                  }
              

              【讨论】:

                【解决方案19】:

                我结合了 BerťákStefano Munarini 答案来创建一个混合解决方案,该解决方案同时处理 评价此应用显示更多应用场景。

                        /**
                         * This method checks if GooglePlay is installed or not on the device and accordingly handle
                         * Intents to view for rate App or Publisher's Profile
                         *
                         * @param showPublisherProfile pass true if you want to open Publisher Page else pass false to open APp page
                         * @param publisherID          pass Dev ID if you have passed PublisherProfile true
                         */
                        public void openPlayStore(boolean showPublisherProfile, String publisherID) {
                
                            //Error Handling
                            if (publisherID == null || !publisherID.isEmpty()) {
                                publisherID = "";
                                //Log and continue
                                Log.w("openPlayStore Method", "publisherID is invalid");
                            }
                
                            Intent openPlayStoreIntent;
                            boolean isGooglePlayInstalled = false;
                
                            if (showPublisherProfile) {
                                //Open Publishers Profile on PlayStore
                                openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                                        Uri.parse("market://search?q=pub:" + publisherID));
                            } else {
                                //Open this App on PlayStore
                                openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                                        Uri.parse("market://details?id=" + getPackageName()));
                            }
                
                            // find all applications who can handle openPlayStoreIntent
                            final List<ResolveInfo> otherApps = getPackageManager()
                                    .queryIntentActivities(openPlayStoreIntent, 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
                                    );
                                    // make sure it does NOT open in the stack of your activity
                                    openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    // task reparenting if needed
                                    openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                    // if the Google Play was already open in a search result
                                    //  this make sure it still go to the app page you requested
                                    openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    // this make sure only the Google Play app is allowed to
                                    // intercept the intent
                                    openPlayStoreIntent.setComponent(componentName);
                                    startActivity(openPlayStoreIntent);
                                    isGooglePlayInstalled = true;
                                    break;
                
                                }
                            }
                            // if Google Play is not Installed on the device, open web browser
                            if (!isGooglePlayInstalled) {
                
                                Intent webIntent;
                                if (showPublisherProfile) {
                                    //Open Publishers Profile on web browser
                                    webIntent = new Intent(Intent.ACTION_VIEW,
                                            Uri.parse("http://play.google.com/store/search?q=pub:" + getPackageName()));
                                } else {
                                    //Open this App on web browser
                                    webIntent = new Intent(Intent.ACTION_VIEW,
                                            Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
                                }
                                startActivity(webIntent);
                            }
                        }
                

                用法

                • 打开发布商资料
                   @OnClick(R.id.ll_more_apps)
                        public void showMoreApps() {
                            openPlayStore(true, "Hitesh Sahu");
                        }
                
                • 在 PlayStore 上打开应用页面
                @OnClick(R.id.ll_rate_this_app)
                public void openAppInPlayStore() {
                    openPlayStore(false, "");
                }
                

                【讨论】:

                • 我建议将此代码分成更小的方法。在这个意大利面条中很难找到重要的代码 :) 另外,您正在检查 "com.android.vending" 那么 com.google.market
                【解决方案20】:
                public void launchPlayStore(Context context, String packageName) {
                    Intent intent = null;
                    try {
                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setData(Uri.parse("market://details?id=" + packageName));
                            context.startActivity(intent);
                        } catch (android.content.ActivityNotFoundException anfe) {
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
                        }
                    }
                

                【讨论】:

                  【解决方案21】:

                  为此目的我的 kotlin 扩展函数

                  fun Context.canPerformIntent(intent: Intent): Boolean {
                          val mgr = this.packageManager
                          val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
                          return list.size > 0
                      }
                  

                  在你的活动中

                  val uri = if (canPerformIntent(Intent(Intent.ACTION_VIEW, Uri.parse("market://")))) {
                              Uri.parse("market://details?id=" + appPackageName)
                          } else {
                              Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)
                          }
                          startActivity(Intent(Intent.ACTION_VIEW, uri))
                  

                  【讨论】:

                    【解决方案22】:

                    人们,不要忘记你实际上可以从中得到更多的东西。例如,我的意思是 UTM 跟踪。 https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

                    public static final String MODULE_ICON_PACK_FREE = "com.example.iconpack_free";
                    public static final String APP_STORE_URI =
                            "market://details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
                    public static final String APP_STORE_GENERIC_URI =
                            "https://play.google.com/store/apps/details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
                    
                    try {
                        startActivity(new Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse(String.format(Locale.US,
                                APP_STORE_URI,
                                MODULE_ICON_PACK_FREE,
                                getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse(String.format(Locale.US,
                                APP_STORE_GENERIC_URI,
                                MODULE_ICON_PACK_FREE,
                                getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                    }
                    

                    【讨论】:

                      【解决方案23】:

                      具有回退和当前语法的 kotlin 版本

                       fun openAppInPlayStore() {
                          val uri = Uri.parse("market://details?id=" + context.packageName)
                          val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)
                      
                          var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                          flags = if (Build.VERSION.SDK_INT >= 21) {
                              flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
                          } else {
                              flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
                          }
                      
                          goToMarketIntent.addFlags(flags)
                      
                          try {
                              startActivity(context, goToMarketIntent, null)
                          } catch (e: ActivityNotFoundException) {
                              val intent = Intent(Intent.ACTION_VIEW,
                                      Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))
                      
                              startActivity(context, intent, null)
                          }
                      }
                      

                      【讨论】:

                        【解决方案24】:

                        对于费率应用:重定向到 Playstore。 在 Flutter 中,你可以通过 Platform 通道来做到这一点

                        颤振部分:-

                         static const platform = const MethodChannel('rateApp');   // initialize 
                        

                        onTap: platform.invokeMethod('urls', {'android_id': 'com.xyz'}),

                        现在是Android Native Part(Java):

                        private static final String RATEAPP = "rateApp";  // initialize variable
                        

                        // 现在在 ConfigureFlutterEngine 函数中:

                                new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), RATEAPP)
                                    .setMethodCallHandler(
                                            (call, result) -> {               
                                                if (call.method.equals("urls") && call.hasArgument("android_id")) {
                                                    String id = call.argument("android_id").toString();
                                                  
                                                    try {
                                                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("$uri" + id)));
                                                    } catch (android.content.ActivityNotFoundException anfe) {
                                                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id)));
                                                    }
                                                    result.success("Done");
                                                } else {
                                                    result.notImplemented();
                                                }
                                            }
                                    );
                        

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 2017-03-08
                          • 1970-01-01
                          • 1970-01-01
                          • 2019-09-06
                          • 2014-03-11
                          • 2023-04-03
                          • 1970-01-01
                          相关资源
                          最近更新 更多