0

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 :

  1. 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 ?
  2. Is there a way to schedule a particular function to be called based on Time in Android ?
  3. Is there a way to call a particular function when the battery level of the device changes or the device is charging or unplugged ?

1 Answers1

0

Here's your answer point wise:

1) You can use LocationManager to get the location of your device.

LocationManager locationManager = (LocationManager) 

this.getSystemService(Context.LOCATION_SERVICE);



LocationListener locationListener = new LocationListener() 
{


 public void onLocationChanged(Location location) 
 {

      makeUseOfNewLocation(location);  //here you get the new location
  }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };


locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

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) {

// we know mobiletuts updates at right around 1130 GMT.
// let's grab new stuff at around 11:45 GMT, inexactly
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 11);
updateTime.set(Calendar.MINUTE, 45);

Intent downloader = new Intent(context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
        0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getActivity().getSystemService(
        Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        updateTime.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, recurringDownload);

}

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:

private BroadcastReceiver mBatInfoReceiver 

= new BroadcastReceiver(){

    @Override
    public void onReceive(Context arg0, Intent intent) {
        int level = intent.getIntExtra("level", 0);
        // do something...
    }
}

    registerReceiver(this.mBatInfoReceiver, 
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

For plugin or charging use following code:

// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;


// How are we charging?

int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;

I Hope All this will help you...

Akshay
  • 1,137
  • 1
  • 10
  • 12