Google Play商店手机应用程序中的“评价此应用程序”链接

298

我想在Android应用程序中放置一个“评价此应用程序”的链接,以打开用户手机上的Google Play商店应用程序中的应用程序清单。

  1. 我需要编写什么代码才能创建market://http://链接并在手机上的Google Play商店应用程序中打开?
  2. 您将代码放在哪里?
  3. 有人有这个的示例实现吗?
  4. 您是否必须指定market://http://链接将放置的屏幕,哪个最好使用-market://还是http://

这里有你需要的一切:https://github.com/delight-im/AppRater 你可以查看源代码来了解它是如何实现的。 - caw
请查看2020年的官方实现方式 - iDecode
现在最好的方法是使用Google的应用内评论 - https://dev59.com/Umgv5IYBdhLWcg3wJNdF#65839978 - Khemraj Sharma
我收到一条短信,里面有我的应用程序的Google Play商店链接。如果该应用程序在我的设备上可用,它需要将我重定向到该应用程序。否则,它需要打开Google Play商店。这种情况是否可能? - undefined
21个回答

597

我使用以下代码从我的应用程序中打开 Play Store:

            val uri: Uri = Uri.parse("market://details?id=$packageName")
            val goToMarket = 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 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=$packageName")))
            }

选项2: 使用resolveActivity而不是try..catch

if (sendIntent.resolveActivity(getPackageManager()) != null) {
     startActivity(chooser);
} else {
    openUrl();
}

2
我需要将这段代码放在androidmanifest.xml的哪个位置?我需要添加其他内容吗?这与用户按下屏幕上的实际链接或按钮有何关联?谢谢。 - Adreno
1
您不需要将任何代码添加到清单中。您只需将此代码放置在按钮/链接的OnClickListener中,这样当单击按钮时,代码将被执行并启动Play商店。 - MRD
67
该解决方案不考虑Play市场的返回堆栈。按下返回按钮后,您不会回到您的应用程序中。如果需要,请添加以下代码行:intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); - Jan Muller
26
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET:此常量已在API级别21中弃用。从API 21开始,它与FLAG_ACTIVITY_NEW_DOCUMENT的表现完全一致,因此应使用FLAG_ACTIVITY_NEW_DOCUMENT代替此常量。 - xnagyg
2
如果从非Activity的Java类中调用,则需要像context.startActivity(goToMarket);一样传递上下文。 - DMur
显示剩余11条评论

57

这里有一份运作良好且最新的代码 :)

/*
* Start with rating the app
* Determine if the Play Store is installed on the device
*
* */
public void rateApp()
{
    try
    {
        Intent rateIntent = rateIntentForUrl("market://details");
        startActivity(rateIntent);
    }
    catch (ActivityNotFoundException e)
    {
        Intent rateIntent = rateIntentForUrl("https://play.google.com/store/apps/details");
        startActivity(rateIntent);
    }
}

private Intent rateIntentForUrl(String url)
{
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("%s?id=%s", url, getPackageName())));
    int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
    if (Build.VERSION.SDK_INT >= 21)
    {
        flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
    }
    else
    {
        //noinspection deprecation
        flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
    }
    intent.addFlags(flags);
    return intent;
}

将代码放在您想要从中调用它的Activity中。
当用户点击按钮评价应用程序时,只需调用rateApp()函数即可。


我应该添加哪个NuGet包,使用什么命名空间才能使“Intent”成为可用类型? 我找到了Android.Content,但在Xamarin Forms中对“Intent”感到困惑。 - s3c

33

我经常使用这段代码:

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

