Callback method for hour Change in Android - android

I want to create a live wallpaper that respond to time change like if the time is 18:00 it should change to night wallpaper.Is there any callback method for checking hour change

Schedule a recurring task in Android and set the initial delay to the time difference between the current time and 18:00 hours. To learn about scheduling recurring tasks take a look at this thread - Scheduling recurring task in Android
I suggest you use an AlarmManager. Take a look at the methods inside the class related to scheduling.

You can use AlarmManager to set a pendingintent to be fired at any time you want.
You can then grab the intent in a broadcast receiver and update your wallpaper.

Related

Should I use setAlarm, setExact or setRepeat Alarm?

For setAlarm() parameter : Should I use SystemClock.elapsedRealtime(), calander,getTimemillise or something?
Because Alarm is coming fine, but when I manually go to my Android phone date & time settings and change the time (not past, but future time), alarm is not coming and triggers immediately.
This is an intended behaviour as specified in the official Alarm Manager docs for all the set methods.
If the stated trigger time is in the past, the alarm will be triggered immediately.
to manage you can try to cancel such alarms with past trigger time in your reciever .
How to choose between
System.currentTimeMillis() vs SystemClock.elapsedRealtime()
As the link specifies the :-
The first method, will give you the standard "wall" clock (time and date) expressing milliseconds and is affected by and changes in time and date from the settings.
While the second method, return the time in milliseconds since the system was booted, and including sleep time. Hence would not be affected with the changes in the settings so it is the recommend basis for general purpose interval timing.
So if you do not want the time to be dependent on the time-zonal changes , it is better to use the second method.Otherwise you need to handle it as mentioned before.
As compared to other alternatives like Date or Calender , it is better to use currentTimeMillis() due to performance benefits.

How to trigger an action for the calendar events start time in android?

Actually I need to trigger an action(Intent) for the calendar events start time.
I had used the Calendar database to read the events title,start time and end time.
By passing the first start time to the alarm manager, I was able to trigger my action only once. My main problem is how to continue this process by passing different time intervals to the Alarm Manager.
Or is there any better solution to notify the action(Intent) ??
Thanks in Advance
It appears there is no intent to act on using a broadcast receiver.
You can however, read the event instances manually and schedule background tasks using the AlarmManager.
Credits to this answer on a similar question

Android, set custom timing of AlarmManager... Please advise

Hi I need to set AlarmManager to run a reminder for me to take medication. I need to repeat it by custom amount of days and custom amount of times to take in the day.
So is there an efficient way to set the AlarmManager or CommonsWare's Implementation of the AlarmManager to remind me "twice a day starting at 9AM for the next 5 days" to remind me to take medication? Pls advice and tnx in advance for any constructive help in sample code and in relevant tutorials.
I haven't looked into Mark's AlarmManager implementation, but there is no way, in general, to get the bare AlarmManager to do what you are trying to do. You can schedule a single alarm, at a specific time, or a repeating alarm, that repeats at fixed intervals. If you want something that handles complex schedules like the one you describe, you'll have to write or find code that does it.
You want to use a PendingIntent with the AlarmManager. The idea is to schedule the pendingIntent with the alarmManager, have that trigger an intentService or broadcast, setup another pendingIntent with the alarmManager for the next desired event. You want to keep in mind that you'll need the BOOT_RECEIVED permission in case the user reboots their device. I have complex scheduling in Audio Control and this is exactly what I do.
Here is a pretty decent tutorial of what I mean:
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
You need to schedule an alarm to the next time you want to take the medicine - according to your algorithm (for example if its twice a day, and you got to the pending intent callback for the first time today, then schedule the next alarm to start after [6,7,8,9,10...] hours).
You will need to save both last time of the alarm launch and the user settings in shared prefs/file/DB.
You need to handle process down (android killed it or the device was rebooted). In the case of device reboot you should use the boot receiver to start your service, but you need to remember that from android 3.1 the user has to use at least one time the GUI in order for you to intercept the boot completed receiver. The boot completed receiver should look when was the last time that the alarm launched, and according to the user settings set the next alarm launch.
In the case of android killed your service, you will need to make research, i can't help here.
see example

Need to implement a timeout feature when application goes to background

I need to create a timeout feature when my app goes to the background for 5 minutes(anything that fires up onPause() except when activity is finishing). If the user goes back to the application then the timer should be cancelled.
Also, I need the timer to be not dependent on the time set in the phone meaning when the app goes to the background and then the user changes the time the application will still timeout within 5 minutes.
Checking out the documentation of the AlarmManager it states the following:
Note: The Alarm Manager is intended for cases where you want to have your application code
run at a specific time, even if your application is not currently running. For normal
timing operations (ticks, timeouts, etc) it is easier and much more efficient to use
Handler.
Alternatively, you could try setting up bound service - these can operate in background even if user toggles frontmost application. you can communicate with the service using Handler, just as you would do with threads.
the easiest would be to use sendEmptyMessageDelayed of predefined type and use call to removeMessages() once your app is back on top.
You should take a look at the AlarmManager. You can easily program your app to run at a specific time or every 5 minutes with a PendingIntent. You can cancel it with the same PendingIntent.
I don't know about the time dependency but it's easy to test.
in onPause() set an alarm using AlarmManager, like this:
AlarmManager alarmManager = getSystemService(Context.ALARM_SERVICE);
// Alarm time is elapsed time since boot (including sleep) plus 5 minutes
long alarmTime = SystemClock.elapsedRealtime() + 5 * 60 * 1000;
alarmManager.set(AlarmManager.ELAPSED_REALTIME, alarmTime, pendingIntent);
For the pendingIntent() you can use either a broadcast Intent or you can have it start one of your activities. Depends what you want to do when the alarm goes off.
NOTE: Using AlarmManager.ELAPSED_REALTIME ensures that changes the user makes to his date/time settings won't have any effect on your 5 minute timeout.
Make sure to cancel the alarm when your app resumes (in onResume()) and when it finishes (in onDestroy())
I wonder if the easiest solution wouldn't be to just set a value in sharedPreferences when your activity pauses and test this value again onResume. To avoid the value being corrupted by the user changing the clock you could employ SystemClock.elapsedRealtime() or SystemClock.uptimeMillis()

Best way to set repeating task

i have a method in my app that i want to be called repeatedly depending on what the user chooses. like if every hour is chosen by the user, the activity fires a method that is being called every hour. i would like to know the best way to schedule this repeated task.
i have been experimenting with Timers and Timer task, but for some reason it doesn't not seem to work when i use the java calendar class with it, like this:
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.SECOND, 30);
updateTimer.scheduleAtFixedRate(cleanCompletedCache, c1.getTimeInMillis(),hour );
and from what i have been reading, Handlers are not suitable for this multi-repeating task. would i have to use an alarm manager for this and why won't the above code execute correctly? thanks
You want the AlarmManager and it's setRepeating or setInexactRepeating calls.
There you schedule an Intent to be delivered to your application, and write an intent receiver to process it. This way, the activation of your application is entirely the responsibility of the Android system, and your application does not need to run for the entire hour it is just waiting to activate.
If, for some odd reason, you would need your code running between timer invocations, you need to keep a background service running, but you'd still use AlarmManager to get the wakeup.

Categories

Resources