Android 6.0位置权限

时间:2015-11-06 08:59:25

标签: android android-permissions android-6.0-marshmallow

我正在尝试为位置实现运行时权限。实施如下:

public class MyLocationManager implements android.location.LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {

    private static LocationManager m_locationManager;
    private static final int REQUEST_COARSE_LOCATION = 999;
    private static final int REQUEST_FINE_LOCATION = 998;

    private String provider; 
    private Context mContext;

    public void startListenLocation(Context context) {

        this.mContext = context;
        m_locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        ...

        if (Build.VERSION.SDK_INT >= 23 && ((ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COARSE_LOCATION) ;
        } else {
            Location location = m_locationManager.getLastKnownLocation(provider);
        }

        ...
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_COARSE_LOCATION: {

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //compiler error on the following line
                    Location myLocation = m_locationManager.getLastKnownLocation(provider);
                } else {
                    //Permission denied
                }
                return;
           }
        }
    }
}

我在getLastKnownLocation()中呼叫onRequestPermissionResult()时出现语法错误。它告诉我再次检查权限,即使我已经这样做了。所以任何人都可以告诉我这里的实施有什么问题

2 个答案:

答案 0 :(得分:3)

方法getLastKnownLocation注释为:

@RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})

因此您收到警告:

  

调用需要用户可能拒绝的权限:代码应明确检查权限是否可用(使用checkPermission)或显式处理潜在的SecurityException

您有两种方法可以避免警告:

  1. 在致电getLastKnownLocation之前添加评论:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_COARSE_LOCATION: 
            {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //noinspection ResourceType
                    Location myLocation = m_locationManager.getLastKnownLocation(provider);
                } else {
                    //Permission denied
                }
                return;
            }
        }
    }
    
  2. 重构代码,添加方法getLastKnownLocationIfAllowed

    public void startListenLocation(Context context) {
    
        this.mContext = context;
        m_locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    
        if (!getLastKnownLocationIfAllowed())
            ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COARSE_LOCATION);
    }
    
    private boolean getLastKnownLocationIfAllowed() {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Location location = m_locationManager.getLastKnownLocation(provider);
            return true;
        }
    
        return false;
    }
    

    在您的活动中:

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_COARSE_LOCATION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    myLocationManager.getLastKnownLocationIfAllowed();
                } else {
                    //Permission denied
                }
                return;
            }
        }
    }
    

答案 1 :(得分:0)

在您的活动中添加此方法并使用该....

    public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {return true;
        } else {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        flag=true;
        return true;
    }
}