Android LocationManager requestLocationUpdates changed? - android

My app is working in Ice Cream Sandwich perfectly well, but now I tried it on KitKat and faced some problems.
The app is kind of server I'm running in my old phone and it provides location when requested. In ICS when the location is requested the GPS icon starts blinking and soon the app receives location update and sends it forward. But now with KitKat the GPS icon does not start blinking when location is requested. The app gives 60s time for finding the GPS location, but usually the GPS isn't even activated during this time. Still now and then the GPS suddenly activates itself (during the 60s) and the location is provided to my app.
Why the GPS doesn't get activated even my app requests location? As said, my app works with ICS without problems. And I do have required permissions set in my manifest.
public variables:
public static LocationManager mlocManager = null;
public static LocationListener mlocListener_fast = null;
onCreate:
mlocListener_fast = new MyLocationListener();
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Handling user requested command (location request)
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener_fast);
What should I do differently to get the GPS location instantly without waiting for sudden location updates/GPS activation, probably triggered by Android or some other app?? Unfortunately I don't have any other Android device I could try this.
EDIT:
It seems that if the app does not request location at startup, then the location request works every time when requested time after time. But if the location was requested (and received) on startup, then the location request does not work anymore. What can cause that? I use exactly the same line (the same location mgr and the same location listener) for location request on startup and later if requested.
Even if the location listener used in startup is different than the one used later, the location request does not work anymore. Tried even initialize the location mngr again just before requesting the location again and it did not help. What's with this??
EDIT2:
It just seems that with KitKat it's not possible to request multiple location requests. I used to have several location listeners for different purposes. For example one for updating location once per hour and another for getting location instantly (user requested update). Now it seems that if I have the 1/60min location listener running as normal, then KitKat location manager fails to handle the instant location requests. Have anyone faced this issue? Would be good to know which Android versions have this issue.

Workaround for this issue is to use only one LocationManager and one LocationListener. If your app has needs for different kind of simultaneous location requests (with different parameters), then you need to implement a "location request handler" which decides which parameters should be used for the location request i.e. which parameters have the tightest requirements for location.
Here is a simple example code that explains the idea of "location request handler":
class LR {
long lock_min_time; // defined in set_lock_lr before using
float lock_min_dist;
boolean lock_active = false;
long idle_min_time = 3600000; // 1 per hour
float idle_min_dist = 200;
boolean idle_active = true;
long fast_min_time = 0;
float fast_min_dist = 0;
boolean fast_active = false;
//constructor
public LR()
{}
public void set_lock_lr(long min_time, float min_dist, boolean active)
{
lock_active = active;
lock_min_dist = min_dist;
lock_min_time = min_time;
System.out.println("LR lock set: "+min_time+", "+min_dist+", "+active);
update_location_request();
}
public void set_idle_lr(boolean active)
{
idle_active = active;
System.out.println("LR idle set: "+active);
update_location_request();
}
public void set_fast_lr(boolean active)
{
fast_active = active;
System.out.println("LR fast set: "+active);
update_location_request();
}
private void update_location_request()
{
// Remove current location request
mlocManager_basic.removeUpdates(mlocListener_basic);
if(fast_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, fast_min_time, fast_min_dist, mlocListener_basic);
System.out.println("LR: fast_active");
}
else if(lock_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, lock_min_time, lock_min_dist, mlocListener_basic);
System.out.println("LR: lock_active");
}
else if(idle_active) // only idle updates
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, idle_min_time, idle_min_dist, mlocListener_basic);
System.out.println("LR: idle_active");
}
}
}

Related

Android get high resolution location info on button press

I have read the Android Docs, FusedLocationProvider vs LocationManager; perused the dizzying array of questions and answers around this topic here in stackoverflow; and developed many tests with poor results so far. Why is this so darned confusing and hard to grasp?
I have an app that needs to get a hi-res Location object (lat/long/alt/accuracy/etc) when the user performs an action in the app; let's say they press a button. What is the best way to do this?
I have used the fusedLocationProviderClient.getLastLocation().addOnSuccessListener() and get wildly mixed results.
I have used locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER). If I start the GPS Status app on my Galaxy S9 then this produces quite wonderful results. But if that is not running, then the results are worthless.
What am I missing here? Everyone likes to point to this Doc site or that Example site that is mostly worthless and doesn't really answer this specific question. I have wasted hours pouring over those sites that simply don't answer this question. Please, just sum up the general algorithm that should be used here and the calls to make. That is all I need.
I want to be able to walk around in my yard (10 meters here and there) and press the button and have the app show the lat/long/accuracy/altitude/distance-from-last-location and have it be correct every time within a certain level of accuracy. What do I have to do? I need hi-res accuracy, but the ability to notify the user of accuracy less than say 100ft, and still obtain the best accuracy possible even if it has an error of 400ft.
You are missing how GPS receivers work.
When there is no app using precise location, all smartphones turn off the GPS receiver to conserve battery power.
Even if you selected location services to be on (in settings), you will notice in the notification bar the icon for GPS use is only present when an app is active, like Google Maps or GPS test app.
Once the receiver is turned on (because some app needs it), it takes some time before a "fix" - accurate location measurement is available.
How long it will take to get a fix depends on several things, including environmental conditions, your phone type, time and distance since last accurate fix, etc.
It may take anywhere from several seconds to sever minutes.
So, what you should do, is subscribe to location as soon as your app is opened, and request to receive it as frequently as possible.
Then, enable the button only once you have good accuracy, and when the button is pressed, show the latest result.
You should probably also display some spinner or message to the user while waiting for accurate fix so the user knows your app is not stuck.
Edit: by "subscribe" I mean register the necessary callback so your app will receive the location from the system when it is ready.
How to do this, depends on which API you choose.
There is no error in the google docs.
If you choose to use fused location, you will need to do the following:
Create a location request object and set priority to PRIORITY_HIGH_ACCURACY, also setInterval and setFastestInterval to 1000 (1 second) to get the best accuracy.
Get a FusedLocationProviderClient object from LocationServices
Use the client to register a callback to your app
There are code examples here:
https://developer.android.com/training/location/request-updates
In the callback function in your app you can check the accuracy, and if it is good enough for you enable the button and save the location so you can display it to the user when they click the button.
Ok - this seems to work. This general flow seems to be the answer.
Assumptions: you are requesting android.permission.ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION in manifest.
Just sample code in the onCreate() function of MainActivity for testing purposes.
check to see if we have ACCESS_FINE_LOCATION permission; if not, request.
get FusedLocationProviderClient
get a start location from getLastLocation(); for purposes of comparison and start of track
define locationCallback() to be called by fusedLocationProvider; all we are interested in is getting the last one in the stack and save to class Field.
define LocationRequest with interval of 5 secs and PRIORITY_HIGH_ACCURACY
check to see if the user device is allowing this; not sure what to do with this other than to notify user if not allowed.
now requestLocationUpdates using the LocationCallback defined above.
when user performs action needing current lat/long (e.g. press button), retrieve class field populated with Location object on last LocationCallback().
I am very open to feedback on this pattern. Hope it helps others (as there is a plethora of questions about this). And would love to hear about any problems with this design or issues that I may encounter.
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
} else {
// getFusedLocationProviderClient
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// getStartLocation
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
StartLocation.set(location);
}
}
}).addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Define LocationCallback
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
LastLocation = locationResult.getLastLocation();
}
}
};
// Now lets request location updates - that is how this must happen
// https://developer.android.com/training/location/change-location-settings
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Attempt to see if requested settings are compatible with user device.
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
// Check to see if location settings are satisfied by user's device settings?
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> locationTask = client.checkLocationSettings(builder.build())
.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
#Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
}
}).addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
}
Toast.makeText(MainActivity.this, "Location Settings Are Not " +
"Correct On This Device", Toast.LENGTH_LONG).show();
}
});
// Request location updates
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
}
}

android requestLocationUpdates failing

I am developing a proximity alert related project. For that whenever I opened my app, I need to get exact readings of where I am. Even though I am following the right coding practices prescribed by android documentation, I am not getting the expected result.
Why there is no alternative command in entire android Geolocation coding for getLastKnownLocation which will give us where we are earlier not now.
I have did one javascript coding in the same lines. There My code is working properly. The Descriptive address and coordinates where my device is working nice there. Those commands getCurrentPosition and watchPosition are givinga beautiful response via their event handler callbacks. Why there is no getCurrentLocation in android geolocation parlance?
Even though I have followed relevant coding practices, MyLocationListener myLocationUpdate which implements LocationListener is not updating my new location when I am moving from one place to another place. I gave MINIMUM_DISTANCE_CHANGE_FOR_UPDATES as 1(in meters) and MINIMUM_TIME_BETWEEN_UPDATES as 1000 (in milliseconds).
I am giving important code snippets below to understand the problem
in onCreate handler of the activity
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
myLocationUpdate = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationUpdate);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Finding Location",Toast.LENGTH_LONG).show();
showCurrentLocation();
}
});
latituteField = (TextView) findViewById(R.id.display_Location);
showCurrentLocation();
in showCurrentLocation function
I am using locationManager.getLastKnownLocation(provider) to retrieving that location.
By using GeoCoder Object and the command geocoder.getFromLocation(latitude, longitude, 1) to get First Address match for the coordinates.
// internal class to handle location cahnge event
private class MyLocationListener implements LocationListener contains all the Overridden functions including public void onLocationChanged(Location location)
But Practically I am getting nothing out of all the application. I have already recorded Time via location.getTime(). It is showing a fixed earlier time, but not the interval i specified.
the problem with getting GPS location is that it isnt available immediately. From my understanding of GPS location provider is that when you request location update, the gpr provider will try to connect to the gps satellites which runs in a separate thread (not entirely sure about it). In the meantime your program is executed normally and there maybe a chance that you wont get any location.
What you can do is use Fused Location Provide which was introduced in this year's IO Event. You can find the tutorial here

Send GPS coordinates to Android emulator using DDMS, telnet or any other means

DDMS is not able to send location to the emulator. I have tried sending just the location from DDMS but still the emulator is not able to receive location. Nothing appears on the DDMS log when I click the Send button.
I tried sending geo fix from telnet which returns OK but doesn't actually update the location, or if it does I can't read it via my application.
The application works properly in the device, is able to capture test location details but not able to capture location data sent to the emulator either via DDMS or telnet.
I am testing on Android 2.2 emulator. Can anyone let me know what is wrong?
My app (below) is written in C# using Mono for Android and may need fixing (I'm a newbie to all things Android so I could have missed something). OnLocationChanged(Location location) just doesn't seem to fire at all, as if the listener isn't properly defined. Any help appreciated.
Note: The first time I run this Activity the LocationManager.GetLastKnownLocation is null but the test provider stuff isn't accessed. When I run it again GetLastKnowLocation is still null but the test provider stuff is accessed and set. Weird.
[Activity(Label = "Location Demo")]
public class LocationActivity : Activity, ILocationListener
{
private TextView _locationText;
private LocationManager _locationManager;
private StringBuilder _builder;
private Geocoder _geocoder;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.LocationActivity);
_geocoder = new Geocoder(this);
_locationText = FindViewById<TextView>(Resource.Id.TextView1);
_locationManager = (LocationManager)GetSystemService(LocationService);
if (_locationManager.GetLastKnownLocation("gps") == null)
{
_locationManager.AddTestProvider("gps", false, false, false, false, false, false, false, 0, 5);
_locationManager.SetTestProviderEnabled("gps", true);
Location loc = new Location("gps");
loc.Latitude = 50;
loc.Longitude = 50;
_locationManager.SetTestProviderLocation("gps", loc);
}
Location lastKnownLocation = _locationManager.GetLastKnownLocation("gps");
if (lastKnownLocation != null)
{
_locationText.Text += string.Format("Last known location, lat: {0}, long: {1}", lastKnownLocation.Latitude, lastKnownLocation.Longitude);
}
else
{
_locationText.Text += string.Format("Last location unknown");
}
_locationManager.RequestLocationUpdates("gps", 5000, 2, this);
}
public void OnLocationChanged(Location location)
{
_locationText.Text += string.Format("Location updated, lat: {0}, long: {1}", location.Latitude, location.Longitude);
}
public void OnProviderDisabled(string provider){}
public void OnProviderEnabled(string provider){}
public void OnStatusChanged(string provider, Android.Locations.Availability availability, Bundle extras){}
}
Kudos to https://stackoverflow.com/users/170333/greg-shackles for getting me this far.
I think the problem may be with how you're calling RequestLocationUpdates(). That third parameter is the minimum distance the device needs to move before you get updates, so you're telling the system to only send updates after the device has moved 2 meters. If it works on a real device, it's probably because you moved more than 6 feet. :)
Try starting with RequestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this). That will start a stream of updates on a real device, but only one when you press 'Send' in DDMS. Once that works, I would work back from there on how often you get updates.
Also, GetLastKnownLocation() is always null when you start the emulator. It's better for devices since it can send you the network location as a starting estimate, or the GPS location if another program was using it recently.
EDIT
It could also be a permissions issue. Normally you need to alter AndroidManifest.xml to get GPS access. The line is
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
See the docs here.
Call
_locationManager.RequestLocationUpdates("gps", 5000, 2, this);
function before doing any location operations, like below:
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.LocationActivity);
_geocoder = new Geocoder(this);
_locationText = FindViewById<TextView>(Resource.Id.TextView1);
_locationManager = (LocationManager)GetSystemService(LocationService);
_locationManager.RequestLocationUpdates("gps", 5000, 2, this); //this will cause updated location to be retrieved from telnet
Note: Its normal that program works after the first run, so that after the first run your application could get the initial updated location from telnet and that will be enough to not throws an exception
Finally resolved this. When the emulator is launched by VS2010 (i.e. F5, start debugging) it does not behave as expected. Launch the emulator externally using AVD.exe, start a virtual device and deploy the app. to it (using F5, start debugging) and everything works fine.
Why starting the emulator from within or outside VS2010 should make any difference is a mystery I am able to live with. Thanks to everyone for their helpful suggestions.
Does your emulated android image have GPS hardware? The description in the emulator should have "hw.gps=yes".
I had the same symptoms before recreating a new image with the right (emulated) hardware. I found a simple web page that displays the current location was handy when debugging the emulation environment.

