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);
}*/
}