Search code examples
androidtimerandroid-1.5-cupcake

Android Application and Timers


Hello I have an android application which has a service running. After 20 mins from that service and other systems (such as GPS) starting I would like it to automaticly stop. I assume I need to use a Timer for that?

Can someone show an example of how I could do it?


Solution

  • Maybe you don't even need a timer for that. Just keep track of when your service was started by storing [System.currentTimeMillis()](http://developer.android.com/reference/java/lang/System.html#currentTimeMillis() in a member variable and [stopSelf](http://developer.android.com/reference/android/app/Service.html#stopSelf() your Service whenever you reach the timeout.

    For example, include the following in your Service's busy part:

    if(System.currentTimeMillis() - TIMEOUT > startTime) {
        stopSelf();
    }