Android onLocationUpdate not called with GPS_PROVIDER

Hi
I have an Android service using the location manager :
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.i("service","start with GPS"); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
}
Then the location listener :
private LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location aLocation) {
Log.i("location listener", "location updated");
}
... other methods ....
}
In the manifest, the authorization for ACCESS_FINE_LOCATION is set.
Everything works ok both in simulator and phone ( Xperia Android 1.6 to 2.2 ) for NETWORK_PROVIDER. It works fine in simulator for GPS_PROVIDER. But when I try to use the GPS in the devices, the location listener is never called. I can see in the DDMS that the GPS is actually working and retrieve locations, but it never calls the listener methods.
There is a strange message though :
WARN/libloc_api(1173): loc_eng_report_position: ignore position report when session status = 1
I can not see what I am missing. Any idea ? Thanks.
It takes a while to get a location fix. The time to first fix de(TTFF) depends on a lot of factors, like number of visible GPS satellites, signal to noise ratio, the GPS chipset etc ...
Here is a nice article that will help you get the best out of GPS on android phones.

LocationListener works on emulator, not on phone

I'm having trouble getting a LocationListener to call the onLocationChanged() callback on my phone. When I run my code in the emulator, it works fine, the callback is called each time I do a geo fix.
When I run the application on my phone, nothing at all happens. The callback is never called. I have location enabled by both GPS and by Wireless in my settings. The application has all of the uses-permissions for location permissions.
Also, when I call getLastKnownLocation() on a LocationManager object, my application crashes. (Still, only on my stupid phone). Even if I try to catch an exception that's causing it to crash, it still just crashes, so I can't even get any information on what is causing it to crash. This is extremely frustrating.
LocationManager.getBestProvider() is returning GPS, and when I open google maps it finds my location in no time at all. What the heck is going on here? Is there some way I can figure out why it's crashing on my phone?
private void setupLocListener(){
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(false);
lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() {
#Override
public void onLocationChanged(Location arg0) {
map.setLocation(arg0);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
});
}
onLocationChanged() wont fire until you actually start receiving GPS coordinates.
By that I mean the chip has to warm up for about a minute or so from my experience before you start receiving data from it.
I usually start some other application and wait for it to prove that the GPS chip has warmed up before I go testing any of my GPS apps.
I know that you mentioned that it works properly in Google Maps but have you tried clearing your memory and restarting your application straight away afterwards?
Also getLastKnownLocation() is always null until you start receiving coords.
The Location framework pushes coordinates to your callback, when they become available. Depending on weather, etc. you may not get a "fix" initially. You should see the "GPS" indicator on the status bar when your listener is successfully registered.
getLastKnownPosition() works just fine (it may return null); and Google Maps uses that, while it is waiting for an initial fix from the location provider.
You may also want to see what other providers are available, e.g. cell-tower data, and attempt to obtain data from those (i.e. LKP), either instead of, or until, your "preferred" provider starts pushing data.
Also, don't assume any particular service exists, e.g. LocationManager (Context.getSystemService() can return null), or any suitable provider exists, (getBestProvider() can return null). Your code will fail as-is on the right device with the right settings. If the documentation says null you must check for it, or users will be uninstalling it because it FC's all over the place.

Categories

Resources