Delayed Rate/Review Prompt for Android Market

Quickly share your Android Code Snippets here...

Delayed Rate/Review Prompt for Android Market

Postby hooked82 » Sat Aug 20, 2011 10:15 am

I was looking at a way to get better rate/review numbers on my apps and wanted to put something into them to give the user a quick way to get to the Market to rate the app. I came across this example, but it didn't fully accomplish what I was looking for:
http://www.androidsnippets.com/prompt-e ... -appirater

What the Code does
Allows you to display a dialog box after the user has opened the app a certain amount of time within a certain timeframe. They will then be presented with options to rate your app, remind them later, or dismiss the dialog saying no thanks or they already rated your app

- In the sample below, after a week of use and opening the app up at least 10 times, the dialog appears to get their review/rating.
- Clicking "No thanks/Already have" will dismiss the box and never show it again
- Clicking "Remind me later" will dismiss the dialog and display it again after 15 days from this point and after opening the app 15 more times
- Clicking "Rate YOUR_APP" the user will be taken to the Market to your app. This will start up a new cycle to show the dialog again in after 30 days and after opening the app 25 more times. This is due to the fact that there is no way to ensure that they did rate/review your app. At this point, they can then click the "No thanks/Already have" button and dismiss the rating dialog forever

RatingHelper Class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class RatingHelper {
  2.  
  3.     private final static String RATE_DONTSHOW = "rate_dontshowagain";
  4.     private final static String RATE_REMIND = "rate_remindlater";
  5.     private final static String RATE_CLICKEDRATE = "rate_clickedrated";
  6.     private final static String COUNT_APPLAUNCH = "app_launch_count";
  7.     private final static String COUNT_REMINDLAUNCH = "remind_launch_count";
  8.     private final static String COUNT_RATEDLAUNCH = "rated_launch_count";
  9.     private final static String DATE_FIRSTLAUNCH = "app_first_launch";
  10.     private final static String DATE_REMINDSTART = "remind_start_date";
  11.     private final static String DATE_RATEDSTART = "rated_start_date";
  12.    
  13.     private final static int DAYS_FIRSTPROMPT = 7;
  14.     private final static int DAYS_REMINDPROMPT = 15;
  15.     private final static int DAYS_RATEDPROMPT = 30;
  16.    
  17.     private final static int LAUNCHES_FIRSTPROMPT = 10;
  18.     private final static int LAUNCHES_REMIND = 15;
  19.     private final static int LAUNCHES_RATED = 25;
  20.    
  21.     private static void clearFirstLaunchPrefs(SharedPreferences.Editor editor) {
  22.         editor.remove(COUNT_APPLAUNCH);
  23.         editor.remove(DATE_FIRSTLAUNCH);
  24.         editor.commit();
  25.     }
  26.    
  27.     private static void clearRemindPrefs(SharedPreferences.Editor editor) {
  28.         editor.remove(RATE_REMIND);
  29.         editor.remove(COUNT_REMINDLAUNCH);
  30.         editor.remove(DATE_REMINDSTART);
  31.         editor.commit();
  32.     }
  33.    
  34.     private static void clearRatedPrefs(SharedPreferences.Editor editor) {
  35.         editor.remove(RATE_CLICKEDRATE);
  36.         editor.remove(COUNT_RATEDLAUNCH);
  37.         editor.remove(DATE_RATEDSTART);
  38.         editor.commit();
  39.     }
  40.    
  41.     private static void addRemindPrefs(SharedPreferences.Editor editor) {
  42.         editor.putBoolean(RATE_REMIND, true);
  43.         editor.putLong(DATE_REMINDSTART, System.currentTimeMillis());
  44.         editor.commit();
  45.     }
  46.    
  47.     private static void addRatedPrefs(SharedPreferences.Editor editor) {
  48.         editor.putBoolean(RATE_CLICKEDRATE, true);
  49.         editor.putLong(DATE_RATEDSTART, System.currentTimeMillis());
  50.         editor.commit();
  51.     }
  52.    
  53.     private static void addDontShowPref(SharedPreferences.Editor editor) {
  54.         clearRemindPrefs(editor);
  55.         clearRatedPrefs(editor);
  56.         editor.putBoolean(RATE_DONTSHOW, true);
  57.         editor.commit();
  58.     }
  59.    
  60.     public static void app_launched(Context mContext) {
  61.         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
  62.        
  63.         boolean dontShow = prefs.getBoolean(RATE_DONTSHOW, false);
  64.         if (dontShow)
  65.                 return;
  66.        
  67.         long appLaunchCount = prefs.getLong(COUNT_APPLAUNCH, 0);
  68.         long remindLaunchCount = prefs.getLong(COUNT_REMINDLAUNCH, 0);
  69.         long ratedLaunchCount = prefs.getLong(COUNT_RATEDLAUNCH, 0);
  70.         long appFirstLaunchDate = prefs.getLong(DATE_FIRSTLAUNCH, 0);
  71.         long remindStartDate = prefs.getLong(DATE_REMINDSTART, 0);
  72.         long ratedStartDate = prefs.getLong(DATE_RATEDSTART, 0);
  73.        
  74.         SharedPreferences.Editor editor = prefs.edit();
  75.  
  76.         boolean ratedClicked = prefs.getBoolean(RATE_CLICKEDRATE, false);
  77.         if (ratedClicked) {
  78.                 long launches = ratedLaunchCount + 1;
  79.                 editor.putLong(COUNT_RATEDLAUNCH, launches);
  80.                
  81.                 if (ratedStartDate == 0) {
  82.                         ratedStartDate = System.currentTimeMillis();
  83.                         editor.putLong(DATE_RATEDSTART, ratedStartDate);
  84.                 }
  85.                
  86.                 if (launches >= LAUNCHES_RATED) {
  87.                         if (System.currentTimeMillis() >= ratedStartDate +
  88.                                         (DAYS_RATEDPROMPT * 24 * 60 * 60 * 1000)) {
  89.                                 clearRatedPrefs(editor);
  90.                                 showRateDialog(mContext, editor);
  91.                         }
  92.                         else
  93.                                 editor.commit();
  94.                 }
  95.                 else
  96.                         editor.commit();
  97.                
  98.                 return;
  99.         }
  100.        
  101.         boolean remindLater = prefs.getBoolean(RATE_REMIND, false);
  102.         if (remindLater) {
  103.                 long launches = remindLaunchCount + 1;
  104.                 editor.putLong(COUNT_REMINDLAUNCH, launches);
  105.                
  106.                 if (remindStartDate == 0) {
  107.                         remindStartDate = System.currentTimeMillis();
  108.                         editor.putLong(DATE_REMINDSTART, remindStartDate);
  109.                 }
  110.                
  111.                 if (launches >= LAUNCHES_REMIND) {
  112.                         if (System.currentTimeMillis() >= remindStartDate +
  113.                                         (DAYS_REMINDPROMPT * 24 * 60 * 60 * 1000)) {
  114.                                 clearRemindPrefs(editor);
  115.                                 showRateDialog(mContext, editor);
  116.                         }
  117.                         else
  118.                                 editor.commit();
  119.                 }
  120.                 else
  121.                         editor.commit();
  122.                
  123.                 return;
  124.         }
  125.        
  126.         long launches = appLaunchCount + 1;
  127.         editor.putLong(COUNT_APPLAUNCH, launches);
  128.        
  129.         if (appFirstLaunchDate == 0) {
  130.                 appFirstLaunchDate = System.currentTimeMillis();
  131.                 editor.putLong(DATE_FIRSTLAUNCH, appFirstLaunchDate);
  132.         }
  133.        
  134.         if (launches >= LAUNCHES_FIRSTPROMPT) {
  135.                 if (System.currentTimeMillis() >= appFirstLaunchDate +
  136.                                 (DAYS_FIRSTPROMPT * 24 * 60 * 60 * 1000)) {
  137.                         showRateDialog(mContext, editor);
  138.                 }
  139.                 else
  140.                         editor.commit();
  141.         }
  142.         else
  143.                 editor.commit();
  144.     }  
  145.    
  146.     public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
  147.         final Dialog dialog = new Dialog(mContext, R.style.NoTitleDialogTheme);
  148.        
  149.         String appTitle = mContext.getString(R.string.base_app_name);
  150.        
  151.         LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  152.  
  153.         LinearLayout rateView = (LinearLayout)inflater.inflate(R.layout.apprate_dialog, null, false);
  154.         TextView rateTitle = (TextView)rateView.findViewById(R.id.rating_title);
  155.         rateTitle.setText("Rate " + appTitle);
  156.         TextView rateDesc = (TextView)rateView.findViewById(R.id.rating_description);
  157.         rateDesc.setText("If you enjoy using " + appTitle + ", please help us out by taking a moment to rate/review the app. Thank you for your support!");
  158.        
  159.         Button b1 = (Button)rateView.findViewById(R.id.rating_ratebtn);
  160.         b1.setText("Rate " + appTitle);
  161.         b1.setOnClickListener(new OnClickListener() {
  162.             public void onClick(View v) {
  163.                 if (editor != null) {
  164.                         clearFirstLaunchPrefs(editor);
  165.                         clearRemindPrefs(editor);
  166.                         addRatedPrefs(editor);
  167.                     mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + mContext.getPackageName())));
  168.                 }
  169.                 else
  170.                         throw new NullPointerException("Must pass a valid SharedPreferences Editor");
  171.                
  172.                 dialog.dismiss();
  173.             }
  174.         });
  175.  
  176.         Button b2 = (Button)rateView.findViewById(R.id.rating_remindbtn);
  177.         b2.setText("Remind me later");
  178.         b2.setOnClickListener(new OnClickListener() {
  179.             public void onClick(View v) {
  180.                 if (editor != null) {
  181.                         clearFirstLaunchPrefs(editor);
  182.                         clearRatedPrefs(editor);
  183.                     addRemindPrefs(editor);
  184.                 }
  185.                 else
  186.                         throw new NullPointerException("Must pass a valid SharedPreferences Editor");
  187.                
  188.                 dialog.dismiss();
  189.             }
  190.         });
  191.  
  192.         Button b3 = (Button)rateView.findViewById(R.id.rating_nothanksbtn);
  193.         b3.setText("No thanks/Already have");
  194.         b3.setOnClickListener(new OnClickListener() {
  195.             public void onClick(View v) {
  196.                 if (editor != null) {
  197.                         clearFirstLaunchPrefs(editor);
  198.                         clearRemindPrefs(editor);
  199.                         clearRatedPrefs(editor);
  200.                     addDontShowPref(editor);
  201.                 }
  202.                 else
  203.                         throw new NullPointerException("Must pass a valid SharedPreferences Editor");
  204.                
  205.                 dialog.dismiss();
  206.             }
  207.         });
  208.  
  209.         dialog.setContentView(rateView);        
  210.         dialog.show();        
  211.     }
  212. }
  213.  
