I'm currently working on an application that needs to change certain settings on the phone such as volume when a certain event is triggered, such as a time event (between 9am and 5pm) or a battery event (battery below 30%).
The first way I thought to do this was to set up a background service which checks system time and battery level (for example) every so many seconds or minutes, then changes the phones settings when the appropriate time or battery level is met. I'm not entirely certain how to do this and I have seen some other methods which seem possible.
What would be the best method to use to perform these background checks and change settings based on the findings without requiring interaction from the user?
Thanks in advance.
[EDIT]
After being told by Jakar and gobernador to use AlarmManager
, BroadcastReceiver
and IntentService
I came across this answer:
android: running a background task using AlarmManager
Which explained with examples how to accomplish this, thanks for the helps guys!
[EDIT]
I managed to set it up and get the service working, it doesn't do anything yet but the rest should be easy. I did it with the following components:
- Boot
BroadcastReceiver
which sets a repeating alarm to run my alarmBroadcastReceiver
. This will start the service after the phone is rebooted. - Similar code in my main application activity that cancels any other pending intents to start the service. This starts the service when the application is run, good for starting after installation.
- Alarm
BroadcastReceiver
class which has code tostartService()
. - Finally my
IntentService
class which has the stuff I want to do in theonHandleIntent()
method. (I also want to add I had a problem with this class, it was because the constructor wasn't allowed any parameters).
Thanks again.