Check Android Permissions in a Method

后端 未结 2 2034
无人及你
无人及你 2021-02-12 16:14

here is my code and it works perfectly fine.

if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManag         


        
相关标签:
2条回答
  • 2021-02-12 16:53

    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);
    }
    
    0 讨论(0)
  • 2021-02-12 17:02

    You can rename your method 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;
    }
    
    0 讨论(0)
提交回复
热议问题