My app is used, amongst other features, for tracking drivers doing delivery work, so we need to track the driver's location constantly. For that, we use a Service that runs in the background and saves the coordinates internally before we send them to a server.
The problem is, as is expected, the more precise we need to get, the more the battery drain increases. To the point where Google Play Console Vitals is showing a way above the average number for wake ups like this:
walarm:com.google.android.location.ALARM_WAKEUP_LOCATOR*
I want to know if I'm doing something wrong or if the only way I can improve the code is by doing minor tweaks.
public class LocationService extends Service implements LocationListener {
private Integer gpsFreqInMillis = 1000 * 10; //10 seconds
private Integer gpsFreqInDistance = 10; //10 meters
...
public void startUpdatingLocation() {
...
final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
...
#Override
public void onLocationChanged(final Location newLocation) {
//saves location in internal database
}
}
Is there a way I can track a precise location without killing my user's battery? I know I can change the min distance and min time values, but 10m/10s is the most precise option we offer.
Only way around to fix this is to optimize the request callbacks by updating these values in your code to
private Integer gpsFreqInMillis = 1000 * 40; //10 seconds
private Integer gpsFreqInDistance = 20; //10 meters
Related
hi guys I have created a background service which gets the person location and if the distance between the device to the destination is smaller i request location updates more often and I am using the removeUpdates(LocationListener) method... the problem is that i noticed that the listener keeps getting updates and the more the loop is going it keeps get more and more updates. does anyone have any idea why this method doesn't work?
Here is my method for using the new location.
void makeUseOfNewLocation(Location location){
manager.removeUpdates(listener);
mLocation = location;
currentLat = location.getLatitude();
currentLong = location.getLongitude();
Location.distanceBetween(currentLat, currentLong, gateLat, gateLong, results);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentDistanceFromDestination = results[0];
Log.d("Current Location", "Lat:"+currentLat+" Long:"+currentLong);
Log.d("Destination Distance", currentDistanceFromDestination+"");
if(currentDistanceFromDestination<3000){
if(currentDistanceFromDestination<50){
Log.d("50m closer", "Calling");
startActivity(callIntent);
stopSelf();
}
if(currentDistanceFromDestination<800){
Log.d("800m closer", "Started listening every 10 seconds.");
Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 ,90 , listener);
}else{
Log.d("3000m closer", "Started listening every 25 seconds");
Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25 * 1000, 300, listener);
}
}
}
Do you initiate the value of your listener between calls?
it might not be the same object so it cannot remove updates to it.
In that case the old listener will still be getting updates with you unable to remove them.
i dont understand why locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return the location null. I gave all permission but its reutning null.
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
I had this exact same problem. It was because my device was not storing a last known location. I simply went on to Google Maps and pinpointed my location with GPS, then a value was returned for getLastKnownLocation()
You can request location updates like this
mLocationManager.requestLocationUpdates(myProvider, 0, 0, locationListener);
then on the first callback in locationListener.onLocationChanged set your coordinates. Just don't forget to call mLocationManager.removeUpdates(locationListener)
Accoding to the documentation, it returns null, if the device is not aware of the last known location. Probably the GPS can not locate you. It takes about a minute or more, anyway. So try to go outside, under the clear sky, away from tall buildings, and wait until GPS can locate you.
I used this method for get location i think it will help you
private void startReceivingLocationUpdates() {
if (mLocationManager == null) {
mLocationManager = (android.location.LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
}
if (mLocationManager != null) {
try {
mLocationManager.requestLocationUpdates(
android.location.LocationManager.NETWORK_PROVIDER,
1000,
0F,
mLocationListeners[1]);
}
catch (SecurityException ex)
{
Log.i(TAG, "fail to request location update, ignore", ex);
}
catch (IllegalArgumentException ex)
{
Log.d(TAG, "provider does not exist " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
android.location.LocationManager.GPS_PROVIDER,
1000,
0F,
mLocationListeners[0]);
if (mListener != null) mListener.showGpsOnScreenIndicator(false);
}
catch (SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex); }
catch (IllegalArgumentException ex) {
Log.d(TAG, "provider does not exist " + ex.getMessage()); }
Log.d(TAG, "startReceivingLocationUpdates");
}
}
When you use GPS as provider then it gives your result with in 1 to 2 mnts so you have to contentiously check for that when get location stop and Network Provider gives you immediate locationwhen you request. So you dont get immediate Location lat lon in GPS provider.
GPS take 1 to 2 only first time then after it will give location you on call...
Below is the code I used for getting longitude and latitude within a timertask.
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
try {
gpt = new GPSTracker(MainActivity.this);
System.out.println("Internet is present");
setContentView(R.layout.tracklayout);
TimerTask myTask = new TimerTask() {
#Override
public void run() {
try {
Log.d("flow", "" + "task()");
gpt = new GPSTracker(MainActivity.this);
lc = gpt.getLocation();
if (gpt.canGetLocation()) {
double latitude = gpt.getLatitude();
double longitude = gpt.getLongitude();
Log.d("latitude", "" + latitude);
Log.d("longitude", "" + longitude);
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Reached.. ",Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
Timer myTimer = new Timer();
myTimer.schedule(myTask, 3000, 30000);
} catch (Exception e) {
e.printStackTrace();
}
} else {
alert.showAlertDialog(MainActivity.this,"Mobile Data is Off","Please Turn On mobile data to proceed", false);
}
}
});
Latitude and longitude are always getting zero even I manually entered co-ordinates through DDMS. There no problem with the code of GPSTracker. Outside timertask it worked fine. What is the problem with this code. Anyone please help
As you are using GPSTracker ,If GPS is not working than you can not have lat and long values for sure, you can't get an accurate location. It may take few minutes or seconds because it depends on lot of constraints like yours position inside building, weather , you device hardware quality etc as you are using sattelite to have locations, So we can say thatit depends on the device and the environment settings ( weather, Location under the sky/inside or outside building,device hardware quality etc as an example).
Why you are not using NETWORK_PROVIDER ?? if you can...
If you want to get a coarse location faster with program than you can get the location using NETWORK_PROVIDER, which isn't that accurate but can get you the location very faster.
Visit for examples
http://www.vogella.com/tutorials/AndroidLocationAPI/article.html
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
http://developer.android.com/guide/topics/location/strategies.html
Don't use timertask. Implement LocationListener with that activity. Write the codes in the timertask to OnLocation changed. Not needed to run in paricular time interval because we get the same data if we are in same location.
This is my code to register network provider
netlocationListener = new MynetLocationListener();
locationMangaer.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,interval,mttravel,
netlocationListener);
/*----------Listener class to get coordinates ------------- */
private class MynetLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Toast.makeText(getBaseContext(), "accuracy & network provider"+loc.getAccuracy(),Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "location changed", Toast.LENGTH_LONG).show();
CharSequence time1=android.text.format.DateFormat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());
.LENGTH_LONG).show();
String date =time1.toString();
float tsp= 0;
Toast.makeText(getBaseContext(),"Location changed : Lat: " + loc.getLatitude()
+ " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
String longitude = Double.toString(loc.getLongitude());
Log.v(TAG, longitude);
String latitude =Double.toString( loc.getLatitude());
Log.v(TAG, latitude);
/*----------to get City-Name from coordinates ------------- */
String cityName=null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (addresses.size() > 0)
// System.out.println(addresses.get(0).getLocality());
cityName=addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
String accuracy=Float.toString( loc.getAccuracy());
String s = longitude+"\n"+latitude +"\n\nMy Currrent City is: "+cityName + "accuracy"
+ accuracy + "speed"+loc.getSpeed();
}
It shows true -
Toast.makeText(getBaseContext(), Boolean.toString(locationMangaer.isProviderEnabled(LocationManager.NETWORK_PROVIDER)), Toast.LENGTH_LONG).show();
If I change provider to GPS then it works perfectly but I need to get location from network provider. I checked working with internet, wifi but still no result.
Its not even entering the onlocationchanged().
For network based location, they are a lot less inaccurate than GPS, meaning you need to move the device a much larger distance to allow the signal to hook on to another network tower before
onLocationChanged() is called. Also not all network towers provide location.
I don't see where you're initializing the location manager. I'd guess you're not getting messages using network location because you're not moving enough to trigger an location change. Network position is not very accurate. I've seen network positioning be over 100ft off, and would hazard a guess that your device has a hundred foot "error radius" that you will have to move outside of to get a location update.
I've cut-n-pasted my code below, you're mileage may vary.
// see if this line gives you what you want
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// get location via network
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// get location via GPS
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
I've asked a question about Commonware's location poller before and got it to work before. Its still working but there is a problem.
Yesterday, (after a bit of experimenting and code studying lol) I realized that in order to get location using both options (prefer GPS, but if not available use Network) I had to pass the extra as 'BOTH'.
After doing that however, the LocationPollerService doesn't FORCE the GPS to locate me. Let me describe the issue below.
Suppose I have my GPS on, and another app actively using the GPS and maintaining a lock. Now, if I use the LocationPollerService, it will return me the value from GPS. However, when my GPS in on, but isn't locked, LocationPollerService doesn't attempt a lock but merely moves on to Network-based Location which isn't what I need when I have GPS enabled.
I would've added code here but I believe this is question specific to cwac-locpoll and the code is already online.
Here's the link:
Commonware's Location Poller
My question? How do I force the service to attempt a location fix when GPS is on?
Update
Here's where I think it looks for GPS location but doesn't actually attempt one. If you could help, that would be great.
#Override protected void onPreExecute()
{
// Added in enabled check to even check if there is a provider
// enabled.
if (provider.equalsIgnoreCase("BOTH"))
{
if (!locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
Log.i(TAG, "Both providers are disabled");
// There is no provider so fail with the LKL if possible
Intent toBroadcast = new Intent(intentTemplate);
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR, "Location Provider disabled!");
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR_PROVIDER_DISABLED, true);
Location lastLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLoc == null)
lastLoc = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
toBroadcast.putExtra(LocationPoller.EXTRA_LASTKNOWN, lastLoc);
sendBroadcast(toBroadcast);
quit();
return;
}
}
else if (!locMgr.isProviderEnabled(provider))
{
Log.i(TAG, provider + " is disabled");
// There is no provider so fail with the LKL if possible
Intent toBroadcast = new Intent(intentTemplate);
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR, "Location Provider disabled!");
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR_PROVIDER_DISABLED, true);
toBroadcast.putExtra(LocationPoller.EXTRA_LASTKNOWN, locMgr.getLastKnownLocation(provider));
sendBroadcast(toBroadcast);
quit();
return;
}
onTimeout = new Runnable()
{
public void run()
{
Intent toBroadcast = new Intent(intentTemplate);
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR, "Timeout!");
toBroadcast.putExtra(LocationPoller.EXTRA_ERROR_PROVIDER_DISABLED, false);
Location lastLoc = null;
if (provider.equalsIgnoreCase("BOTH"))
{
Log.i(TAG, "Both provider timed out");
lastLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLoc == null)
lastLoc = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else
{
Log.i(TAG, provider + " timed out");
lastLoc = locMgr.getLastKnownLocation(provider);
}
toBroadcast.putExtra(LocationPoller.EXTRA_LASTKNOWN, lastLoc);
sendBroadcast(toBroadcast);
quit();
}
};
handler.postDelayed(onTimeout, TIMEOUT);
try
{
Log.e(TAG, "Provider: " + provider);
if (provider.equalsIgnoreCase("BOTH"))
{
Log.i(TAG, "Adding both providers");
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
else
{
locMgr.requestLocationUpdates(provider, 0, 0, listener);
}
}
catch (IllegalArgumentException e)
{
// see http://code.google.com/p/android/issues/detail?id=21237
Log.w(TAG, "Exception requesting updates -- may be emulator issue", e);
quit();
}
}