0

I have a service that listens to location changes. I use an instance of location manager and request location updates in onStartCommand, however it requires that I add permission checks - ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION. If the user didn't grant the permissions I want to stop the service, so I call stopSelf() and return START_NOT_STICKY from onStartCommand() is it a good practice to handle this situation?

here's onStartCommand():

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d(TAG, "onStartCommand: ");

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            Log.d(TAG, "onStartCommand: missing permissions");
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            stopSelf();
        }
        else {

            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 5000, 10, mLocationListener);

            prepareNotificationAndStartService();

        }

        return START_NOT_STICKY;
    }

   private void prepareNotificationAndStartService(){

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, 0);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Address place holder")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .build();

    startForeground(1, notification);
}
Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 1
    Why not check for these permissions before starting the service? – Chrisvin Jem Jul 17 '19 at 10:11
  • I do check them, but if I use `mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, mLocationListener);` without the if statement it throws an error. – Keselme Jul 17 '19 at 10:14
  • What is your question? I don't see a problem with this code, seems like a legit way to do it :) – Vucko Jul 17 '19 at 10:17
  • @Vucko What I wanted to ask is, if it's ok to call for `stopSelf()` from `onStartCommand` because the documentation says that it will be stopped only if the service already running, and when in `onStartCommand()` it still not running. – Keselme Jul 17 '19 at 10:20
  • 1
    Well according to the top-rated Android answerer here on SO, [it's okay](https://stackoverflow.com/questions/8279199/can-i-call-stopself-in-service-onstartcommand) – Vucko Jul 17 '19 at 10:35

0 Answers0