在我的Android应用中,我想在某个时间点提示用户在Android市场中为应用评分。
搜索了一种方法后,我发现了一些代码on this website。这段代码看起来效果很好。
但遗憾的是,当Android市场未安装在用户的手机上时,此代码似乎会引发“强制关闭”错误消息。有没有办法检查Android市场是否已安装,如果没有,请不要尝试执行代码?
引发错误的行可能就是这一行,因为它无法解析URI:
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
顺便说一下,那个代码还有其他可以改进的东西吗?
修改
几年后,我将所有代码放入一个小型库项目中:AppRater on GitHub
答案 0 :(得分:36)
以下是您需要的所有代码,(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 */
}
答案 1 :(得分:14)
您始终可以从getInstalledPackages()课程中拨打PackageManager并检查以确保已安装市场类。您还可以使用queryIntentActivities()来确保您构建的Intent能够被某些东西处理,即使它不是市场应用程序。这可能是最好的事情,因为它最灵活和最强大。
答案 2 :(得分:9)
您也可以使用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();
答案 3 :(得分:5)
首先,您需要计算应用程序的使用次数;
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();
}
答案 4 :(得分:1)
并非所有Android设备都使用应用市场。 Kindle和Nook有自己的市场,所以需要代码来检查市场是否存在是一个好的。虽然应该有一种方法可以将评级发送到正确的市场,无论它是哪一个。需要注意的事情。
答案 5 :(得分:1)
这个简单的代码将实现您想要的,不需要外部库或任何花哨的东西。只需将其放在主要活动的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
}
答案 6 :(得分:0)
如果应用程序是通过Android Market下载的,那么用户将在手机上安装Android Market,所以我真的不认为这是一个问题。这看起来很奇怪......
您可以使用以下命令在应用页面上启动Android电子市场,它会更加自动化:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=" + getPackageName()));
startActivity(i);
答案 7 :(得分:0)
当我使用" market:// details?id =" + getApplicationContext()。getPackageName()它打开了我的mobogenie市场,所以我更喜欢使用https://play.google.com/store/apps/details?id=" + getApplicationContext()。getPackageName()
答案 8 :(得分:0)
使用此代码
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())));
}
答案 9 :(得分:-2)
现在还有其他选择。这是一个比较,可以帮助您选择使用哪一个。
https://polljoy.com/blog/irate-vs-appirater-open-source-rating-prompts-alternatives