這個簡單的代碼將實現你想要什麼,不需要外部庫或任何幻想。只需將它放在主要活動的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
謝謝,我現在要嘗試 –