【问题标题】:Prompt user to rate an Android app inside the App提示用户对应用内的 Android 应用进行评分
【发布时间】:2020-08-18 04:26:25
【问题描述】:

在我的 Android 应用中,我想在某个时间点提示用户对 Android 市场中的应用进行评分。

搜索方法后,我找到了一些代码on this website。这段代码似乎运行良好。

但不幸的是,当用户的手机上未安装 Android 市场时,此代码似乎会引发“强制关闭”错误消息。有什么方法可以检查是否安装了Android Market,如果没有,不要尝试执行代码?

引发错误的行可能是这一行,因为它无法解析 URI:

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

顺便说一句,该代码还有其他可以改进的地方吗?

编辑:

几年后,我将所有代码放入了一个小型库项目:AppRater on GitHub

【问题讨论】:

  • 是否可以在应用程序在 Playstore 上发布之前测试您的库?或者它必须出现在商店中才能显示弹出窗口?
  • @StackDiego 只需从 GitHub 项目获取最新的 JAR 并调用 demo() 而不是 show() :) 感谢您的反馈!
  • 谢谢,我马上去试试

标签: android user-input rating voting


【解决方案1】:

这是您需要的所有代码,(Kurt 的答案和推断信息的集合,加上链接和问题):

/* This code assumes you are inside an activity */
final Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
final Intent rateAppIntent = new Intent(Intent.ACTION_VIEW, uri);

if (getPackageManager().queryIntentActivities(rateAppIntent, 0).size() > 0)
{
    startActivity(rateAppIntent);
}
else
{
    /* handle your error case: the device has no way to handle market urls */
}

【讨论】:

  • 完美 :-) 喜欢这种方法。这将在应用程序在商店中发布时起作用
【解决方案2】:

您可以随时从PackageManager 类调用getInstalledPackages() 并检查以确保安装了市场类。您还可以使用queryIntentActivities() 来确保您构建的 Intent 能够被某些东西处理,即使它不是市场应用程序。实际上,这可能是最好的做法,因为它最灵活、最健壮。

【讨论】:

  • 谢谢!这正是我想要的:)
【解决方案3】:

您也可以使用 RateMeMaybe:https://github.com/Kopfgeldjaeger/RateMeMaybe

它为您提供了相当多的配置选项(最少天数/启动到第一个提示,最少天数/启动到每个下一个提示,如果用户选择“不是现在”,对话框标题,消息等)。它也很容易使用。

自述文件中的示例用法:

RateMeMaybe rmm = new RateMeMaybe(this);
rmm.setPromptMinimums(10, 14, 10, 30);
rmm.setDialogMessage("You really seem to like this app, "
                +"since you have already used it %totalLaunchCount% times! "
                +"It would be great if you took a moment to rate it.");
rmm.setDialogTitle("Rate this app");
rmm.setPositiveBtn("Yeeha!");
rmm.run();

【讨论】:

  • 假设如果我在我的第一个活动的 onCreate 中执行代码,那么它会向用户显示对话框吗?因为每次我在 14 天后调用 RateMeMaybe 的方法时都会显示对话框。对吗?
【解决方案4】:

首先需要统计应用的使用次数;

SharedPreferences preferences = getSharedPreferences("progress", MODE_PRIVATE);
int appUsedCount = preferences.getInt("appUsedCount",0);
appUsedCount++;
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("appUsedCount", appUsedCount);
editor.apply();

if (appUsedCount==10 || appUsedCount==50 || appUsedCount==100 || appUsedCount==200 || appUsedCount==300){
    AskForRating(appUsedCount);
} else {
    finish();
}

比你可以这样提示;

private void AskForRating(int _appUsedCount){

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Please Rate Us");
    alert.setIcon(R.drawable.book);
    alert.setMessage("Thanks for using the application. If you like YOUR APP NAME please rate us! Your feedback is important for us!");
    alert.setPositiveButton("Rate it",new Dialog.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton){
            String url = "https://play.google.com/store/apps/details?id=YOUR PACKAGE NAME";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });
    alert.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    alert.show();
}

【讨论】:

  • 这里的不同之处在于它会通过浏览器而不是使用 Play 商店应用程序将您带到网站。您应首先检查无法使用 Play 商店应用程序,然后使用浏览器访问网站。
【解决方案5】:

并非所有安卓设备都使用应用市场。 Kindle 和 Nook 有自己的市场,因此需要代码来检查市场是否存在是一个很好的选择。尽管应该有一种方法可以将评级发送到正确的市场,无论它是哪个市场。需要研究的东西。

