When we start to develop any commercial android application, we may need to develop a trial application which runs on the user's device for a specific time and forces the user to buy the application to use it further.
Below are three techniques you can use to implement a time-limited trial in an Android app, with pros and cons for each.
1) Save first-run timestamp locally (easiest)
When the user runs the application for the first time, save the current date and time to SharedPreferences, a file, or the app database. Every time the app starts, compare the current date/time with the saved first-run timestamp to determine whether the trial period has expired. If it has, call finish() or otherwise prevent further use.
Pros:
- Very easy to implement.
Cons:
- Easily broken by uninstalling and reinstalling the app (which clears the app data), or by manipulating the device clock.
2) Hard-coded end date (simple, but flawed)
Hard-code a single end date for the trial in the application. All users stop being able to use the trial after that date.
Pros:
- Easy to implement.
Cons:
- Can be bypassed by changing the device date/time.
- All users share the same end date (not individualized).
3) Server-validated device registration (recommended, most robust)
This approach requires a server and a database. Whenever the app starts, the device sends a unique device identifier to your server. The server checks whether there is already an entry for that device; if not, it creates one with the current date/time as the trial start. On each app start the server evaluates whether the trial period has expired and communicates the result back to the app.
Notes and links:
- To obtain a device identifier you can refer to TelephonyManager.getDeviceId():
- https://developer.android.com/reference/android/telephony/TelephonyManager.html#getDeviceId%28%29
- For an example of sending data to a server from Android, see my earlier post: "Syncing data to the Server" — https://karjol-gt.blogspot.in/2012/05/sending-sqlite-data-of-android-app-to.html
Pros:
- Much harder for users to bypass (uninstall/reinstall and device clock changes do not reset server records).
Cons:
- Requires extra infrastructure (server + database) and careful handling of identifiers and privacy.
Where to do the check in your app
Do the trial expiration check in the onCreate() of your main Activity. If the trial has expired, show an AlertDialog that points the user to the full/commercial version on Google Play and then call finish() when the user confirms.
Useful links:
- Google Play: https://play.google.com/
- finish() reference: https://developer.android.com/reference/android/view/ActionMode.html#finish()
Example flow (high level, keep this as guidance rather than exact code):
- App starts -> collect device identifier -> POST to server endpoint -> server replies {"expired":true/false} -> if expired show dialog and call finish().
Security and privacy considerations (brief)
- Be careful with device identifiers and permissions. Some identifier APIs require runtime permissions or are deprecated on newer Android releases; review current Android docs before choosing an identifier.
- Document how you store and use device identifiers in your privacy policy.
Closing
Third approach is the most robust and is the recommended way to implement a trial application that cannot be easily broken. It requires more effort (server + database) but provides the best protection for commercial apps.
Happy coding.





