I basically want to create a timer. I have a textview where I need to show a timer like "Updating your location in 2min 29 secs" and I want the timer to decreament For eg 2min 28secs followed by 2min 27 secs.
And I want to update it even when the user is not using my app (I dont want it to update in real sense what I mean is when user opens my app after say 1 min then the timer should show 1min 27 secs).
Can some one please help me out in pointing out the correct and efficient way of doing this ? Thanks :)
The most efficient way in my opinion is using scheduleAtFixedRate when your activity is in foreground and when it is going to background you do not need any services you can just save the current time and the remaining time in a sharedpreferances and when again your activity comes to foreground read those values plus current time and then update the textview in a proper way.
You could use a service and send intents that your activity receives (the one with the TextView) using LocalBroadcastManager. You should register a BroadcastReceiver inside Activity.onResume(), and unregister it inside onPause().
Using a TimerTask should be the default approach but you must be aware that if your activity is sent to background it can be killed by the system. That's why I recommend using a service.
Hope it helps.
Related
I have a service which reads data from a Socket connection and during peak hours. The frequency of the changes is every five seconds.
I have two options in mind.
Update the List, using a BroadCastReceiver.
Create a new Thread in the Activity where I created the List, read a static variable every five seconds, and when the change occurs, change the static variable from the Service.
Which option is more efficient and what are the pros and cons of both?
If you think there is a better option, please let me know.
Thank you.
I would use the BroadcastReceiver (with the LocalBroadcastManager) option. I would recommend you to make your model class implement Parcelable. I would definitely avoid the solution 2
BroadCastReceiver will be efficient for your case.
It will help you to update your ListView when it receives new data. So you don't need to check for update in every 5 seconds.
It will increase the performance of your App. On the other hand in case of very frequent updates it will not wait for the given interval.
So go with broadcastreceicer.
Here is the pros that you will get from BroadcastReceiver :
A Broadcast receiver wakes your application up, the inline code works only when your application is running.
For example if you want your application to be notified of an incoming call, even if your app is not running, you use a broadcast receiver.
If your application is playing audio, and you want to stop the music on an incoming call, you use the inline code.
With broadcast receiver you can't uptade the UI, you can see WHY here, if you talk about other thing, explain us please... In that post you can see a other method to update UI from intent service, and I think is the best way to update UI from intent service because you only update UI when you need it and you don't overcharge the app process .
Finally if you want to update your UI every 5 seconds do it but with a little difference... I would do update every 5 seconds my UI if my app is in first plane if isn't it then update my app when app comes to first plane in OnResume() method because you don't use extra memory from device when my app is in background...
Tell me if I helped you and good programming!
I have an App where the user can create multiple countdown alarms. Each one has it's own name, time to count, ringtone, etc.
I extended the CountDownTimer to hold each alarm setup and created a Singleton manager to manage all the alarms.
Everything is working fine until I leave the App.
The counters don't actually die. They have a weird behavior. After starting a counter:
1) if I press back key until the home screen and then power off the screen the alarm will trigger as it should.
2) if I press the home button then turn off the screen it will trigger only if I open the app.
3) if I just turn off the screen while still on the app it will trigger as soon as I turn on the screen.
I expected trouble because for that kind of thing I need to use Services, Handlers or other stuff like these.
The problem is, I'm new to Android and after reading a lot about it, I couldn't find a solution.
As far as I understood I should use Services, but it can have only one instance. How would I work with multiple alarms?
Is there other way of doing it?
I need a way to start each alarm and tell the system to keep counting no matter what happens! (and to call an Activity when it finishes)
Any help?
Is there other way of doing it?
If your goal is to alert the user of events in the future, regardless of whether your app is running, dump all your CountdownTimer logic, and switch to AlarmManager. It is specifically designed for this sort of scenario. You can set up an AlarmManager event for each timer, to get control when the end time is reached.
Also note that you will need to maintain information about your registered events in a file or database:
so users can add and remove them, and
so you can set up AlarmManager again if the user reboots their device
and to call an Activity when it finishes
Users may reject your app for that behavior. Not everyone wants to be interrupted in the middle of what they are doing with their device. Please allow this to be configurable, so the user can opt for something less intrusive, like a Notification.
You should replace your CountdownTimers with AlarmManager (as in CommonsWare's answer), after setting it up it will trigger your function to execute on specified time, here is official guide:
http://developer.android.com/training/scheduling/alarms.html
Soon you will find that after device reboot your alarms are lost, you need to restart them by catching android.intent.action.BOOT_COMPLETED broadcast.
Also you should not do a lot of work in your alarm onReceive, you should pass information to some other component like IntentService, to make it safe while device is asleep you should use WakeLock (sample project).
[edit] ---- you should also store your alarm/ringtone related data in some persistant storage, you can use for it shared preferences, or (more work) sqlite database.
I you really want to use timer (i.e. when you want to show countdowns on screen):
You don't have to have multiple timers to show multiple countdowns.
Everything you have to do is just put the target time into some collection, or event put it into the View.setTag().
Then you have timer (let say 1s tick) - on every single tick you have to iterate over all your gauges, check the target time and current time, parse the number and adjust the gauge.
However - as the timers works on separate (non main) threads - you will have to use handler or something like that.
If you want to just set some alarms - take the CommonsWare's answer - it's good one.
Basically,
I need to for a queue collection to persist about for about 15 seconds after a user either shuts down the app or switches to another application and then, presumably, comes to the app.
I'm looking to either save the items in the queue or process in some other way the items during the 15 seconds that they persist.
Is this possible? Can anyone point me in the right direction?
This is my second android app and the first time I have to do this so any help would be appreciated.
You can use onResume() to run commands when they com back from another task.
As for running something X seconds after they quit the application, that would require to include a "service" in your application, which could be called by your task onStop()
Although it is possible for your activity to remain active in the background you should not depend on it, as different devices will be more or less agressive with resource reclamation.
Using a service is the only sure fire way to run something in the background while orhter applications run.
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/guide/topics/fundamentals/services.html
I'm trying to find a way to implement this.
basically it's a timer say like in farm ville or such games that count even when you exit the app.
how do I implement that?
Put all logic that's supposed to work even if your app is not visible inside service. Use AlarmManager, Timer there. Service has the same priority as visible Activity, so it won't be killed.
You are going to need to keep a service running to keep track of these things when your app is not visible in the Activity stack. See here:
http://developer.android.com/reference/android/app/Service.html
I am having some design techniques about How shall I schedule a code to retrieve the weather info?
Should I use alarms to retrieve the weather each 10 minutes?
And do I need to run a service for this? Or just put the code in the Broadcastreceiver and start when the alarm fired?
This is a good question. Yes, you will need to put this code in either a service or broadcastreceiver because when and activity loses focus (meaning the user is using a different app or the phone is asleep) they pause and/or close. However, i have no experience with either Services or Broadcastrecievers, so that is as much as i can tell you.