Current Location Update

Android offers so many API to find the location till froyo version we had only request location updates and it will eat up the battery with continuous updates.
There is a new API introduced in 2.3 to obtain single update of GPS.

An simple example to obtain location (single update) is as below:

  1. Register a receiver to listen to intent – “SINGLE_LOCATION_UPDATE_ACTION”
    registerReceiver(singleUpdateReceiver,new IntentFilter(SINGLE_LOCATION_UPDATE_ACTION));
  2. Get the instance of location manger service
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  3. Create criteria instance and set accuracy as required
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_LOW);
  4. Create a pendingIntent instance. Pendingintent is registered to receive broadcast with action “SINGLE_LOCATION_UPDATE_ACTION” from the location manager
    Intent updateIntent = new Intent(SINGLE_LOCATION_UPDATE_ACTION);
    singleUpatePI = PendingIntent.getBroadcast(this, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  5. Finally, request for single update
    locationManager.requestSingleUpdate(criteria, singleUpatePI);
  6. We need to declare the receiver with onReceive function as below
    protected BroadcastReceiver singleUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG,"onReceive");
    //unregister the receiver so that the application does not keep listening the broadcast even after the broadcast is received.
            context.unregisterReceiver(singleUpdateReceiver);

    // get the location from the intent send in broadcast using the key - this step is very very important
            String key = LocationManager.KEY_LOCATION_CHANGED;
            Location location = (Location)intent.getExtras().get(key);

    // Call the function required
            if (location != null) {
                onLocationChanged(location);
            }

    // finally remove the updates for the pending intent
            locationManager.removeUpdates(singleUpatePI);
        }
    };

PendingIntent Explanation

Why Pending Intent?

They are used when the control moves onto another application to perform certain activity like -> when we need to use alarm manager or NFC Service

Usage of Alarm Manager – when we need a certain action to be performed at certain point of time

Getting an instance of PendingIntent

    There are three main ways

  1. getService() – launches a service
    Example:
    Intent lIntent = new Intent(this,AlertService.class);
    PendingIntent refreshIntent = PendingIntent.getService(this,0,lIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    Explanation:
        This lauches AlertService.
    Parameter Explanation:
        this – current context
        0 – any integer value – unique for identification
        lIntent – intent to be launched as a result
        FLAG_UPDATE_CURRENT – which indicates if there is an pending intent already, delete the prev and include this.
  2. getActivity() – launches the activity
    Example:
    Intent lIntent = new Intent(this,AlertActivity.class);
    PendingIntent refreshIntent = PendingIntent.getActivity(this,0,lIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Explanation:
        This lauches AlertActivity.
  3. getBroadcast() – receives a broadcast
    Example:
    Intent lIntent = new Intent(APP_UPDATE);
    PendingIntent refreshIntent = PendingIntent.getBroadcast(this,0,lIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    Explanation:
        This receives the broadcast – u need to register the broadcast either in the manifest file or activity launched.
    Parameter Explanation:
        APP_UPDATE – string value – denotes the action for broadcast