When I first created Baby’s First Game for Android I gave away a free promotional version. This promotional version was basically to get feedback from people before the real release. I wanted the app to only work for a limited time after which it would disable itself. I suppose I could have just released the full version without any limitations but I didn’t know how well (or poorly as it turns out) my app would do or how a free version floating around would affect it. Plus I wanted to see how one might create a time limited app anyway.
I created a single method called doPromoCheck()
that stores the current install date in the app settings and checks them on every start up. I also added a check to alert the user once a day that the application was a promotional version and would expire in x
number of days. This message I wanted shown only once a day as to not annoy the users. Here’s the method with some static class variables for configuration:
... private static final String PROMO_DATE = "promoDate"; private static final String PROMO_DISPLAYED = "promoDisplayed"; private static final long DAYS_MILLIS = 24 * 60 * 60 * 1000; private static final long PROMO_DURATION = DAYS_MILLIS * 30; // 30 days private void doPromoCheck() { SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); long now = System.currentTimeMillis(); long exp = prefs.getLong(PROMO_DATE, now + PROMO_DURATION); editor.putLong(PROMO_DATE, exp); // check if the promo period has ended if(now > exp) { AlertDialog alertDialog; String ok = getResources().getString(R.string.label_ok); alertDialog = new AlertDialog.Builder(this) .setCancelable(false) .setPositiveButton(ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); // stops the app as soon as the dialog is dismissed. } }).create(); String title = "Promo Version"; alertDialog.setTitle(title); String msg = "This promotional version has expired. Please purchase the official version from Google Play."; alertDialog.setMessage(msg); alertDialog.show(); } else { long disp = prefs.getLong(PROMO_DISPLAYED, now); // only display the dialog if it's been one day since the last time it was displayed if(now >= disp) { // save the next display time for one day from now. editor.putLong(PROMO_DISPLAYED, now + DAYS_MILLIS); AlertDialog alertDialog; String ok = getResources().getString(R.string.label_ok); alertDialog = new AlertDialog.Builder(this) .setCancelable(false) .setPositiveButton(ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // do nothing } }).create(); String title = "Promo Version"; alertDialog.setTitle(title); long days = (exp - now) / DAYS_MILLIS; String msg = "This promotional version will expire in " + days + " days. Please consider purchasing the official version from Google Play."; alertDialog.setMessage(msg); alertDialog.show(); } } editor.commit(); }
The first time this is run it calculates the expiration date and stores it in the private app settings. Each time it is run after, it pulls the expiration out of the app settings. To use this method in your app, put the call as the first thing in your main Activity onCreate()
method:
protected void onCreate(Bundle savedInstanceState) { doPromoCheck(); ... }
That’s it. Your application will now only last for 30 days and will simply show the promotional period ended dialog after. Of course, the app can be uninstalled and reinstalled to reset the expiration but it’s annoying and probably not worth the hassle to most people.