My Android app has a TextView telling the user the data age ("13 minute ago"). I currently update this every second using a Runnable to catch when the minute changes with a notifyDataSetChanged() to my adapter. But this is causing garbage collection every second...
Is there another method to trigger a TextView update (or take general action) when the system clock changes minute without checking every second for such a change?
Since you already set up a Runnable and callback, use modulo to calculate when the next update should happen:
handler.postDelayed(runnable,
DateUtils.MINUTE_IN_MILLIS - System.currentTimeMillis % DateUtils.MINUTE_IN_MILLIS);
This will run the callback when the next minute occurs whether it is 59, 30, or 1 second(s) away. As a note, if the Runnable only updates one TextView don;t forget to switch to DateUtils.HOUR_IN_MILLIS when the event is an hour old, no sense updating "1 hour ago" every minute.
You may find methods like DateUtils.getRelativeTimeSpanString() useful for creating the "x minutes ago" strings.
While Sam's answer is perfectly good, I'm just adding this because it fits the question so perfectly. There is actually a broadcast sent by the system every minute when the clock changes. It is Intent.ACTION_TIME_TICK and you can only receive it using a BroadCastReceiver that was registered manually in your code, not through the manifest. See the documentation here.
User the Timer object with fixed period.
The methods take a TimerTask object.
See the Timer documentation from google
REMEMEBER to cancel the timer when stopping the activity
There is prettier solution for this besides Sam's great answer.
Make use of http://developer.android.com/reference/android/text/format/DateUtils.html to find the time span and register for android.intent.action.TIME_TICK where you'll be updating the time in the listview.
Here is another same question and answer
Related
I have an app which will give the user a bonus power up every 45 minutes. I managed to set a repeating alarm to do it but I have some questions:
1 - Where should I call setRepeating() method? - Having in mind that the alarm should be set automatically and NOT by an onClick event, for instance, I find it tricky to know where to set it. Now I am calling it on the onCreate method of my MainMenu activity. The problem is that everytime he user enters the app it is called. I thought about setting a SharedPreference variable to check if I already called it but it doesn't seem the right approach.
2 - How to the display the time left until next power up? - I would like to display to the user how much time is left until the next power up. The problem is that if the user closes the app and open it again I no longer have a reference to the alarm but it is still running. How can I do that?
Any help will be appreciated. Thanks in advance
I'd use shared preferences for both. When you set the alarm, write a shared preference that says when the next alarm is scheduled for. In onCreate, look at the alarm. If its in the future, no need to set it. If its in the past, set it again. The same answer solves problem 2- subtract the current time from the time in the shared preference, and that's the time left. When the alarm goes off, update the shared preference to the next time.
Android's DatePicker.OnDateChangedListener constantly keeps firing while the user is spinning the calendar. I think this has changed in recent Android versions. Previously it only fired after the spinner stopped.
I would like to update content based on the new date, which involves web-access so this is too slow (and wasteful) to do on every event.
I prefer not to use a separate dialog to choose the date and update my content on the close-event of that dialog.
How do I know (trigger an event) when the datepicker has stopped spinning?
I can think of some mechanism with timers to check if the date is changed in, let’s say, the last second, but this looks pretty complex to me.
I solved it, by re-starting a CountDownTimer on every OnDateChangedListener-event. The timer is only 500 ms.
As long as the user is scrolling CountDownTimer will not finish. It is just constantly canceled and started on every OnDateChangedListener-event, until DatePicker is not scrolling anymore (not for 500 ms that is).
Is there any way in Android to make some action when a certain date is reached ? Something like a listener that is activated when an specific date is reached.
For example, I want to call a method or something when the day is the number 8 or it is 20/12/2012. Does Android provide something like that or I have for example to test every 'x' seconds if I have reached the date I wanted ? I think that would waste resources and should be a better way of doing it.
Thank you
The best way is to set an alarm for midnight on the day you are watching for, using AlarmManager.
there is an intent action called ACTION_DATE_CHANGED. i don't know exactly when it's fired, but you could make a broadcast receiver to catch it and see if it gets fired at midnight.
http://developer.android.com/reference/android/content/Intent.html#ACTION_DATE_CHANGED
I'm new user of App inventor. I need small stopwatch. Basic function:
- set time ex. 25 min (if time=0;alarm=true)
- start time
- reset time (if you want reset time you will see the comfir box (yes or no)
Can anybody show me what block I must use to create it ?. Sorry for my English.
Here is a link to a discussed about this in the App Inventor Coffee Shop.
https://groups.google.com/forum/?fromgroups#!topic/appinventor/qrhfCSv4US0
This block will need a label, and a timer.
You can choose to make it in seconds or milliseconds by changing the clock functions.
Here is an example. The user enters the duration into the minute and second textboxes. Then they press the button. The timer interval is set to the duration converted to milliseconds. Then the timer is enabled. After the duration is up, Clock1.timer fires. The notifier shows an alert that the timer finished and the timer turns off. To reset the timer, just set Clock1.timerEnabled to true.
I'm creating a note application that notifies you at a certain time & date and I'm using a Timer object to schedule that notification. Basically, the user will input the note, time and date to be notified, and then will hit a submit button. Once that button is hit, a TimerTask is scheduled using the Timer class and this note will reflect on a ListActivity.
The problem I'm running into is this...
If the user decides to edit the note by changing the time and date to be notified, since the TimerTask was already scheduled, once the user hits submit, another TimerTask will be scheduled. Hence, there will be two notifications for the same note! I want to store the timer object somewhere so that when the note is to be edited, I can simply edit the timer object. This is my current approach but if you guys have any suggestions on how I can do this better that would be great!
Dump the Timer and TimerTask and use AlarmManager. Timer and TimerTask are fine for things with short periods that are only relevant while an activity is on-screen. AlarmManager is for longer periods, such as "time and date to be notified", because it allows your service to get out of memory.