安卓6.0位置权限

3

我正在尝试实现位置权限的运行时授权。具体实现如下:

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;
           }
        }
    }
}

当我在onRequestPermissionResult()中调用getLastKnownLocation()时,出现了语法错误。它告诉我再次检查权限,即使我已经这样做了。那么有人可以告诉我这里的实现有什么问题吗?

2个回答

3

getLastKnownLocation 方法带有以下注释:

@RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})

因此,您会收到以下警告消息:

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

您有两个选项可以避免这个警告:
  1. Add a comment before the call 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. Refactor your code, add a method 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;
    }
    

    In your Activity:

    @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;
            }
        }
    }
    

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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接