Question:
In some paid applications, it shows you a dialog so you can buy the full version of the app, and in others it is the 30-day trial.
How to apply an expiration date to an application on Android?
Answer:
Response inspired by SO , SO , mobile-trial
Ways that could be implemented, from simpler to skip to more complex.
-
First method save the installation date in the
base de datos
,fichero interno o externo de la app
or useshared preferences
. But it is very likely that the user will skip the test time restriction :, uninstall and reinstall the app , if an external location is used for the file, the user may not be aware of the residue. -
Second method: use a fixed deadline date
hard bomb-time
that is to say that for all users this date expires. It is easy to get around that restriction, just change the system time to a date before the deadline , for the user it can be a bit cumbersome to do it often and decide to buy. -
Third method of all the other is the safest (but not infallible) that if it requires more resources,
internet
connection,servidor web propio
, is to create a remote unique identifier checker, you can get the device'sgetDeviceId
and check the base remote data if it exists, get the install date, if it doesn't exist add a new entrygetDeviceId : fecha agregación
. If the device does not have internet, the user can be warned and the app terminated.
Code of each method
not checked if they work:
Example of the first method using SharedPreference
private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private final long ONE_DAY = 24 * 60 * 60 * 1000;
@Override
protected void onCreate(Bundle state){
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String installDate = preferences.getString("InstallDate", null);
if(installDate == null) {
// First run, so save the current date
SharedPreferences.Editor editor = preferences.edit();
Date now = new Date();
String dateString = formatter.format(now);
editor.putString("InstallDate", dateString);
// Commit the edits!
editor.commit();
}
else {
// This is not the 1st run, check install date
Date before = (Date)formatter.parse(installDate);
Date now = new Date();
long diff = now.getTimeInMillis() - before.getTimeInMillis();
long days = diff / ONE_DAY;
if(days > 30) { // More than 30 days?
// Expired !!!
}
}
...
}
Example of the second method using time-bomb
protected void onResume()
{
super.onResume();
Calendar expirationDate = Calendar.getInstance();
expirationDate.set(2016, 12, 31); //expirara final de año
Calendar t = Calendar.getInstance(); //obtener fecha actual
if (t.compareTo(expirationDate) == 1)
finish();
}
Example of the third method response from user @Martin_christmman implement https://github.com/mobile-trial