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
BroadcastReceiverwhich 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
BroadcastReceiverclass which has code tostartService(). - Finally my
IntentServiceclass 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.
You can check battery level in a service. I guess I would recommend enabling everything (except the battery), then see how long it takes to check/set all of what your app is suppose to. If it is under 50ms, then use a
BroadcastReceiverin any case where battery level is not a concern (for example, time triggers).Then if user enables battery level check, then always use the service.
If running all the checking/setting system settings takes over 50ms, then always use a
Service.I would recommend registering a
BroadcastReceiverwith theBOOT_COMPLETEDaction, so your app will be able to run after a reboot. Also, I do not recommend running a service constantly. I would recommend usingAlarmManagerto run the service every 10 minutes or so. Reason being, if your service takes 15 seconds to do what it needs, then it will waste battery for it to keep running all the time.Also, if you are going to use a mix of Service/BroadcastReceiver, I would recommend creating a class (call it
Mfor methods). Then inMyou will have a method (saydoSomething()) to do whatever you need. Then in the Service or Receiver, you would just need to callM.doSomething()so you don’t have to change the code twice for every one time you change it.Let me know if I was unclear/confusing or I need to add anything, and I’ll do my best to elaborate.
EDIT:
You would need to use
AlarmManagerto set the alarms,Calendarto get and set specific times/dates. https://stackoverflow.com/a/7342724/802469 will help you with this process. To keep the device’s CPU awake and keep it from stopping your service, you would need to usePowerManager. For other things that you haven’t specified, you should be able to find by Googling or by looking in the docs.WifiManagerfor changing wifi setting.