Answers:
我使用以下代码从我的应用打开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())));
}
这将启动Play商店,并打开您的“应用”页面。用户可以在那里对其进行评分。
这是一个有效的最新代码:)
/*
* 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()
函数即可。
我总是使用以下代码:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
这是如果您同时在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();
}
}
}
您始终可以从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);
如果要在模拟器上进行测试,则可能没有安装市场:有关更多详细信息,请参见以下链接:
我使用这种方法来使用户对我的应用程序进行评分:
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=
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)
}
}
你可以用这个,对我有用
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();
}
播放商店评分
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())));
}
}
});
另一种可能对您有用的方法是Linkify。如果我有一个TextView要求用户对应用程序进行评分,则可以链接文本中的几个单词,使其突出显示,并且当用户触摸它们时,Play商店会打开,以供他们查看:
class playTransformFilter implements TransformFilter {
public String transformUrl(Matcher match, String url) {
return "market://details?id=com.qwertyasd.yourapp";
}
}
class playMatchFilter implements MatchFilter {
public boolean acceptMatch(CharSequence s, int start, int end) {
return true;
}
}
text1 = (TextView) findViewById(R.id.text1);
text1.setText("Please rate it.");
final Pattern playMatcher = Pattern.compile("rate it");
Linkify.addLinks(text1, playMatcher, "",
new playMatchFilter(), new playTransformFilter());
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.StringRes;
import android.widget.Toast;
public class PlayStoreLink {
public void checkForUpdate(Context context, int applicationId)
{
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(context.getString(R.string.url_market_details)
+ applicationId)));
} catch (android.content.ActivityNotFoundException anfe) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(context.getString(R.string.url_playstore_app)
+ applicationId)));
} catch (Exception e) {
Toast.makeText(context,
R.string.install_google_play_store,
Toast.LENGTH_SHORT).show();
}
}
}
public void moreApps(Context context, @StringRes int devName) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(context.getString(R.string.url_market_search_app)
+ context.getString(devName))));
} catch (android.content.ActivityNotFoundException anfe) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(context.getString(R.string.url_playstore_search_app)
+ context.getString(devName))));
} catch (Exception e) {
Toast.makeText(context,
R.string.install_google_play_store,
Toast.LENGTH_SHORT).show();
}
}
}
public void rateApp(Context context, int applicationId) {
try {
Uri uri = Uri.parse(context.getString(R.string.url_market_details)
+ applicationId);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH)
flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
else
flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
intent.addFlags(flags);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
checkForUpdate(context, applicationId);
}
}
}
<string name="install_google_play_store" translatable="false">Please install google play store and then try again.</string>
<string name="url_market_details" translatable="false">market://details?id=</string>
<string name="url_playstore_app" translatable="false">https://play.google.com/store/apps/details?id=</string>
<string name="url_market_search_app" translatable="false">market://search?q=pub:</string>
<string name="url_playstore_search_app" translatable="false">http://play.google.com/store/search?q=pub:</string>
<string name="app_link" translatable="false">https://play.google.com/store/apps/details?id=</string>
devName是Play商店上开发者帐户的名称
您可以使用此简单代码在活动中对应用程序进行评分。
try {
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
}
market://details?id=
我的应用程式连结就像https:\\play.google.com\apps\details?id=
我结合使用以下方法 这个和这个答案不使用基于异常的编程,同时还支持预API 21意图标志。
@SuppressWarnings("deprecation")
private Intent getRateIntent()
{
String url = isMarketAppInstalled() ? "market://details" : "https://play.google.com/store/apps/details";
Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("%s?id=%s", url, getPackageName())));
int intentFlags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
intentFlags |= Build.VERSION.SDK_INT >= 21 ? Intent.FLAG_ACTIVITY_NEW_DOCUMENT : Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
rateIntent.addFlags(intentFlags);
return rateIntent;
}
private boolean isMarketAppInstalled()
{
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=anyText"));
return getPackageManager().queryIntentActivities(marketIntent, 0).size() > 0;
}
// use
startActivity(getRateIntent());
由于intent标志FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
已从API 21中弃用,@SuppressWarnings("deprecation")
因此我在getRateIntent方法上使用标记,因为我的应用程序目标SDK低于API 21。
我还尝试了在其网站上建议的Google 官方 方式(2019年12月6日)。依我所见,如果未安装Play Store应用程序,将无法解决问题:
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);
在活动类中声明一个方法。然后复制并粘贴下面的代码。
private void OpenAppInPlayStore(){
Uri uri = Uri.parse("market://details?id=" + this.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=" + this.getPackageName())));
}
}
现在,从代码的任何位置调用此方法。
请遵循下面来自我的实际项目的图像。