Skip to content Skip to sidebar Skip to footer

Android Timer That Counts When App Is Closed

i have an activity in which i want to track the time elapsed between two actions(for ex startAction and endAction).I've used the following code to implement a timer that increments

Solution 1:

You can start a Service and it will be up and running even if your application is closed.

Here is a good tutorial.

Solution 2:

You could create a Service or create a thread in which your timer runs. This wouldn't destroy your timer when you app is closed.

I hope this helps.

Solution 3:

I resolved your problem using the life cycle.

If the application is not destroyed you can count time by apllying your code into the onPause or onStop.

But ypu have to advertise the user when he is going to destroy the app.

This was my solution to contiune receiving messages wit my app running in foreground and to avoid an excessive power consumtion.

Read about de lifeCycle here:

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

I hope this can be useful.

Post a Comment for "Android Timer That Counts When App Is Closed"