1
votes

If users gives permission, a background service is created that starts tracking user locations.

Background service keeps tracking user location,even after user destroys the application.

But if user goes to application settings and remove location permission then service crashes and gives following error message,

java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.

The service runs after every 10 seconds, Kindly guide me how to check this in running service, that either it has permissions or not, because after every 10 seconds it runs the service and calls onLocationChanged method.

public class UpdateService extends Service implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {


     @Override
    public void onCreate() {
        super.onCreate();

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

            mGoogleApiClient.connect();
        }
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(10000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        super.onStartCommand(intent, flags, startId);
        return START_STICKY ;
    }                        


 @Override
    public void onLocationChanged(Location location) {

    saveLocationand(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));

}         

EDIT:

This is how i check permission in Application launch

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,
                android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                showGPSDisabledAlertToUser();
            }
            startService(new Intent(MainActivity.this, UpdateService.class));
        } else
            checkLocationPermission();
    }       
2
It's but obvious if you did remove location permission than app won't work. - M D

2 Answers

2
votes

Use Google Setting's api to check whether the location permission has been enabled or not.

Settings API docmentation

You can also use below code to check if app has requisite permission.

String permission = "**permission**";
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED); 
1
votes

This is because Location permission is flagged as Dangerous permission and hence it requires runtime permission.

You can check my answer here about how to check if location permission is enabled or not, and proceed.

What is the difference between shouldShowRequestPermissionRationale and requestPermissions?

Read more at

https://developer.android.com/training/permissions/requesting.html

Here is the code that I am using in my project:

private void startLocationUpdates() {

    if (!(mHasLocationPermission && mHasLocationSettings)) {
        return;
    }
    try {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient,
                mLocationRequest,
                this
        );
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

You can check if you still have those permissions and location settings enabled or not.