How to avoid use of GPS using LocationClient of Google Play Services? - android

I have an app that runs in background and I want to avoid use of GPS.
According to official doc LocationRequest API:
(...) Applications cannot specify the exact location sources, such as GPS, that are used by the LocationClient. (...)
Is it really impossible to prevent the app from using GPS when using Google Play Services Fused Location Provider?
In my case, I know the Wi-Fi is always ON and it should be enough to retrieve a good position using NETWORK_PROVIDER. May I use the old-school location retriever to avoid use of GPS?

You're looking for LocationManager, which provides much more control, but requires far more code. If you don't request ACCESS_FINE_LOCATION permission, the GPS will never be used.
Check out this guide: https://developer.android.com/guide/topics/location/strategies.html
It should be noted, if you designate ACCESS_COARSE_LOCATION with LocationRequest, it will intentionally obfuscate the location result.
Location requests from applications with ACCESS_COARSE_LOCATION and
not ACCESS_FINE_LOCATION will be automatically throttled to a slower
interval, and the location object will be obfuscated to only show a
coarse level of accuracy.

Related

Are there any advantages of using FusedLocationProviderApi over LocationManager?

Earlier to get user current location I have used LocationManager:
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
It is easy to read and very straightforward code.
But I have noticed that Google recently released New Client API Model in Google Play Services and suggests to use FusedLocationProviderApi which looks like much more complicated, it is async, it requires to handle callbacks etc.
Are there any advantages of using FusedLocationProviderApi over LocationManager?
FusedLocationProvider uses a mix of hardware to determine location based on the context of the request, meaning it's optimized transparently to you. It will also cache captured locations between applications to avoid unnecessary work to determine location info. So if a user has a variety of location-aware apps, they potentially avoid taxing the device (and waiting) for a location capture as one may have already been cached.
While the ol' LocationManager will suffice in small, one-off situations, you should definitely consider the newer alternative as the benefits may be great, and the work to implement, easy.
You may as well use it as Google Play Services is regularly updated across devices, and continuously includes improvements to location-based features (and more).
A link to an explanation of the FusedLocationProvider at launch: https://www.youtube.com/watch?v=Bte_GHuxUGc
In some situations FusedLocationProvider performs horribly. If you want to track your path on a map for example, it may decide to switch between GPS data and Wifi data. To calculate an accurate distance travelled, it is much better to force GPS readings with LocationManager, especially if you also want to track altitude, which wifi doesn't provide.
Just keep in mind that FusedLocationProvider uses LocationManager under the covers to at least get GPS points. It may be performing Wifi scans and doing a look up on the results to determine location as well, instead of using the Network provider, but there isn't any documentation about this that I am aware of.
Unless you cannot or do not want to use FusedLocationProvider, I would recommend using it.

GoogleApiClient Vs LocationClient to get accurate location in Android

