android: run timer for long period - android

if i want to run a timer for a long time in android and show the user, when they go to a particular activity, for example, the duration since the timer started as a live number - how should I implement this? By live, i mean the time shown changes in real time as one would expect a timer to do. There will only need to be one instance of this timer, it'll pretty much be like android's stopwatch but implemented to function within a custom app.
Would I require wakelock?
should I create a service?
or should I just use a simple java timer?
any help/advice much appreciated.
thank you.

What do you mean 'long timer' 1 minut, day?
You have to realise that android can kill you activity any time, so the activity is not the right place to do it.
You can run you operation in Service - his life is longer then activity's, but you probably want to check time even if user returns in activity after week or reboot device.
If you tell more info about what you want it'll be easier to solve you problem.
show the user, when they go to a particular activity
As I see you problem the solution should be like this:
First enter to the activity - create timer, start it,
On activity stop save current value of the timer and system time,
On recreate activity read saved value and start new timer with value of init_value + (current_time - saved_time).
In this case you can be sure that timer is persistant even if user left your activity and even restart device.
And also battery life will be much more longer :)

Related

Handler or Alarm Manager for writing to a file ever second

If I want to write to a file every 1 second some infomation that I get from the phone (in addition to updating activity GUI), then would I be better using a recursive handler(postDelayed) or should I use alarmmanager?
Which one is more stable? Which one would you recommend? My worry with the handler that if one timeout crashes (or for some reason didn't get called) then none of the rest will start. With Alarm manager, I don't think you have this but "some people" say it is too heavy for 1 sec intervals and you don't have much control over gui (I find it hard to belive the later statement).
Please advise
Thank you

Android Best Way to Detect and Handle User INACTIVITY

Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.
So my question is twofold:
1) Is there a better way to detect user inactivity than using a combination of
activity.onUserInteraction() to reset a CountDownTimer?
Note: One reported downside to this approach is that softkeypad interaction might not be
caught by this approach.
Note: Another reported downside is the CountDownTimer is off main thread and might not update
correctly. I am not sure how big an issue this is?
Note: CountDownTimer appears to have cancellation issues as well:
how to stop/cancel android CountDownTimer
2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem
there are still some questions:
a) should each activity launch its own countdown timer?
b) Should a single countdown timer be restarted in the onCreate method of each activity?
c) lets say I want to dim the screen or goto main activity when the countdown expires where
should the timeout handler be located? In each activity? In a service?
Thanks
Just stumbled upon this question as I've answered something similar just now.
Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.
Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.
To quote from my previous answer:
You'll need to invest a little thought into exactly what your
requirements are here, but from what I can tell, you want to keep
track of the user interactions and if a time limit expires since the
last interaction, perform some action, in your case logging them out
of your application.
Firstly, you'll need some place that you can track when the last
interaction occured, since you'll want this to be application wide you
could use a singleton to hold this, or override the Application class,
either way should do.
Next, you'll need to start tracking user interactions. From your
activities, you can override the onUserInteraction method, this gets
invoked anytime the user interacts with the application such as key
event. Each time you hit this method, update your singleton and let it
know something has happened, with a timestamp.
Finally, you'll need some kind of looping check to constantly check if
anything has happened recently. Theres various was of doing this, you
could have a continuous loop that compares current timestamp to the
last recorded event, a bit of draft code :
while(true)
{
if (timeLastEventRecorded < (now - 15))
{
//nothing has happened in 15 minutes, so take corrective action
}
}
Presumably you'll already have some code in your application that
takes care of logouts, such as when the user clicks "logout", you
should just be able to invoke that in the sample above.

Redirect the page in Android

I am having two page, in one page am having chronometer, after I left and come back the page the chronometer has stopped, any one can tell me how to redirect the page
My code:
Chronometer chrono=(Chronometer)findViewById(R.id.chronometer);
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
thank you
I have no idea what you mean by "redirect the page", but it sounds as if you should read up on Android application fundamentals. Android activities do not execute when not in foreground. If you want to reset the clock once the activity is again in the foreground, you should probably override Activity.onResume().
Maybe I'm not understanding what you want to do, but it sound like you'd like to start a stop watch of some sort, go to another activity, then come back and check the amount of time passed. If that's your goal, you might be better off storing the start time in a variable, then when the activity is reactivated get the current time, compare to the start time, and compute the time passed.

How to know for how much long the application is running in android?

I am doing an application in android which requires to know for how long the application is running. Do anyone know how to retrieve such information??. Is there any way where android provides the information about the running applications ,from how much time they are running??
I'm not aware of any method which would handle that but you can simply implement it yourself. Just capture the current time in seconds and in the onPause() method do the same. Then just subtract the first saved time value and the last saved value and you should know for how many seconds the application was running.
There might be another more elegant solution I don't know of tho.
You could user SystemClock.uptimeMillis() or SystemClock.elapsedRealtime() in your main activity once it is first launched. Then each time you need to see how long the app is running call the method again and subtract from the original value.
There is some data collected for all applications to show battery usage by application, but I've not done the research to find out how to access it programmatically (if even possible) and am not sure when it is reset (perhaps on every charger/usb disconnect).
For one application the previous suggestions about instrumenting create and pause/resume methods sounds best.

Advice on structuring a service, activity, thread

can someone please help me?
I would like to write a program which uses a service to periodically update a text view on an activity.
I do this by having ActivityA with a 2 buttons to start/stop my service. In the service I run a timer which triggers every second. From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
I'm sure there are likely better ways to do this, such as using only one activity, maybe using a thread but the main design consideration is to have the service running even if my activity is destoyed (the counter value would instead go trigger some alarm or file write instead of a text view update).
Sorry for rambling. I find the android developer resources offer too many solutions!
Thanks
Ben
In the service I run a timer which triggers every second.
Why? Most Android devices run on batteries. Batteries are never big enough. What value are you giving the user to justify your expenditure of CPU and RAM (and, hence, battery life)?
From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
Where is "here"?
I'm sure there are likely better ways to do this, such as using only one activity
I would think so.
maybe using a thread
Probably not.
but the main design consideration is to have the service running even if my activity is destoyed
This is significantly more complicated than you are perhaps thinking.
(the counter value would instead go trigger some alarm or file write instead of a text view update).
If your goal is to do something at a particular time, use AlarmManager.
I suspect that there is a better approach for whatever it is that you are trying to do than the path you are presently headed down. Unfortunately, since I do not know what it is that you are trying to do, I have limited ability to provide more specific advice.
I think what you want to do is at best done with an simple AsyncTask. If you use the onProgressUpdate method you can increase the value in the textview at every time you reach a certain point during your background work. It is also able to cancel the background work etc. There is no need for the full Service, Thread work.

Categories

Resources