Android “评价此应用”-手机上 Google Play 商店应用中的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10816757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Rate This App"-link in Google Play store app on the phone
提问by Adreno
I'd like to put a "Rate This App"-link in an Android App to open up the app-listing in the user's Google Play store app on their phone.
我想在 Android 应用程序中放置一个“评价此应用程序”链接,以在用户的手机上的 Google Play 商店应用程序中打开应用程序列表。
- What code do I have to write to create the
market://
orhttp://
-link open in the Google Play store app on the phone? - Where do you put the code?
- Does anyone have a sample implementation of this?
- Do you have to specify the screen where the
market://
orhttp://
link will be placed, and which is the best to use -market://
orhttp://
?
- 我必须编写什么代码才能在手机上的 Google Play 商店应用中创建
market://
orhttp://
链接打开? - 你把代码放在哪里?
- 有没有人有这个的示例实现?
- 您是否必须指定将放置
market://
或http://
链接的屏幕,以及最好使用的屏幕-market://
还是http://
?
回答by miguel.rodelas
I open the Play Store from my App with the following code:
我使用以下代码从我的应用程序打开 Play 商店:
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())));
}
This will launch the Play Store with your App page already opened. The user can rate it there.
这将在您的应用程序页面已打开的情况下启动 Play 商店。用户可以在那里对其进行评分。
回答by Gy?rgy Benedek
Here is a working and up to date code :)
这是一个有效且最新的代码:)
/*
* 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;
}
Put the code in the Activity
you would like to call it from.
When the user clicks a button to rate the app, just call the rateApp()
function.
把代码放在Activity
你想调用它的地方。
当用户单击按钮对应用程序进行评分时,只需调用该rateApp()
函数即可。
回答by Cabezas
I always use this code:
我总是使用这个代码:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
回答by H?i Phong
This is if you publish your app in both Google Play Store and Amazon Appstore. I also handle the case that users (especially in China) don't have both app store and browser.
如果您在 Google Play 商店和 Amazon Appstore 中发布您的应用程序。我还处理用户(特别是在中国)没有应用商店和浏览器的情况。
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();
}
}
}
回答by K_Anas
You can always call getInstalledPackages()from the PackageManagerclass and check to make sure the market class is installed. You could also use queryIntentActivities()to make sure that the Intent you construct will be able to be handled by something, even if it's not the market application. This is probably the best thing to do actually because its the most flexible and robust.
您始终可以从PackageManager类调用getInstalledPackages()并检查以确保市场类已安装。您还可以使用queryIntentActivities()来确保您构建的 Intent 能够被某些东西处理,即使它不是市场应用程序。这实际上可能是最好的做法,因为它最灵活和强大。
You can check if the market app is there by
您可以通过以下方式检查市场应用程序是否在那里
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);
If the list has at least one entry, the Market's there.
如果列表至少有一个条目,则市场就在那里。
You can use the following to launch Android Market on your application's page, it's a bit more automated:
您可以使用以下命令在您的应用程序页面上启动 Android Market,它的自动化程度更高:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=" + getPackageName()));
startActivity(i);
If you want to test this on your emulator you probably you don't have the market installed on it : see these links for more details:
如果你想在你的模拟器上测试这个,你可能没有安装市场:有关更多详细信息,请参阅这些链接:
How To Enable the Android Market in the Google Android Emulator
如何在 Google Android Emulator 中启用 Android Market
回答by gtgray
I use this approach to make user rate my apps:
我使用这种方法让用户评价我的应用程序:
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();
}
回答by Suman
You can use this, it works for me
你可以用这个,它对我有用
public static void showRateDialogForRate(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) {
////////////////////////////////
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_CLEAR_WHEN_TASK_RESET |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
context.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
}
}
})
.setNegativeButton("CANCEL", null);
builder.show();
}
回答by Keshav Gera
Play Store Rating
Play 商店评分
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())));
}
}
});
回答by kuzdu
A kotlin version
一个 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)
}
}
回答by hecht
A point regarding all the answers that have implementations based on the getPackageName() strategy is that using BuildConfig.APPLICATION_ID may be more straight forward and works well if you use the same code base to build multiple apps with different app ids (for example, a white label product).
关于所有基于 getPackageName() 策略实现的答案的一点是,如果您使用相同的代码库构建具有不同应用程序 ID 的多个应用程序(例如,一个白标产品)。