-1

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();
    }       
dev90
  • 7,187
  • 15
  • 80
  • 153

2 Answers2

2

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); 
rohitanand
  • 710
  • 1
  • 4
  • 26
  • Thanks, but how can i check it in running service – dev90 Feb 06 '17 at 06:07
  • Whenever the app is launched you need to check this. Check the documentation of SettingsApi. – rohitanand Feb 06 '17 at 06:14
  • Service got crashed even before user launched the application. Let say, User installs the application first time, and gives Location permission,A background service is started, User closes the application and goes to Settings and remove the Location permission for this app. The background service will get crashed instantly. – dev90 Feb 06 '17 at 06:19
  • 1
    GoogleApiclient field provides you with onConnectionFailed method which provides you with error code when api client is not able to connect. Use it make your application fail safe. Even you can put try catch in request location updates. – rohitanand Feb 06 '17 at 06:22
1

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.

Community
  • 1
  • 1
Sumit Jha
  • 2,095
  • 2
  • 21
  • 36
  • Thanks for the answer, I have understand your point. But how can i check this in running service. The service is running, its getting user location after each 10 seconds, in between user removes location permission rights, It does not know about it, and hits onLocationUpdate() method again, and it crashes – dev90 Feb 06 '17 at 06:07