Skip to content

Instantly share code, notes, and snippets.

@Phocacius
Last active October 29, 2016 11:23
Show Gist options
  • Save Phocacius/f75dec1f216fe8038ca3 to your computer and use it in GitHub Desktop.
Save Phocacius/f75dec1f216fe8038ca3 to your computer and use it in GitHub Desktop.
Simple Android Java class that allows you to determine whether or not to show a "rate my app"-dialog and allows you to show the dialog that takes the user to the Play Store when the dialog is confirmed.
public class RateAppManager {
private static final String MS_PREFS = Prefs.class.getSimpleName();
private static SharedPreferences preferences;
private static final String APP_START_COUNT = "appStartCount";
private static final String APP_INSTALL_TIMESTAMP = "appInstallTimestamp";
private static final String RATE_DIALOG_SHOWN = "rateDialogShown";
private static SharedPreferences getPreferences() {
if(preferences == null) {
preferences = MyApplication.getAppContext().getSharedPreferences(MS_PREFS, Context.MODE_PRIVATE);
}
return preferences;
}
// should be called in the SplashScreen- or MainActivity
public static boolean shouldShowRateDialog() {
SharedPreferences prefs = getPreferences();
// dialog already shown and dismissed?
if(prefs.getBoolean(RATE_DIALOG_SHOWN, false)) {
return false;
}
int starts = prefs.getInt(APP_START_COUNT, 0);
Calendar now = Calendar.getInstance();
// app started for the first time, save start date
if (starts == 0) {
prefs.edit()
.putLong(APP_INSTALL_TIMESTAMP, now.getTimeInMillis())
.putInt(APP_START_COUNT, 1)
.apply();
return false;
}
// only show dialog after 10 starts ...
if(starts < 10) {
prefs.edit()
.putInt(APP_START_COUNT, ++starts)
.apply();
return false;
}
// ...and if the app is installed for more than 3 days
return now.getTimeInMillis() - prefs.getLong(APP_INSTALL_TIMESTAMP, 0) >= 1000 * 3600 * 24 * 3;
}
public static void showAppRateDialog(final Context context) {
// context has to be an activity context, not the application context!
// styled
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));
// default style
// AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getString(R.string.rate_app_title));
builder.setMessage(context.getString(R.string.rate_app_message));
builder.setNegativeButton(context.getString(R.string.rate_app_maybe_later), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppRateManager.postponeRateDialog();
dialog.dismiss();
}
});
builder.setNeutralButton(R.string.rate_app_no_thanks, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppRateManager.rateDialogShown();
dialog.dismiss();
}
});
builder.setPositiveButton(R.string.rate_app_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppRateManager.rateDialogShown();
dialog.dismiss();
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
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())));
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private static void rateDialogShown() {
getPreferences().edit().putBoolean(RATE_DIALOG_SHOWN, true).apply();
}
private static void postponeRateDialog() {
// show again in two days by setting the install date to one day ago
Calendar now = Calendar.getInstance();
long oneDayAgo = now.getTimeInMillis() - 1000*3600*24;
getPreferences().edit().putLong(APP_INSTALL_TIMESTAMP, oneDayAgo).apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment