Android requestLocationUpdates without minDistance - java

I have a need to get the gps location every 20 seconds or so, but ignoring the need to have the user move a certain number of meters, I tried setting the minDistance to zero but still no luck, its only when I manually send the location using the location control from DDMS that this gets activated and the location gets updated.
Note - This is run and tested from the emulator and not a real device, for now at least
private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journey);
Bundle dataBundle = this.getIntent().getExtras();
try {
driverData = new JSONObject(dataBundle.getString("driverData"));
} catch (JSONException e) {
e.printStackTrace();
}
//Set the UI elements into variables
journeyDescription = (TextView)findViewById(R.id.journeyDescription);
journeyText = (TextView)findViewById(R.id.journeyText);
confirmButton = (Button)findViewById(R.id.btnCheck);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
try{
//Until requests are found keep broadcasting the drivers location and checking for requests
if(!foundRequest){
updateDriverLocationAndCheckForNewDriverRequests(location);
}
//Always keep updating the drivers location
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
}catch(JSONException e){
Toast.makeText(JourneyActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Register the listener with the Location Manager to receive location updates can be either gps provider
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
}
Here is the related code fragment, any ideas on how to trigger the listener without having the user move or me having to send information from the DDMS Location Controls woild be greatly appreciated
Regards,
Milinda

requestLocationUpdates takes milliseconds, not seconds.
You should change:
private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;
to:
private final static int INTERVAL_TIME_SECONDS = 20 * 1000; // 20 seconds
private final static float INTERVAL_DISTANCE_METERS = 0f; // 0 meters

As far as testing on the emulator goes. I think your limited to manually pushing GPS locations or creating a few automated simulators:
Testing GPS in Android

Related

Getting location when the app is closed using fusedLocationProviderClient

I am currently working on a location-based reminder app.
Obviously, the should send a notification when the user is close to a location he set.
For that, I am using a foreground service - which starts when the app starts. It also works when the app is closed, like swiped up from the apps list (not force stopped).
In the service, I am getting the current location and iterating over the location to see which ones I should send a notification for.
Every thing works fine, but for some reason, when the app is closed, the service doesn't enter the onLocationResult:
LocationServices.getFusedLocationProviderClient
(ServiceLocationNotification.this).requestLocationUpdates(locationRequest, new LocationCallback() {
#Override
public void onLocationResult(#NonNull LocationResult locationResult) {
GetCurrentLocationAsyncTaskServiceLocationNotification task =
new GetCurrentLocationAsyncTaskServiceLocationNotification
(ServiceLocationNotification.this,
locationResult,
this);
try
{
currentLatLng = task.execute().get();
}
catch (Exception e)
{
System.out.println("Exception");
e.printStackTrace();
}
}
},
Looper.getMainLooper()); // Use the main thread's looper to receive location updates
(I checked with prints).
GetCurrentLocationAsyncTaskServiceLocationNotification (sorry for the long name) is a class i created to retrieve the current location and get the latLng out of it:
// for the ServiceLocationNotification class.
public class GetCurrentLocationAsyncTaskServiceLocationNotification extends AsyncTask<Void, Void, LatLng>
{
private WeakReference<ServiceLocationNotification> ServiceLocationNotificationWeakReference;
private LocationCallback locationCallback;
private LocationResult locationResult;
GetCurrentLocationAsyncTaskServiceLocationNotification(ServiceLocationNotification activity,
#NonNull LocationResult locationResult,
LocationCallback locationCallback)
{
ServiceLocationNotificationWeakReference = new WeakReference<>(activity);
this.locationResult = locationResult;
this.locationCallback = locationCallback;
}
// .execute().get() gets the return value.
#Override
protected LatLng doInBackground(Void... voids)
{
double latitude;
double longitude;
// get location
LocationServices.getFusedLocationProviderClient(ServiceLocationNotificationWeakReference.get())
.removeLocationUpdates(locationCallback);
// Check if the location result is not null and has at least one location
if (locationResult != null && locationResult.getLocations().size() > 0)
{
// Get the most recent location from the location result
int index = locationResult.getLocations().size() - 1;
latitude = locationResult.getLocations().get(index).getLatitude();
longitude = locationResult.getLocations().get(index).getLongitude();
return new LatLng(latitude, longitude);
}
return null;
}
}
I would appreciate any help understanding why this happens (not entering the onLocationResult function).
Thanks!

Getting longitude and latitude takes a very long time

I am getting the longitude and latitude of my device, but it takes at 30 seconds to a minute to do so. Any suggestions to cut the time down?
public class MainActivity extends Activity
{
public String zipcode;
public double latG;
public double lonG;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.getLastKnownLocation(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
if (location != null)
{
latG = location.getLatitude();
lonG = location.getLongitude();
Toast.makeText(MainActivity.this,
latG + " " + lonG,
Toast.LENGTH_LONG).show();
}
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(latG, lonG, 1);
}
catch (IOException e)
{
Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("Error in getting address information.");
alertDialogBuilder.setCancelable(true);
}
for (Address address : addresses)
{
if(address.getPostalCode() != null)
{
zipcode = address.getPostalCode();
Toast.makeText(MainActivity.this, zipcode, Toast.LENGTH_LONG).show();
break;
}
}
}
}
You are using GPS_PROVIDER for fetching the GPS data. GPS_PROVIDER fetches details directly from the satellite so it takes time for the first time you load this. Moreover GPS_PROVIDER takes more than 30 seconds if your are not below the open sky. GPS_PROVIDER may return NULL when you are inside the office or in basement.
There is an alternative way for this is to use NETWORK_PROVIDER. This provider will give you GPS details based on your current Network state. This will not be much accurate like GPS_PROVIDER but it works faster.
hi you are using GPS PROVIDER which can take some time as it depends on several constraints like yours building position, physical position, weather as gps data is available from the satellite so use a network provider which may faster in yours case please have a look on the given code snippet at
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
Best option is to use Google Play Services and its LocationClient.
http://developer.android.com/google/play-services/location.html
This gives you a provider that automatically picks the best available information from all the provider types and can return the location immediately in some cases using getLastLocation()
try to run it in device rather than running it in the emulator.
try this
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
It will give the response what ever the service is available.Even you can place your priority
Use this code to fetch faster,
String provider;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if (location != null)
{
onLocationChanged(location);
//your remaining code
}

