Search code examples
androidtrialware

How to make an Android application work only for some trial period


I have an application which should work only for 30 days or few days.

It's just a simple application which has some audio files playing to corresponding to images displayed with some text displayed.


Solution

  • There is only one way to truly achieve this:

    You will have to set up a server, and then whenever your application is started your app sends the phones unique identifier to the server. If the server does not have an entry for that phone id then it makes a new one and notes the time. If the server does have an entry for the phone id then it does a simple check to see if the trial period has expired. It then communicates the results of the trial expiration check back to your application. This approach should not be circumventable, but does require setting up a webserver and such.

    There are other ways(such as storing installation date somewhere) but then if user uninstalls your app, that info will be gone and when he reinstalls there is no way to know if he installed before.

    EDIT: Ok, since you want to go with SharedPreferences way, here is an example:

    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 !!!
            }
        }
    
        ...
    }
    

    I haven't compiled this but should give you an idea.