I am creating an Android application that stores a couple of conditions like Location, Time, Contact and Battery level. What I intend for my application to do is to store these conditions along with some phone setting changes that the user wants to activate if these conditions are met. It would be better if I put my questions in a list :
- How do I check the Location of the device at regular intervals ? Is there a listener service (like for the Contacts option the TelephonyManager can be used) that can be used to listen for a callback in case the device location changed ?
- Is there a way to schedule a particular function to be called based on Time in Android ?
- Is there a way to call a particular function when the battery level of the device changes or the device is charging or unplugged ?
Here’s your answer point wise:
1) You can use LocationManager to get the location of your device.
You can also use GeoCoder class to exactly point out the address using the longitude and latitude received from locationManager.
Detail tutorial here – LocationManager
2) You can schedule the particular function to be called – using BroadcastReceiver and AlarmManager service.
private void setRecurringAlarm(Context context) {
}
Detail tutorial BroadCastReceiver and AlarmManager
So, if you want you can recurrently check for the present location of device ( using LocationManager as shown in point 1) inside OnReceive of your BroadCastReceiver.
3) There is a BroadCastReceiver to catch when BatteryLevel changes as below:
For plugin or charging use following code:
I Hope All this will help you…