Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Android Permissions in a Method

Tags:

here is my code and it works perfectly fine.

if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {     mMap.setMyLocationEnabled(true); } 

But I don't like such a big code on every check, and want to delegate it to a method in my utility class.

if (Utils.hasMapLocationPermissions(getActivity())) {     mMap.setMyLocationEnabled(true); } 

But setMyLocationEnabled has annotation @RequiresPermission And thus I can't delegate it to a method, because lint and editor shows it as an error.

Is there some annotation to write on my utility method and suppress lint?

Something like this

@ChecksPermission public boolean hasMapLocationPermissions(Activity activity) {   return // my checking logic.. } 
like image 801
Ioane Sharvadze Avatar asked Mar 16 '16 09:03

Ioane Sharvadze


People also ask

How do you check if permission is granted or not?

checkSelfPermission(activity, Manifest. permission. X) checks if any permission is already granted, if you check out other answers they do the same, and rest of the code asks for the permissions not granted.

How can I tell if an Android user is denied permission?

The method shouldShowRequestPermissionRationale() can be used to check whether the user selected the 'never asked again' option and denied the permission.

How do I check APK permissions?

Open Settings and choose Apps & notifications. Find and select the app you want to check permissions for. Tap Permissions. Now you can see all the app's permissions.


2 Answers

You can rename your method such as checkLocationPermission(Activity activity). I´ve discovered that your method's name must start with "check" and end with "Permission" to pass Lint warnings.

For example:

public static boolean checkLocationPermission(Context context) {     return ActivityCompat.checkSelfPermission(context,             Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED             && ActivityCompat.checkSelfPermission(context,             Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; } 
like image 176
tjeubaoit Avatar answered Sep 29 '22 12:09

tjeubaoit


You can suppress this error in both the editor and in lint reports by annotating your method with @SuppressWarnings("MissingPermission"), or you can suppress the error for just a single statement by putting //noinspection MissingPermission above that line.

For example:

@SuppressWarnings("MissingPermission") public boolean hasMapLocationPermissions(Activity activity) {   // your checking logic } 

Or:

if (Utils.hasMapLocationPermissions(getActivity())) {     //noinspection MissingPermission     mMap.setMyLocationEnabled(true); } 
like image 45
Bryan Herbst Avatar answered Sep 29 '22 13:09

Bryan Herbst