10

Hello does anyone have a code example of how I can time bomb an Android application so It will not work after a given date?

I would like to release a "beta" application for testing but would like to make sure it will only work while the application is officially in beta.

Tim B
  • 40,716
  • 16
  • 83
  • 128
Tom
  • 7,316
  • 9
  • 35
  • 35
  • I think this is already answered here : [http://stackoverflow.com/questions/995719/android-trial-applications](http://stackoverflow.com/questions/995719/android-trial-applications) Well, not with any code examples – Tommy Aug 03 '09 at 17:49

2 Answers2

16

I would suggest using the Calendar class and having your application checking the current date against your expiration date in your OnResume(s).

The code would look something like this:

    protected void onResume()
    {   
        super.onResume();

        Calendar expirationDate = Calendar.getInstance();
        expirationDate.set(2009, 7, 3);  //hardcoded expiration date
        Calendar t = Calendar.getInstance();  //Calendar with current time/date
        if (t.compareTo(expirationDate) == 1)
           finish();
    }
Will
  • 19,789
  • 10
  • 43
  • 45
  • 3
    The Java Calendar class month numbering start at 0, not 1. – Will Aug 29 '09 at 03:47
  • 2
    So, 15th September would be: (2009, 8 , 15) Think thats correct. – Tom Aug 29 '09 at 18:00
  • What if the user changes the date to the past ? – Guido Mar 20 '10 at 11:39
  • @GuidoGarcía See the 3rd technique in this answer. http://stackoverflow.com/a/996288/957245 – Declan McKenna Feb 22 '12 at 23:08
  • Why did you use onResume() ? – chia yongkang Nov 21 '19 at 03:08
  • If my memory serves, there is a certain order that procedures are called. In theory, an Android app could never be closed, so OnStart() would be a bad place to check for expiration. OnResume() happens whenever you switch to an app, so I'm confident it will be called. I haven't touched Android programming in about a decade, so things might have changed.. – Will Nov 23 '19 at 01:48
3

Also depending on your application, you may want to have the expiration call make a call to a webserver, that way if you wanted to extend or change the date, it would be dynamic and would not cause the applications to expire prematurely. Just my 2 cents.

broschb
  • 4,976
  • 4
  • 35
  • 52
  • This would be a great solution. I would love to do this but I am unsure on how to best program using web servers and android. – Tom Aug 04 '09 at 23:49
  • @Tom that's because it's a pain in the ass. Look up AsyncTask. – Jeremy Logan Aug 05 '09 at 09:41
  • 1
    It's actually fairly simple and straight forward. I have been playing around with this, and am going to write a blog(broschb.blogspot.com) post on this, and will update this once I have. But I used GoogleAppEngine and Restlet(http://www.restlet.org/). Restlet has libraries for GAE, and Android. With this it is pretty simple to get something simple set up. I'll try and write something up in the next few days and post back. – broschb Aug 05 '09 at 17:13
  • @broschb That will be amazing. I was thinking wouldn't it be cool to power such a system with the AppEngine. – Tom Aug 05 '09 at 17:15
  • 1
    I wrote an article on setting up the first part of this, with a small example for getting the app engine setup. I'll try to get the second part of the post done with the android integration this weekend, in the meantime you can see how simple it is to get GAE going. The post is here. http://broschb.blogspot.com/2009/08/restful-service-on-google-app-engine.html – broschb Aug 08 '09 at 18:19
  • 1
    I created another followup tutorial, that shows how to tie all of this together by calling an application in GAE, and retrieve and expire date from Android. You can see it here http://broschb.blogspot.com/2009/08/android-and-google-app-engine.html – broschb Aug 14 '09 at 00:54