【讨论】:

  • 这不是一个真正的问题,考虑到 Nook 和亚马逊市场有一个提交过程,这些链接总是被检查。您永远不会让应用程序通过与 Google 的费率链接在这两个市场中发布,因此强制关闭不会成为问题,您必须重做特定于市场的费率代码。
【解决方案6】:

这个简单的代码将实现你想要的,不需要外部库或任何花哨的东西。只需将其放在主要活动的 OnCreate 事件中即可。变量 RunEvery 将确定速率消息出现的频率。在示例中,它设置为 10。

// Count times app has been opened, display rating message after number of times  
// By Rafael Duval   
    try {

        // Get the app's shared preferences
        SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

        // Get the value for the run counter
        int counter = app_preferences.getInt("counter", 0);

        // Do every x times
        int RunEvery = 10;

        if(counter != 0  && counter % RunEvery == 0 )
        {
            //Toast.makeText(this, "This app has been started " + counter + " times.", Toast.LENGTH_SHORT).show();

           AlertDialog.Builder alert = new AlertDialog.Builder(
                     MyActivity.this);
                   alert.setTitle("Please rate");
                   alert.setIcon(R.drawable.ic_launcher); //app icon here
                   alert.setMessage("Thanks for using this free app. Please take a moment to rate it.");

                   alert.setPositiveButton("Cancel",
                     new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog,
                        int whichButton) {                            
                          //Do nothing
                      }   
                     });

                   alert.setNegativeButton("Rate it",
                     new DialogInterface.OnClickListener() {

                      public void onClick(DialogInterface dialog, int which) {   

                           final String appName = getApplicationContext().getPackageName();
                           try {
                            startActivity(new Intent(Intent.ACTION_VIEW,
                              Uri.parse("market://details?id="
                                + appName)));
                           } catch (android.content.ActivityNotFoundException anfe) {
                            startActivity(new Intent(
                              Intent.ACTION_VIEW,
                              Uri.parse("http://play.google.com/store/apps/details?id="
                                + appName)));
                           }   

                      }
                     });
                   alert.show();            
        }


        // Increment the counter
        SharedPreferences.Editor editor = app_preferences.edit();
        editor.putInt("counter", ++counter);
        editor.commit(); // Very important          

    } catch (Exception e) {
        //Do nothing, don't run but don't break
    }           

【讨论】:

    【解决方案7】:

    如果应用程序是通过 Android Market 下载的,用户将在手机上安装 Android Market,所以我真的不认为这是一个问题。好像很奇怪……

    您可以使用以下内容在您的应用程序页面上启动 Android Market,它更加自动化:

    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse("market://details?id=" + getPackageName()));
    startActivity(i);
    

    【讨论】:

    • 您发布的代码有效,但上面链接中显示的代码更短 - 只需一行即可开始意图。并且:当然,用户有可能没有 Android 市场应用程序:他们通过 Android 市场下载我的应用程序,然后(有时)从他们的手机中删除 Android 市场应用程序;)
    • 他们到底为什么要这样做?这似乎非常愚蠢。绝对不是您作为开发人员应该关心的事情。但如果你坚持,你可以按照 Kurtis 的建议去做。检查应用程序是否已安装。您还可以执行以下操作:stackoverflow.com/questions/4439043/… 并检查它是否正在返回应用程序。
    • 谢谢。这可能是“愚蠢的”,但还是有可能的。 getPackageName() 方法是避免对包名称进行硬编码的好方法。但由于该类(见链接)在一个外部类中,我不能使用它。
    【解决方案8】:

    当我使用 "market://details?id=" + getApplicationContext().getPackageName() 时,它会在我身上打开 mobogenie 市场,所以我更喜欢使用 https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName()

    【讨论】:

    • 这只是意味着您的替代市场表示它也可以处理market:// URL。然后你可能选择了这个应用作为你手机上的默认应用。
    【解决方案9】:

    使用此代码

    Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
    // To count with Play market backstack, After pressing back button, 
    // to taken back to our application, we need to add following flags to intent. 
    goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                    Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    try {
        startActivity(goToMarket);
    } catch (ActivityNotFoundException e) {
        startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
    }
    

    【讨论】:

      【解决方案10】:

      现在还有其他选择。这是一个比较,可以帮助您选择使用哪个。

      https://polljoy.com/blog/irate-vs-appirater-open-source-rating-prompts-alternatives

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 2021-01-11
        • 1970-01-01
        相关资源
        最近更新 更多