The gps is disabled, every so often in android 7

1

The gps is disabled, every so often on android 7 nougat on a Sony Xperia, I'm using the Google merge API. I have a service running in the background.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    buildClient();

    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }
    wakeLock.acquire();
    if (scanHandler == null) {
        new pro().execute();
    }
    return Service.START_STICKY;

}

protected synchronized void buildClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(scan_interval_ms);
    mLocationRequest.setFastestInterval(scan_interval_ms);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

protected void startLocationUpdates() {
    createLocationRequest();
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        try {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            Log.v(TAG, "Location update started ..............: ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

private void removeUpdateLocation()
{
    if (mGoogleApiClient.isConnected()){
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}

@Override
public void onConnected(@Nullable final Bundle bundle) {

    Log.i(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
    if (mGoogleApiClient.isConnected()) {
        startLocationUpdates();
    }
}

In Android API 23 (6.0) Marshmallow works correctly and in previous versions.

Using the Android API occurs the same (GPS is disabled for the indicated version).

    
asked by ja12 12.02.2018 в 10:32
source

1 answer

-1

The problem lies in the limitations that were added in the new versions of android for the services in the background. What is sought is that applications optimize better the use of resources to prevent certain applications from abusing processes that can waste many resources such as mobile data network or battery. This is best described in the following article

link

As mentioned in the article, to avoid this problem you should use a Foreground service instead of a background service and this should work correctly in all the apis.

To use a foreground service you can review this article

link

in the section Running a service in the foreground

    
answered by 12.02.2018 в 15:30