简体   繁体   中英

Android requestLocationUpdates using PendingIntent with BroadcastReceiver

How do I use

requestLocationUpdates(long minTime, float minDistance, Criteria criteria,
                                                           PendingIntent intent) 

In BroadcastReciver so that I can keep getting GPS coordinates.

Do I have to create a separate class for the LocationListener ?

Goal of my project is when I receive BOOT_COMPLETED to start getting GPS lats and longs periodically.

Code I tried is :

public class MobileViaNetReceiver extends BroadcastReceiver {

    LocationManager locmgr = null;
    String android_id;
    DbAdapter_GPS db;

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            startGPS(context);
        } else {
            Log.i("MobileViaNetReceiver", "Received unexpected intent "
                + intent.toString());
        }
    }

    public void startGPS(Context context) {
        Toast.makeText(context, "Waiting for location...", Toast.LENGTH_SHORT)
                .show();
        db = new DbAdapter_GPS(context);
        db.open();
        android_id = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);
        Log.i("MobileViaNetReceiver", "Android id is _ _ _ _ _ _" + android_id);
        locmgr = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5,
                onLocationChange);
    }

    LocationListener onLocationChange = new LocationListener() {

        public void onLocationChanged(Location loc) {
            // sets and displays the lat/long when a location is provided
            Log.i("MobileViaNetReceiver", "In onLocationChanged .....");
            String latlong = "Lat: " + loc.getLatitude() + " Long: "
                + loc.getLongitude();
            // Toast.makeText(this, latlong, Toast.LENGTH_SHORT).show();
            Log.i("MobileViaNetReceiver", latlong);
            try {
                db.insertGPSCoordinates(android_id,
                        Double.toString(loc.getLatitude()),
                        Double.toString(loc.getLongitude()));
            } catch (Exception e) {
                Log.i("MobileViaNetReceiver",
                        "db error catch _ _ _ _ " + e.getMessage());
            }
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };    
    //pauses listener while app is inactive
    /*@Override
    public void onPause() {
        super.onPause();
        locmgr.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, onLocationChange);
     }*/
}

There are two ways of doing this:

  1. Use the method that you are and register a BroadcastReceiver which has an intent filter which matches the Intent that is held within your PendingIntent (2.3+) or, if you are only interested in a single location provider, requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent) (1.5+) instead.
  2. Register a LocaltionListener using the requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) method of LocationManager .

I think that you are getting a little confused because you can handle the location update using either a BroadcastReceiver or a LocationListener - you don't need both. The method of registering for updates is very similar, but how you receive them is really very different.

A BroadcastReceiver will allow your app / service to be woken even if it is not currently running. Shutting down your service when it is not running will significantly reduce the impact that you have on your users' batteries, and minimise the chance of a Task Killer app from terminating your service.

Whereas a LocationListener will require you to keep your service running otherwise your LocationListener will die when your service shuts down. You risk Task Killer apps killing your service with extreme prejudice if you use this approach.

From your question, I suspect that you need to use the BroadcastReceiver method .

public class MobileViaNetReceiver extends BroadcastReceiver {

    private static final String TAG = "MobileViaNetReceiver"; // please

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
            Log.i(TAG, "Boot : registered for location updates");
            LocationManager lm = (LocationManager) context
                                .getSystemService(Context.LOCATION_SERVICE);
            Intent intent = new Intent(context, this.getClass());
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                                        PendingIntent.FLAG_UPDATE_CURRENT);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,5,pi);
        } else {
            String locationKey = LocationManager.KEY_LOCATION_CHANGED;
            if (intent.hasExtra(locationKey)) {
                Location loc = (Location) intent.getExtras().get(locationKey);
                Log.i(TAG, "Location Received");
                try {
                    DbAdapter_GPS db = new DbAdapter_GPS(context);//what's this
                    db.open();
                    String android_id = Secure.getString(
                        context.getContentResolver(), Secure.ANDROID_ID);
                    Log.i(TAG, "Android id is :" + android_id);
                    db.insertGPSCoordinates(android_id,
                        Double.toString(loc.getLatitude()),
                        Double.toString(loc.getLongitude()));
                } catch (Exception e) { // NEVER catch generic "Exception"
                    Log.i(TAG, "db error catch :" + e.getMessage());
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM