Recipe 8.3 Monitoring a Location
Your application wants to monitor a particular location, and when the device is near that location you want to perform some actions. This is very useful if you are writing an application that reminds you when you are near places of interest, for example.
Solution
One very cool feature of the LocationManager class is its ability to monitor a specific location. This is achieved using the addProximityAlert() method.
The following code snippet shows how to monitor a particular location so that if the user is within a five-meter radius of that location, your application will fire a PendingIntent object to launch the web browser:
package net.learn2develop.monitorlocation;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
LocationManager lm;
PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---use the LocationManager class to obtain locations data---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onResume() ...
Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.