Tartar
Tartar

Reputation: 5452

Android multiple run time permissions for location

In my sample app, I am getting location from user and i am using Google Play Services and FusedLocationApi. I am also trying to implement run time permissions for location and i am looking for the best practice. I have been searching for it whole day and i found a solution like this. I have these two methods in my PermissionUtils class

public static void askPermissions(final Activity activity) {
    if (isRuntimePermissionRequired()) {
        if (ContextCompat.checkSelfPermission(activity, FINE_LOCATION) != GRANTED || ContextCompat.checkSelfPermission(activity, COARSE_LOCATION) != GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(activity, COARSE_LOCATION)) {
                new AlertDialog.Builder(activity)
                        .setTitle("Permission required")
                        .setMessage("Location is required for this application to work ! ")
                        .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(activity,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        REQUEST_LOCATION);
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                activity.finish();
                            }
                        })
                        .show();
            } else {
                if (getApplicationLaunchedFirstTime(activity)) {
                    setApplicationLaunchedFirstTime(activity);
                    ActivityCompat.requestPermissions(activity, PERMISSIONS_LOCATION, REQUEST_LOCATION);
                } else {
                    new AlertDialog.Builder(activity)
                            .setTitle("Permission Disabled")
                            .setMessage("Please enable the permission in \n  Settings>Uber>Permission \n and check 'location' permission")
                            .setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    activity.startActivity(new Intent(Settings.ACTION_SETTINGS));
                                }

                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    activity.finish();
                                }
                            })
                            .show();
                }
            }
        }
    }
}

public static boolean isPermissionsGranted(Context context) {
    if (ActivityCompat.checkSelfPermission(context, FINE_LOCATION) == GRANTED && ActivityCompat.checkSelfPermission(context, COARSE_LOCATION) == GRANTED) {
        return true;
    }

    return false;
}

And in my Activityi am using these methods in onConnected as below

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "onConnected");
    if (PermissionUtils.isPermissionsGranted(this)) {
        try {
            Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (location == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            } else {
                handleNewLocation(location);
            }
        } catch (SecurityException ex) {
            Log.e(TAG, "SecurityException caught" + ex);
        }
    } else {
        PermissionUtils.askPermissions(this);
    }
}

But application keeps calling onConnected back to back until i give allow permissions. What am i doing wrong and what is the best practice for implementing permissions in Android ? Any help would be appreciated.

Upvotes: 0

Views: 354

Answers (1)

Paresh
Paresh

Reputation: 6857

See,

When will onConnected() be called? when you call mGoogleApiClient.connect() right?

So call that only if the permission is granted. Hence onConnected() will not get called if you permission is not granted.

By doing this, you will be requesting location updates only if the permissions are granted.

Upvotes: 1

Related Questions