Mock GPS location issue

I'm developing an APP that get user specified latitude,longitude, and altitude, then fake this GPS location on the phone, and show that I am at that location in google map. I have the required permission on manifest file and mocked location is enabled in developer settings.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//lm.clearTestProviderEnabled(mocLocationProvider);
lm.addTestProvider(mocLocationProvider, false, false, false, false, false, false, false, 0, 10);
lm.setTestProviderEnabled(mocLocationProvider, true);
mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(Integer.parseInt(latitude.getText().toString())); // double
mockLocation.setLongitude(Integer.parseInt(longitude.getText().toString()));
mockLocation.setAltitude(Integer.parseInt(altitude.getText().toString()));
mockLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation( mocLocationProvider, mockLocation);
But looks like my GPS location is not changed at all on google map, what is the problem?
Update: I just installed an app called "fake GPS location" on my phone and that app works fine, but I still don't know what's wrong with my code, but I think mine is a formal way to achieve this.
Update #2: Although some of similar applications can run on my phone, but I found some exceptions, http://www.cowlumbus.nl/forum/MockGpsProvider.zip, this app is not working on my phone. can someone help me with this issue? millions of thanks! I'm not getting any error message when setting the location each time.
Update#3 : I noticed that this app is fairly old, so it does not run on 4.1. if so, how to do the same thing in the new version? my phone is samsung galaxy s3, hope it helps.
Update#4: for your info, the code from app in my update#2 is:
package nl.cowlumbus.android.mockgps;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MockGpsProviderActivity extends Activity implements LocationListener {
public static final String LOG_TAG = "MockGpsProviderActivity";
private static final String MOCK_GPS_PROVIDER_INDEX = "GpsMockProviderIndex";
private MockGpsProvider mMockGpsProviderTask = null;
private Integer mMockGpsProviderIndex = 0;
/** Called when the activity is first created. */
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Use saved instance state if necessary. */
if(savedInstanceState instanceof Bundle) {
/** Let's find out where we were. */
mMockGpsProviderIndex = savedInstanceState.getInt(MOCK_GPS_PROVIDER_INDEX, 0);
}
/** Setup GPS. */
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
// use real GPS provider if enabled on the device
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else if(!locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
// otherwise enable the mock GPS provider
locationManager.addTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER, false, false,
false, false, true, false, false, 0, 5);
locationManager.setTestProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER, true);
}
if(locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
locationManager.requestLocationUpdates(MockGpsProvider.GPS_MOCK_PROVIDER, 0, 0, this);
/** Load mock GPS data from file and create mock GPS provider. */
try {
// create a list of Strings that can dynamically grow
List<String> data = new ArrayList<String>();
/** read a CSV file containing WGS84 coordinates from the 'assets' folder
* (The website http://www.gpsies.com offers downloadable tracks. Select
* a track and download it as a CSV file. Then add it to your assets folder.)
*/
InputStream is = getAssets().open("mock_gps_data.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
// add each line in the file to the list
String line = null;
while ((line = reader.readLine()) != null) {
data.add(line);
}
// convert to a simple array so we can pass it to the AsyncTask
String[] coordinates = new String[data.size()];
data.toArray(coordinates);
// create new AsyncTask and pass the list of GPS coordinates
mMockGpsProviderTask = new MockGpsProvider();
mMockGpsProviderTask.execute(coordinates);
}
catch (Exception e) {}
}
}
#Override
public void onDestroy() {
super.onDestroy();
// stop the mock GPS provider by calling the 'cancel(true)' method
try {
mMockGpsProviderTask.cancel(true);
mMockGpsProviderTask = null;
}
catch (Exception e) {}
// remove it from the location manager
try {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER);
}
catch (Exception e) {}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// store where we are before closing the app, so we can skip to the location right away when restarting
savedInstanceState.putInt(MOCK_GPS_PROVIDER_INDEX, mMockGpsProviderIndex);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onLocationChanged(Location location) {
// show the received location in the view
TextView view = (TextView) findViewById(R.id.text);
view.setText( "index:" + mMockGpsProviderIndex
+ "\nlongitude:" + location.getLongitude()
+ "\nlatitude:" + location.getLatitude()
+ "\naltitude:" + location.getAltitude() );
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
/** Define a mock GPS provider as an asynchronous task of this Activity. */
private class MockGpsProvider extends AsyncTask<String, Integer, Void> {
public static final String LOG_TAG = "GpsMockProvider";
public static final String GPS_MOCK_PROVIDER = "GpsMockProvider";
/** Keeps track of the currently processed coordinate. */
public Integer index = 0;
#Override
protected Void doInBackground(String... data) {
// process data
for (String str : data) {
// skip data if needed (see the Activity's savedInstanceState functionality)
if(index < mMockGpsProviderIndex) {
index++;
continue;
}
// let UI Thread know which coordinate we are processing
publishProgress(index);
// retrieve data from the current line of text
Double latitude = null;
Double longitude = null;
Double altitude= null;
try {
String[] parts = str.split(",");
latitude = Double.valueOf(parts[0]);
longitude = Double.valueOf(parts[1]);
altitude = Double.valueOf(parts[2]);
}
catch(NullPointerException e) { break; } // no data available
catch(Exception e) { continue; } // empty or invalid line
// translate to actual GPS location
Location location = new Location(GPS_MOCK_PROVIDER);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(altitude);
location.setTime(System.currentTimeMillis());
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAccuracy(16F);
location.setAltitude(0D);
location.setTime(System.currentTimeMillis());
location.setBearing(0F);
// show debug message in log
Log.d(LOG_TAG, location.toString());
// provide the new location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.setTestProviderLocation(GPS_MOCK_PROVIDER, location);
// sleep for a while before providing next location
try {
Thread.sleep(200);
// gracefully handle Thread interruption (important!)
if(Thread.currentThread().isInterrupted())
throw new InterruptedException("");
} catch (InterruptedException e) {
break;
}
// keep track of processed locations
index++;
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
Log.d(LOG_TAG, "onProgressUpdate():"+values[0]);
mMockGpsProviderIndex = values[0];
}
}
}
Problem solved: I added the following code to set my current location and it can successfully show up in google map application.
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setBearing(bearing);
location.setSpeed(speed);
location.setAltitude(altitude);
location.setTime(new Date().getTime());
location.setProvider(LocationManager.GPS_PROVIDER);
location.setAccuracy(1);
Conclusion: If you want to use mock location service in the new version of android, you have to set every attribute by yourself.

requestLocationUpdates generates location by wrong time

I'm trying to get locations from 2 different providers: network and GPS.
I'm using minTime 1 minute and minDistance 300 meters in requestLocationUpdates. With those parameters I don't expect to ever get more than one update per minute (per provider). The problem is, I am getting updates more frequently than that (more than 1 per minute). Why?
Here is some code to demonstrate:
mLocationManager.removeUpdates(listener);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);
Here is the listener:
private final LocationListener listener = new LocationListener()
{
#Override
public void onLocationChanged(Location location)
{
if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER))
updateUILocation(location,LocationService.this.gpsLocation);
else
updateUILocation(LocationService.this.networkLocation, location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
UpdateUILocation:
private void updateUILocation(Location networkLocation,Location gpsLocation)
{
Location location = null;
if(gpsLocation == null || gpsLocation.getAccuracy() > 800)
{
if(!(networkLocation == null || networkLocation.getAccuracy() > 800))
location = networkLocation;
}
else
if(networkLocation == null)
{
if(gpsLocation.getAccuracy() < 800)
location = gpsLocation;
}
else
if(gpsLocation.getAccuracy() < networkLocation.getAccuracy() && gpsLocation.getAccuracy() < 500)
location = gpsLocation;
else
if(networkLocation.getAccuracy() < 800)
location = networkLocation;
if(location!=null)
{
if (mGeocoderAvailable)
doReverseGeocoding(location);
}
// Bypass reverse-geocoding only if the Geocoder service is available on the device.
}
The doReverseGeocoding turn the location into text and call the handler:
mHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == UPDATE_ADDRESS) // msg.what == 1
{
LocationService.this.address = (String) msg.obj;
new SendLocation(LocationService.this.id,(String)msg.obj); //send the location to the db
LocationService.this.gpsLocation = null; //reset gps value
LocationService.this.networkLocation = null; //reset network value
}
}
};
I tested this application while driving (which means that the minDistance parameter is not a factor) and I received more than 1 location update per minute.
Here are the locations I received while testing (please ignore the locations since it's in hebrew, just look for the time): http://imrip.interhost.co.il/
"The minTime is 1 minute and the minDistance is 300 meter. With those
parameters I never should get 2 locations in less then 1 minute..."
That's simply not true, not pre-JellyBean anyway.
http://developer.android.com/reference/android/location/LocationManager.html
Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.
Pre-JB you can get updates MORE FREQUENTLY than your min time specifies, especially if GPS reception is sketchy. See this answer for more details:
requestLocationUpdates interval in Android

Location returns NULL

I am writing an Android app that returns longitude and latitude, however location holds a Null value.
Please see if you can see why, Its been bugging me all day. Code below:
public class example extends Activity {
public static double latitude;
public static double longitude;
LocationManager lm;
LocationListener ll;
Location location;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
//location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
int index1 = provider.indexOf("gps");
if(index1 < 0)
// gps not enabled, call msgbox
showMsgBox("GPS is off", "GPS is off. Turn it on?", "Turn on");
else
areWeThereYet(); }});
}
private void areWeThereYet()
{
if(weAreThere())
{
toastMsg("in correct place");
}
else if(location!=null)
toastMsg("not there yet");
}
private boolean weAreThere() {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
toastMsg(latitude + " " + longitude);
return inCorrectPlace(question);
}
else
{
toastMsg("Location not ready yet");
return false;
}
}
private void toastMsg(String msg) {
Toast toast = Toast.makeText(this, msg, 2000);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
}
}
If the GPS has not ever gotten a location since the device was booted, the location object will be null. One thing you can do is attempt to get a GPS location and a network location, and check to two to see if either of them are good (or which one is better) and use that location.
If you're using the emulator, then see here for advice on setting up the emulator to provide a location. If you're testing it on your device, it may be because you've never had a gps location on it. Try using the Google maps application before testing your app.

Categories

Resources