Check if Location is unavailable in Android Location Client - android

I would like to check if Location Client is able to provide any location. Is there any way to do so, apart from calling normal location system service?

The accepted answer was correct at the time. But in recent times things have changed with the availability of the SettingsApi It allows you to check if the location services are available on the device and there is a complete example provided by google on github that shows how to use this API. The sample code will prompt the user to enable location if it's switched off.

AFAIK, there is no method to check availability.
Possible Reason:
LocationClient uses fused API which gives location with appropriate combination of GPS,WiFi,Cell,Sensors (according to the Priority you have set for retrieving location).
Reference:
Fused API - Beyond the blue dot
Alternate Solution:
You already know it.you can check availability of provider by using location system service and LocationManager

Related

How to avoid getting null on user location result in android?

In my app I am using FusedLocationProviderClient to get user location every time he enters the app by mFusedLocationClient.getLastLocation().addOnCompleteListener().But sometimes result is null,I am using <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in manifest.How can I get user location even if it not so correct?Because the logic of app depends on that location,I don't need to show it on map or smth like that,just get the location.Thx in advance
Don't use getLastLocation(). There is always a chance to have it return null if it doesn't have a location currently.. Use requestUpdates or requestSingleUpdates- when the callback for those is called the location is guaranteed to be non-null. getLastLocation() should only be used as an optimization to improve speed, it cannot be relied on.
Follow the following example and your app is guaranteed to work as you wish. I have recently used this code and it is working like a charm.
Only thing you should know is that for the location updates to actually work ( to get real latitude and longitude values) you should enable "google play services". There may be several ways to do this but perhaps the simplest of all is to open "settings" on your device and probably somewhere under "security" or "permissions" (depending on your android distribution) find "google play services" and give it the permission it needs.
https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates

Mock coordinates Android Marshmallow

In my app I am using Android Location Manager API and I have a requirement of identifying whether the location coordinates are from normal Location Manager or if they are from Fake(Mock) Location provider. I used location.isFromMockProvider(). Once I turn on the fake location providing app, isFromMockProvider() method returns me true. And after that it does not matter if I turn, fake location providing app, on or off isFromMockProvider() method always returns true.
I found that once FakeGPS app, once triggered, overrides the location manager coordinates and once I stop the FakeGPS app, the Location Manager does not start collecting correct coordinates.
Is there something I am missing.
Disappointed with no response but I am glad that I got it working :)
Posting the resolution as it might help someone else.
Once the FakeGPS app starts mocking location, it stops my app's Location manager.
Now even if I stop the FakeGPS app mocking service, the location manager of my app won't trigger back.
I have to detect the event and restart the location manager of my app, after removing any test provider and clearing test locations.
just resetting and removing test location should help
String t = LocationManager.GPS_PROVIDER;
locationManager.clearTestProviderEnabled(t);
locationManager.clearTestProviderLocation(t);
locationManager.clearTestProviderStatus(t);
locationManager.removeTestProvider(t);

Activity Recognition Api Not working in some devices

Does the location access have any dependency on activity recognition process?
In some devices i could get my activity recognition sample working only after turning on my location setting
This issue is fixed in latest update of Google Play services(4.3.23 (1069729-030)). All we need to do is replace library project "google-play-services_lib" with latest version(4.3.23 (1069729-030)). I noticed a delay of about 1 minute in AR updates if we toggle location settings, but it works.
The Activity Recognition is part of the Google Play Location API. See the docs here:
Recognizing the User's Current Activity
You can see that this is part of the Location Services offered by Play. Internally there needs to be some way to determine the speed of movement of the device - and that requires location based calculations.
Below is an extract from the Class Overview paragraph of the ActivityRecognitionClient documentation:
It only makes use of low power sensors in order to keep the power usage to a minimum.
This suggests (as the OP pointed out) that the API relies on network location, and not GPS location.
So it would make sense from a security aspect.
If I, as a user, decide I don't want to let any apps see or use my location, then I would expect the Play API to obey that decision.
Anything else, and the security is lost.

Getting the message GEOFENCE_NOT_AVAILABLE when using Google Play Services API on Android

I downloaded the GeoDetection.zip file for trying out location based geofencing using the Google Play Services API that is available from the developer android website from the following link:
http://developer.android.com/training/location/geofencing.html
but when i tried to add geo fences using this app, i constantly get the error GEOFENCE_NOT_AVAILABLE
i tried to check what this error means, and i found that if location access is not available that is when i should get this message, but i checked the Location Services section on my phone and i saw that all the Location provides GPS/Wifi Location are all enabled. I am also connected to WIFI successfully. Not sure why i still get the message.
Any body else encounter this issue?..if so, do you have a fix that i could try?
Any other suggestions?.. i tried a lot of options to make sure it is capturing all the options, but there is isn't much debug information for me to proceed any further.
Thanks in advance
Are you requesting the FINE_LOCATION permission?
Remember on Android 6 and above you have to handle the permissions at run-time too.
The above error, GEOFENCE_NOT_AVAILABLE is an expected error code, to let you know that the location adapter is off and location (and geofence) will NOT be tracked anymore.
Once available again, your geofences will be active and you will get the desired callback.
Few Reasons why the triggers aren't happening :
Check if your expiry timestamp that you set in your geofence request, is still valid.
Did your device get restarted ? If yes, you need to re-register fences. (Refer documentation : Re-register geofences only when required documentation)
Is your location, set to High Accuracy mode in Device settings ?
When you re-register your fences, you should un-register them first and then only re-register.
Open Google maps and check if it's showing the right location.
Is Battery Optimisation on for your device ? This will delay geofence triggers
Is you app whitelisted for background operations (Happens in some Chinese OEM's)

How to get user's location?

I want to get user's location in my app. So, I read documentations and know how to get it work. But, if user haven't set "Alloy apps to use internet/GPS to get location" in preferences, my app crashes.
The only way I found is to show dialog that location settings are not enabled and to go programmaticaly to location preferences.
But is there another way to solve this? I saw many apps (like yandex.maps) that do not request this settings being enabled.
well, your app should not install if the user doesn't agree to the terms of use, and allow the permissions requested.
still you should check to see that there's a way to look the information up when you want it.
in your code, before the actual test for user's location, you should probably call a function that checks for all the location manager listings, and return false if there's no way to determine this.
if the function returns false, you should prompt the user and do nothing. else, you should do what it is you're trying to do.
try reading vogella's tutorial about using location manager
Refer this two links below and you might get your answer easily.
How do I get the current GPS location programmatically in Android?
android - how to get current location latitude and longitude
Hope it will help you.

Categories

Resources