这个简单的代码将实现你想要什么,不需要外部库或任何幻想。只需将它放在主要活动的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
}
是否有可能在Play商店中的应用程序发布之前测试您的图书馆吗?或者它必须出现在商店才能显示弹出窗口? –
@StackDiego只需从GitHub项目中获取最新的JAR,并调用'demo()'而不是'show()':)感谢您的反馈! – caw
谢谢,我现在要尝试 –