Access only location manager - android

I want to check location service is open or not when the app starts.
I use this code :
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
However this code check GPS open or not. In my program, i don't need GPS. I just need location access permission. How can i do that?
Thanks for help.

If you are referring on using either Wifi/Data on accessing location:
LocationManager.NETWORK_PROVIDER

Related

LocationManager still cant find last known location after GPS is turned on

I'm having a problem with android where even after the user turns their location service on, the LocationManager still can't find the users location.
Before I launch the activity that needs to use the user's location I call the following:
//Menu Activity
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER){
Intent nearby = new Intent(mActivityContext, NearbyActivity.class);
startActivity(nearby);
}else{
alertNoGps();
}
Once GPS is turned on the nearby activity can be launched, but the LocationManager still cannot find the users last known location. My relevant code for this is:
//Nearby Activity
try{
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
...
}else{
Toast.makeText ...
}
} catch(SecurityException e){
e.printStackTrace();
}
The phone I'm currently testing on is running Android Marshmallow 6.0.1. If the GPS is already turned on before the app is run through android studio then I have no problem getting the last known location. It's only when the GPS is turned off before I run the app and turn it on while the app is running that I have problems. What about LocationManger is causing this?
Edit: Also in the manifest I have both Fine and Coarse location permissions. I do not know if this is an issue with API 23 and above because of the extra permission checks required, but I have to imagine they are still handled by the Security Exception.
There are two different ways to retrieve the location, one is based on network and another is based on GPS services. Certainly, location based on GPS services would always be more accurate.
According to the documentation, it might take some time to receive the location using the GPS services. In this case, the last known location can be used to get the recently available location by using getLastKnownLocation() method.
Here, to get the last available location, you should change
LocationManager.GPS_PROVIDER to LocationManager.NETWORK_PROVIDER.
And if you wish to receive the accurate, real time location based on GPS services, I recommend you to use FusedLocationApi.
You can find it here
https://developer.android.com/training/location/receive-location-updates.html
Cheers..!!
This is exactly how it should work. Android clears the last known location for a provider when it is disabled, and will return null until a new point is polled for that provider.
In your situation, I would recommend that if you get null location point for last known location with GPS, just request for a single update.
I found out that similar to Pablo's answer, getLastKnownLocation() will be cleared and because it is a rather casual function can return null quite often. If GPS coordinates are more demanded, then you should be more forceful when trying to get GPS coordinates and use something like requestLocationUpdates() or Google's FusedLocationApi rather than getLastKnownLocation().
So now I am using FusedLocationApi with a GoogleApiClient I built using:
client = new GoogleApiClient.Builder(mActivityContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
After the client is connected I find the GPS location in the onConnected() method override which looks like:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = LocationServices.FusedLocationApi.getLastLocation(client);
This seems to be much more reliable in finding GPS coordinates.

How to check if location available using FusedLocationProviderApi and GoogleClient?

We can check for location availability using Location manager's isProviderEnabled method.
I am trying to use the new api's for location, i.e. fusedLocationProviderApi and trying to check if location can be retrieved.
How to check that ?
As you've mentioned in your question, You can check if the Provider is enabled by using Location Manager's isProviderEnabled method, like below.
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
By using FusedLocationApi with GoogleApiClient and LocationRequest, you can request Location Updates with LocationListener Interface which will have a callback method called when the location is changed. onLocationChanged(Location location). So you can know if the location can be granted or not in onLocationChanged method.
So an example flow can be; Checking Location providers to see if they are enabled, and then trying to connect to GoogleApiClient to see if the user has GooglePlayServices etc that can get the User's Location.

get location without google play services -android

I know some phones do not preinstall google play sevice. I want to ask is there possible way to get location without Google Play Services , just use LocationManager. Not LocationClient.
you can use LocationManager
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
USe a LocationManager and set the appropriate provider, GPS or NETWORK.
you can get periodic updates or a single update using the requestLocationUpdates() and the getLastKnownLocation() methods respectively.
Lastly, get a Location object and retrieve the latitude, longitude by calling the required methods on the Location object.

Does Android LocationManager.PASSIVE_PROVIDER provides fine accuracy location?

I'm trying to receive background location update from 3rd party apps through
mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, MIN_TIME_SHORT, MIN_DISTANCE, mLocationListener);
But it turns out that the PASSIVE_PROVIDER only gives location updates from NETWORK_PROVIDER. Is there a way to also receive updates from GPS_PROVIDER or it is not supported?
PASSIVE_PROVIDER means if other application fires the location search, our application will also given the location info.
Is there a way to also receive updates from GPS_PROVIDER or it is not supported?
To get the position from GPS_Provider you have to use this snippet :
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
where lm is your LocationManager. Don't forget to set uses-permission
PS: you can use both Network_Provider and GPS_Provider

How to get current location on android

Hey guys
i want to get the latitude and longitude of the users location.
I have tried developers site and stack overflow , there are loads of examples targeted on the moving user which use some thing like :-
onLocationChanged()
I dont need updates of the location , the user will just open it for a minute or two
so i need to find the location the time the application starts and what if the user has never used any geolocation app before hence i would also like an alternative for
LocationManager.getLastKnownLocation()
My code is :-
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(currentLocation.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
and is working fine.
But i am scared that the getLastKnownLocation may return null when used for first time.
Thanks
Sign up for location updates and once you receive the first update, you can unregister for future updates.
In other words, create a LocationListener and register it with your LocationManager's requestLocationUpdates method. In the onLocationChanged method of your LocationListener, call the removeUpdates method on your LocationManager, with this as parameter.
It might take some time before you get a new location, but except for using getLastKnownLocation there's not really an alternative.

Categories

Resources