我想把一个“Rate This App”-链接在一个Android应用程序打开应用程序列表在用户的谷歌播放商店应用程序在他们的手机。
我需要写什么代码来创建市场://或http://-link在手机上的谷歌Play商店应用程序中打开? 你把代码放在哪里? 有人有这个的示例实现吗? 你是否必须指定market://或http:// link将被放置的屏幕,并且最好使用哪个- market://或http://?
我想把一个“Rate This App”-链接在一个Android应用程序打开应用程序列表在用户的谷歌播放商店应用程序在他们的手机。
我需要写什么代码来创建市场://或http://-link在手机上的谷歌Play商店应用程序中打开? 你把代码放在哪里? 有人有这个的示例实现吗? 你是否必须指定market://或http:// link将被放置的屏幕,并且最好使用哪个- market://或http://?
当前回答
Kotlin解决方案(2020年谷歌发布应用程序内评论API):
您现在可以使用谷歌提供的应用程序审查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.
}
}
源
其他回答
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 Store上开发者帐户的名称
我用以下代码从我的应用程序打开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();
}
Kotlin解决方案(2020年谷歌发布应用程序内评论API):
您现在可以使用谷歌提供的应用程序审查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.
}
}
源
您总是可以从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);
如果你想在模拟器上测试这个,你可能没有安装市场: 详情请参见以下链接:
如何在谷歌Android模拟器中开启Android市场
在Android模拟器上安装谷歌播放
另一个可能对你有用的方法是Linkify。如果我有一个TextView,是要求用户评价应用程序,我可以链接两个词在文本中,使他们突出显示,当用户触摸他们,播放商店打开,准备他们的审查:
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());