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:
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.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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With