I am trying to get an accurate location for my emergency alert app. I am already using the locationManager to get the location but fetching location with this technique takes about 30seconds I am looking for faster & accurate way.
The main goal here is to get faster/accurate location (Indoor or outdoor).
I have decided to use GooglePlayservices API for this purpose. I figured out there are two options LocationClient and GoogleApiClient?
I am not sure what is best here? Can somebody tell me what is the difference with both or is there a better option?
Let me know!
Thanks!
The GoogleApiClient location client is good if you need a fused high accuracy location.
With fused I mean a location calculated using Wifi, GPS and so on.
With the location client, instead, you have to select a provider. You can even use a criteria, but in this case I think the information is not "fused". In your case I think the best choice is to use the GoogleApiClient location client and you can use getLastLocation().
I would recommend to use GooglePlayServices, not only by the reasons #gresywolf82 hasstated, but also because it is most stable way of getting Location data.
In my experience I have encountered that some Android Devices may have issues obtaining location from the SDK's locationManager, this is due to probable bugs in the SDK, you can check an issue in the Android Open Source Project - Issue Tracker regarding this problem with the locationManager here
I have found the previous problem myself while using the locationManager and had absolutely no issues when switching to Google Play Services API.
SO in conclusion, not just for more precision but also for better stability within various devices I definitely recommend using Google Play Services API.
Both LocationClient and GoogleApiClient provide fused locations.
It is the LocationManager where you need to specify your own providers, this is a low level access if you want to roll your own.
As far as I can see, the GoogleApiClient is an aggregate of LocationClient and other GoogleApis (someone pls. correct if I'm mistaken). If you only wish to target LocationClient you may do so without any Activity compliance.
On the other hand, it would appear that GoogleApiClient requires tight coupling with the Activity life cycle, needing to be disconnected onStop and connected onStart. The trouble I have with such asynchronous calls is trying to make sure the API is up and ready by the time I need it. Therefore I usually go with LocationClient and keep it running outside of Activity lifecycles.
This works, but if you have issues with power consumption you may want to disconnect when app is in background, and reconnect when app comes into foreground.
Do not use GoogleApiClient. It is old and problematic. Use the new API. Please read the following Android newsletter link for the details :
https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

Android Location Update Clarification

Am working with the location update application which will update the user location periodically to the server.
I have used the fused location provider to get the location update. I have referred the following link Android Location Update.
Regarding the location update I have following clarifications,
1) I requested Location using Pending Intent. I had given the Time
interval as 5 min in the location request. I am getting the location
information successfully at every 5 min. But my question is "How effectively
Android uses the GPS to get the location - GPS need not to be on for
the whole 5 min". Around 4 min 50 seconds it starts using the GPS to
get the user location. I just want to know how the fused location provider effectively using the GPS.
2) And also I want to know the time taken to fetch the user location
by using available providers. How much time approximately taken by the
android api to get the user location by using fusion location
provider.
3) The Fused Location Provider uses GPS only at the nearest time of given interval to get
the user location. The remaining time the GPS is not usable. Is there
is better solution to switch off the GPs or effective way to use the
GPS to save the mobile battery power.
4) Am giving the time interval as 1 min. Some time am not getting the
location update every one minute. For Example, First minute successfully
I am getting the user location. Then not getting the user location at 2nd Minute. Then 3rd minute
getting fine and so. I have GPS ON and with mobile network available and WiFi
connected.
5) And what is the maximum time interval we can give. Am not finding
any maximum time limit in the document. We can give the minimum time interval as zero. But it is not recommended.
6) And also I want to know the minimum OS support while using this feature. I have referred below link Does Google Activity Recognition work on older versions of Android? Which says Everything in Google Play Services should work back to API level 8(Android 2.3).
Please help me on this. I hope this could help other developer also who are working on Android Location Updates.
Thanks in advance.
I have used the fused location provider API's in our project and it's was really helpful to improve battery usage.Earlier We were using Android's Location framework APi's .
Please read below article which I have prepared ,you can refer the information to implement it in your project.
In simple words, it’s the best way to get location data in Android platform as of now.
GooglePlay Services provide many location APi’s to get the location data(e.g. User’s current location or you can say device’s last known location).
The Fused Location Provider is one of the location APIs in Google Play services.
Prerequisite is that:
1- Google Play services sdk is used as library project(and also Google PlayService is properly installed in your device)
Download and install the Google Play services component from the SDK Manager and add the library to your project.
Import GooglePlayServices lib from android google-play-services_lib in your development project as Library project.
2- You should have an actual device as this APi won’t work in Emulator.
The Fused Location Provider intelligently manages the underlying location technology (GPS/Wi-Fi/Network provider’s connection) and gives us the best location according to our needs.
Why to use
=============
We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert. But there were two main issues with this approach:
1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors).
2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.
Benefits
==========
1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.
2. Battery efficient: Minimizes out app’s use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
Steps to use this Api:
=====================
1- Declare the location related permission in the manifest file.
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
Or
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2. Implement related interfaces and callbacks.
Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:
2.1 com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks: Specifies methods that Location Services calls when a location client is connected or disconnected.
2.2
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener
3 Connect to Google Play Services
Connecting LocationClient to Google api.To do this , create a LocationClient object (it’s actually instance of GoogleApiClient object) as below:
mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
and then call connect() :
mLocationClient.connect();
4- Create an instance of FusedLocationProviderApi by using LocationServices class as below:
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
5- Retrieve the current location
Inside onConnected(Bundle bundle){
Location currentLocation = fusedLocationProviderApi .getLastLocation(mLocationClient);
if (mCurrentLocation != null) {
Log.d(TAG, "last location =" + mCurrentLocation.getLatitude()
+ " - " + mCurrentLocation.getLongitude());
}
}
6- Receive periodic location updates
Create and setup LocationRequest object:
mLocationRequest = new LocationRequest();
private void setLocationParameter() {
// Set the update interval
mLocationRequest.setInterval(Constants.SECONDS_TO_UP);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(Constants.SECONDS_TO_UP);
// Set the distance to 10 meters.
mLocationRequest.setSmallestDisplacement(Constants.METERS_TO_UP);
}
6- Request for periodic Location updates: To get periodic location updates from Location Services, we send a request using a location client.
LocationListener listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Utils.locationUpdates = Utils.locationUpdates + 1;
if (Utils.locationUpdates == 1) {
mLocationRequest
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationServices.FusedLocationApi.requestLocationUpdates(
mLocationClient, mLocationRequest, listener);
}
}
}
};

Google Play Fused Location and GPS

I need clarification on Google Play Fused Location.
I ask a high priority (LocationRequest.PRIORITY_HIGH_ACCURACY) but on my Android (4.4.4) I set the localization mode to Saving Battery: in this case Google Play don't use anyway the GPS, it's correct?
It think this because I try t use Google Play Fused Location, but if the GPS is off he use only the other methods for retrieve coords, and the obtained position is inaccurate (and I can't see the GPS icon on the status bar).
So, where is the advantage to use Fused Location? To retrieve an High accuracy position I need to set on the GPS anyway?
I really don't understand, thanks.
The advantage of the Fused Location API is that it combines and manages all the different types of location providers into one easy to use api where as if you wanted to use the built in LocationManager you have to manage them all yourself

Differences of location services

I newbie in location and trying make service to show my current location and another one location saved previously. And what the difference of using LocationListener vs LocationClient?
The LocationListener connects to the LocationManager and retrieves your location. This worked and works fine.
The LocationClient is a new way to implement this while some of the rest gets deprecated. It features more functions.
Both methods actually works.
Location Manager was introduced in Android SDK and can be used as a feature of android.
Location Client is something that's part of Google Play SDK and is introduced in the recent Google IO 2013.
You can understand that since LocationClient is the latest, it is more efficient in getting the location with minimal energy(battery drain) with greater accuracy.
Reasons to use LOCATIONCLIENT because:
The location update behavior is very abnormal and wont work as you expect. i.e. The location updates get stuck when switching networks. (It keeps giving you some old location)
The location client wont work on modified android versions of the android OS, as it requires Google play services.
Location Client might be good on the battery of the phone but it won't be good with giving you timely accurate location updates.
I recommend good old Location Manager as I don't find location client reliable at all.
Note : There is no point of saving battery if you are not even getting your current location in a location based application.

Categories

Resources