-3

I am creating an app that has to expire after three days. the user should not be able to reinstall the app if it expires. I am storing a file on sdcard at the first run of app, then I want to check the current time of device to that of the creation time of the file. I am not able to calculate differences between two dates . I have written following code.How can I accomplish this task.plz its urgent. thanks in advance.

Date firstFile =  new Date(firstFile.lastModified());
Date currentTime= new Date(currentfile.lastModified());

I am able to get the values in both variables, but I need to calculate difference in days or minutes

fvu
  • 32,488
  • 6
  • 61
  • 79
Karthik Pai
  • 587
  • 1
  • 9
  • 18
  • start from http://stackoverflow.com/q/3384254/1007273 – hovanessyan Nov 28 '12 at 13:46
  • 3
    So the user can delete the file and create it manually and use the app forever? – jlordo Nov 28 '12 at 13:46
  • Are you aware that the user can easily extend the trial period just by changing the file? – Philipp Nov 28 '12 at 13:47
  • Date's [getTime](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime%28%29) method gives you a timestamp in millis, it's trivial to subtract these and convert the result to minutes ( / 1000 / 60) or days ( / 1000 / 60 / 60 /24 ) – fvu Nov 28 '12 at 13:49
  • Store the install date in the file encrypted. That way users can't easily temper it. – Chan Nov 28 '12 at 14:08
  • If you enrcypt the file you won't able to change it, but delete it will be enough. – Enrichman Nov 29 '12 at 10:59

5 Answers5

0

Get the time in millis and then do some simple math.

int days = (int) (milliseconds / (1000*60*60*24));

The problem is that this is not secure. If you delete the file where you're storing the date... you're done.

The only way to make it safe is to use a server to map the phone at the first start.

More information about Expiring App.

Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • I am answering my own question now. We can do this by storing install time in sharedpreferences. And then on opening the app check it in sharedpreference.User cannot modify it manually. – Karthik Pai Jul 04 '18 at 07:15
0

Get the millis for each date. Subtract and compare with number of millis in 3 days.

    Date firstFile =  new Date(firstFile.lastModified());
    Date currentTime= new Date(currentfile.lastModified());
    long millisInADay = 1000*60*60*24;
    long maxMillis = millisInADay * 3;
    if (maxMillis < (currentTime.getTime() - firstFile.getTime()) {
        // expired;
    }

Of course as the others have mentioned all the user needs to do is remove the file or edit it and then they are free to use the app. But the typical user probably wouldn't be savvy enough to figure that out. Just depends on your target user.

km1
  • 2,383
  • 1
  • 22
  • 27
0

I am answering my own question now. We can do this by storing install time in sharedpreferences. And then on opening the app check it in sharedpreference.User cannot modify it manually.

Karthik Pai
  • 587
  • 1
  • 9
  • 18
0

The right thing to do is, store the deviceId of device and install-time to a server via post api on first install. Check install time from this server.

Karthik Pai
  • 587
  • 1
  • 9
  • 18
-1

My first advice don't use default datetime package. Go for JodaTime library.

DateTime firstFile =  new DateTime(firstFile.lastModified());
DateTime currentTime= new DateTime(currentfile.lastModified());
long milles = currentTime.getMillis() - firstFile.getMillis();
int days = (int) (milliseconds / (1000*60*60*24));

Some reasons to consider Jodatime over java.util.Date -

  • From JDK1.0
  • Uses two digit years (from 1900)
  • January is 0, December is 11
  • Should have been immutable
  • Most methods deprecated in JDK1.1
  • Uses milliseconds from 1970 representation

For more usage regarding Joda-time refer the IBM tutorial.

Plus using a file to enforce trial period is futile. You should try a server trial or store a file encrypted with the date(not the lastModified). Even if the user finds the date he can't change it. Hope it inspires you.

Chan
  • 2,601
  • 6
  • 28
  • 45
  • Why? Standard Date stuff works just fine for what is required here. – Gimby Nov 28 '12 at 15:23
  • I would ask you to read this - http://mestachs.wordpress.com/tag/jodatime/ and this - http://www.ibm.com/developerworks/java/library/j-jodatime/index.html – Chan Nov 28 '12 at 16:44
  • @Duli-chan The downvoted isn't mine, but although in general I agree that Joda is the best way to escape date calculations related insanity in Java, OP's requirement is so trivial that it's impossible to mess up even with Java's standard Date implementation - adding Joda doesn't make it any simpler/more robust or whatever. – fvu Nov 28 '12 at 19:07