I develop an application which needs to be location aware. I tried to use the new google api client but i have some problems.
1.When i ask for location I ask for PRIORITY_HIGH_ACCURACY but i want the user to have the choise to save his battery by using only the mobile networks. When I check Battery saving mode (or disable GPS on old devices) everything works fine. But when I tested the app on a phone with lollipop, even if I close all location providers, the battery is killed. (I also noticed that and by the time the battery is drained, no location update received).
2.I tested to some devices and tried to get location with mobile networks. That worked fine when the device was close to a wifi (but not connected). But in a specific device (sony xperia M2) it is needed to connect to wifi to update location. Is there anything i can do? Is problem of the device or the version of google play services?
My code is here. I fire periodically a pending intent to run the service to be sure that location listener is still listening (i had some problems in the past where location listener disconnected)
public class UpdateLocService extends Service implements GoogleApiClient.ConnectionCallbacks, LocationListener {
public static final String SENDER_ID = "66801";
public static final String devIdToServer = "send dev id to server";
private static LocationListener listener = null;
//private LocationClient locationClient;
private GoogleApiClient mGoogleApiClient;
private static Location lastLocation = null;;
private LocationRequest mLocationRequest;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
Log.d("UpdateLocService", "dienem service ");
locApi();
}
private void locApi() {
if(mGoogleApiClient != null && (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())){
dLog.d("loc.txt","tryed to connect but already connected or trying now");
dLog.d("flow.txt","tryed to connect but already connected or trying now");
}else{
dLog.d("loc.txt","new connection attempt");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
//.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000 * 60);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
public static Location getLocation(){
return lastLocation;
}
@Override
public void onLocationChanged(Location location) {
dLog.d("loc.txt",location.toString());
lastLocation = location;
}
}