位置权限编译错误

时间:2017-03-13 01:35:02

标签: android android-permissions android-gps runtime-permissions

我确定我在这里有权限:

private void checkPermissions() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_LOCATION_REQUEST_CODE);
    } else {
        getLocation();
    }
}

private void getLocation() {
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new UserLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
}

但我仍然收到requestLocationUpdates()调用的编译错误:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`

当我删除函数调用时,错误得到解决:

private void checkPermissions() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_LOCATION_REQUEST_CODE);
    } else {
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new UserLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
    }
}

但是我需要在onRequestPermissionsResult回调中调用getLocation函数,所以我想保留它。 / p>

清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />

这通常如何解决?

2 个答案:

答案 0 :(得分:1)

管理这些权限是一场噩梦。我建议你使用这个库来处理所有样板的东西。 你可以用很少的RXJava知识来做到这一点 https://github.com/tbruyelle/RxPermissions

compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.3@aar'

然后:

final String permission = Manifest.permission.ACCESS_FINE_LOCATION;
rxPermissions
.request(permission)
.switchMap(new Func1<Boolean, Observable<Boolean>>() {
    @Override
    public Observable<Boolean> call(Boolean permissionGranted) {
        if (permissionGranted) {
            //todo: do stuff require permissions.
        } else {
            rxPermissions.shouldShowRequestPermissionRationale(activity, permission).switchMap(
            new Func1<Boolean, Observable<Boolean>>() {
                @Override
                public Observable<Boolean> call(Boolean canShowRationale) {
                    if (canShowRationale) {
                        /**
                         * when user deny permission,Show dialog explaining why permission is
                         * necessary. On Selecting Ask again, permission access will be
                         * re-requested.
                         */

                        //todo: 
                    } else {
                        /**
                         * When user deny permission and select Never Ask Again, Show dialog
                         * explaining why permission are necessary. In this case user need to
                         * go to settings and grant permission.
                         */

                        //todo: 
                    }
                }
            });
          }
    }
});

答案 1 :(得分:0)

对于非反应式方法,这个库将是很好的帮助,它使用简单的基于注释的API来处理运行时权限:

https://github.com/hotchemi/PermissionsDispatcher

var getTotal = function() {
    var exponentBase = 1 + parseFloat(b);
    total = a * (Math.pow(exponentBase, c)) + d * 
    ((Math.pow(exponentBase, c) - 1) / b)
};