Parsed in 0.061 seconds, using GeSHi 1.0.8.4


Layout XML
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.  xmlns:android="http://schemas.android.com/apk/res/android"
  4.  android:layout_width="match_parent"
  5.  android:layout_height="match_parent"
  6.  android:padding="8dip"
  7.  android:background="#ffffff"
  8.  android:orientation="vertical">
  9.         <LinearLayout android:layout_width="fill_parent"
  10.                                   android:layout_height="wrap_content"
  11.                                   android:orientation="vertical"
  12.                                   android:padding="4dip">
  13.                 <TextView android:id="@+id/rating_title"
  14.                                   android:layout_width="fill_parent"
  15.                                   android:layout_height="wrap_content"
  16.                                   android:textStyle="bold"
  17.                                   android:textColor="#5E5E5E"
  18.                                   android:textSize="20dip"
  19.                                   android:layout_marginBottom="10dip"/>
  20.                 <LinearLayout android:layout_width="fill_parent"
  21.                                           android:layout_height="2dip"
  22.                                           android:layout_marginBottom="10dip"
  23.                                           android:background="#5E5E5E"/>
  24.                 <TextView android:id="@+id/rating_description"
  25.                                   android:layout_width="fill_parent"
  26.                                   android:layout_height="wrap_content"
  27.                                   android:layout_marginBottom="10dip"
  28.                                   android:textColor="#5E5E5E"/>
  29.                 <Button android:id="@+id/rating_ratebtn"
  30.                                 android:layout_width="fill_parent"
  31.                                 android:layout_height="fill_parent"
  32.                                 android:layout_marginBottom="4dip"/>
  33.                 <Button android:id="@+id/rating_remindbtn"
  34.                                 android:layout_width="fill_parent"
  35.                                 android:layout_height="fill_parent"
  36.                                 android:layout_marginBottom="4dip"/>
  37.                 <Button android:id="@+id/rating_nothanksbtn"
  38.                                 android:layout_width="fill_parent"
  39.                                 android:layout_height="fill_parent"/>
  40.         </LinearLayout>
  41. </LinearLayout>
  42.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


Main Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.  
  5.         RatingHelper.app_launched(this);
  6. }
  7.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


It could use a little bit more cleanup, but I believe I've got all of the bugs out.

Let me know what you think, or if you think some code could be handled better.
hooked82
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 06, 2011 11:38 pm

Top

Re: Delayed Rate/Review Prompt for Android Market

Postby Nazgulled » Tue Oct 09, 2012 4:05 pm

Looks great. I'll be using this on my own app and give you proper credit :)
User avatar
Nazgulled
Freshman
Freshman
 
Posts: 8
Joined: Tue Nov 22, 2011 4:57 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Google Feedfetcher and 7 guests