7
喜欢简洁明了的话语。:) - androidStud
我使用它,但是它显示了这个错误- android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=PackageName }- 我该怎么办? - Mina Dahesh
你能检查一下这个吗? - Cabezas
@Cabezas。通常我想在手机上显示所有现有的市场。通过点击它们中的任何一个,如果我的应用程序存在,则市场会显示该应用程序。那么我应该怎么做? - Mina Dahesh
1
@Cabezas。我使用这段代码:try { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("bazaar://details?id=vow_note.maxsoft.com.vownote")); intent.setData(Uri.parse("myket://comment?id=vow_note.maxsoft.com.vownote")); startActivity(intent); }catch (ActivityNotFoundException e1) { try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("MARKET URL"))); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("MARKET URL"))); } catch (ActivityNotFoundException e2) {Toast.} - Mina Dahesh
显示剩余5条评论

32

Kotlin解决方案(2020年Google提供的应用内评论API):

现在,您可以直接使用由Google提供的应用内评论API。

首先,在您的build.gradle(app)文件中,添加以下依赖项(完整设置可以在此处找到)。

dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:core:1.8.0'
    implementation 'com.google.android.play:core-ktx:1.8.1'
}
创建一个方法并将此代码放入其中:
val manager = ReviewManagerFactory.create(context)
val request = manager.requestReviewFlow()
request.addOnCompleteListener { request ->
    if (request.isSuccessful) {
        // We got the ReviewInfo object
        val reviewInfo = request.result
        val flow = manager.launchReviewFlow(activity, reviewInfo)
        flow.addOnCompleteListener { _ ->
          // The flow has finished. The API does not indicate whether the user
          // reviewed or not, or even whether the review dialog was shown. Thus, no
         // matter the result, we continue our app flow.
        }
    } else {
        // There was some problem, continue regardless of the result.
    }
}

来源

这里输入图片描述


3
如果您正在使用 Kotlin 依赖项,则不需要使用此项: implementation 'com.google.android.play:core:1.8.0' - dansamosudov
@dansamosudov,我很高兴它在没有核心库的情况下工作了,尽管我没有收到你的点赞 :( - iDecode
请谨慎使用,应用内评论可能无法显示,因为有许多原因(例如:旧版Playstore、企业电子邮件、Google Play服务问题)。https://issuetracker.google.com/issues/167352813 - Linh
1
如果您希望用户在点击按钮后进行应用程序评论,那么您不应该使用In-App Review,因为它可能每个月只显示一次。 - Linh
@Linh,你可以在上面答案的else条件中重定向到playstore作为备选方案。 - Anandroid
@Anandroid 不行,这样做不起作用。根据此答案中代码示例的注释:“API不会指示用户是否已经进行了评论,甚至不会指示评论对话框是否已显示。”因此,当从按钮触发时,您必须始终显示商店页面,而不是使用此API。 - Søren Boisen

22

如果您在Google Play商店和Amazon应用商店都发布了您的应用程序,则需要考虑以下情况。我还处理用户(尤其是中国用户)没有安装应用商店和浏览器的情况。

public void goToMyApp(boolean googlePlay) {//true if Google Play, false if Amazone Store
    try {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse((googlePlay ? "market://details?id=" : "amzn://apps/android?p=") +getPackageName())));
    } catch (ActivityNotFoundException e1) {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse((googlePlay ? "http://play.google.com/store/apps/details?id=" : "http://www.amazon.com/gp/mas/dl/android?p=") +getPackageName())));
        } catch (ActivityNotFoundException e2) {
            Toast.makeText(this, "You don't have any app that can open this link", Toast.LENGTH_SHORT).show();
        }
    }
}

不回答手头的问题。 - user4652595
你想要打开你的应用在亚马逊应用商店的页面,需要怎样的代码呢? - isJulian00
我应该添加哪个NuGet包,以及为了让Intent成为可行的类型,我应该使用什么命名空间? 我找到了Android.Content,但在Xamarin Forms中,我对Intent感到困惑。 - s3c
我收到了一条短信,里面包含了我的应用程序在Google Play商店的链接。如果这个应用程序在我的设备上可用,它应该将我重定向到该应用程序。否则,它应该打开Google Play商店。能否通过Google Play商店的链接将我重定向到该应用程序呢? - undefined

11

您可以从PackageManager类中始终调用getInstalledPackages(),并检查市场类是否已安装。您还可以使用queryIntentActivities()来确保您构建的Intent将能够被处理,即使它不是市场应用程序。实际上,这可能是最好的做法,因为它最灵活、最强大。

您可以通过以下方式检查市场应用程序是否存在

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://search?q=foo"));
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);

如果列表至少有一个条目,则该市场存在。
您可以使用以下内容在应用程序页面上启动Android Market,这是更自动化的方式:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=" + getPackageName()));
startActivity(i);

如果您想在模拟器上测试此功能,可能您的模拟器上没有安装市场: 请参阅以下链接获取更多详细信息: 如何在Google Android模拟器中启用Android市场 在Android模拟器上安装Google Play

我需要将这段代码放在androidmanifest.xml的哪个位置?我需要添加其他内容吗?这与用户按下屏幕上的实际链接或按钮有何关联?谢谢。 - Adreno

8
我使用这种方法让用户评价我的应用程序:
public static void showRateDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle("Rate application")
            .setMessage("Please, rate the app at PlayMarket")
            .setPositiveButton("RATE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (context != null) {
                        String link = "market://details?id=";
                        try {
                            // play market available
                            context.getPackageManager()
                                    .getPackageInfo("com.android.vending", 0);
                        // not available
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                            // should use browser
                            link = "https://play.google.com/store/apps/details?id=";
                        }
                        // starts external action
                        context.startActivity(new Intent(Intent.ACTION_VIEW, 
                                Uri.parse(link + context.getPackageName())));
                    }
                }
            })
            .setNegativeButton("CANCEL", null);
    builder.show();
}

这是干什么用的?- market://details?id= 我的应用链接类似于 https:\\play.google.com\apps\details?id= - Sagar Balyan
2
@SagarBalyan,这是一个特殊的URI,用于在Google Play市场应用程序中打开您的应用程序页面。如果您使用提供的链接启动活动,则Android将在默认浏览器中打开您的应用程序页面,或者让您选择要启动的浏览器应用程序。 - gtgray

7
一份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
    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)
    }
}

第一个链接 Uri.parse("market://details?id=" + context.packageName) 和 catch 块中的第二个链接 Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName)) 有什么不同吗?我认为这是多余的。 - Dr Mido
首先是Play商店应用程序的方案。因此,该应用程序尝试打开Play商店应用程序。如果由于某些原因无法工作,则将调用http链接。在早期,这种回退甚至更加必要。我认为到2022年,99%的用户应该能够通过Scheme打开Play商店应用程序。 - kuzdu

7

Java解决方案(2020年Google应用内评论API):

现在您可以直接使用Google提供的应用内评论API。

首先,在您的build.gradle(app)文件中,添加以下依赖项(完整设置可在此处找到)

dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:core:1.8.0'
}

将此方法添加到您的Activity中:
void askRatings() {
    ReviewManager manager = ReviewManagerFactory.create(this);
    Task<ReviewInfo> request = manager.requestReviewFlow();
    request.addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            // We can get the ReviewInfo object
            ReviewInfo reviewInfo = task.getResult();
            Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
            flow.addOnCompleteListener(task2 -> {
                // The flow has finished. The API does not indicate whether the user
                // reviewed or not, or even whether the review dialog was shown. Thus, no
                // matter the result, we continue our app flow.
            });
        } else {
            // There was some problem, continue regardless of the result.
        }
    });
}

然后你可以简单地使用它进行调用

askRatings();

来源

enter image description here


使用实现方式?这会在我们的项目中添加另外1MB的库吗?@iDecode....天哪 - gumuruh
它肯定会增加您的应用程序大小,但我认为不会超过几个KB(尽管我没有检查过)。 - iDecode

6

应用商店评分

 btn_rate_us.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("market://details?id=" + 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=" + getPackageName())));
                }
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接