请求位置更新权限

requestLocationUpdates permission

我有一个 Android 应用程序将 GPS 位置发送到 DB。 今天测试了一下,发现location manager需要权限才能调用requestLocationUpdates.

我想将权限设置为允许,我不想向设备或用户请求权限。

现在,代码需要添加一些代码来请求权限吗?

     /**
     * Enable location services
     */
    public void connect() {
        Log.d(TAG, ".connect() entered");

        // Check if location provider is enabled
        String locationProvider = LocationManager.NETWORK_PROVIDER;
        if (!locationManager.isProviderEnabled(locationProvider)) {
            Log.d(TAG, "Location provider not enabled.");
            app.setCurrentLocation(null);
            return;
        }

        // register for location updates, if location provider and permission are available
        String bestProvider = locationManager.getBestProvider(criteria, false);
        if (bestProvider != null) {
            locationManager.requestLocationUpdates(bestProvider, Constants.LOCATION_MIN_TIME, Constants.LOCATION_MIN_DISTANCE, this);
            app.setCurrentLocation(locationManager.getLastKnownLocation(locationProvider));
        }
    }

    /**
     * Disable location services
     */
    public void disconnect() {
        Log.d(TAG, ".disconnect() entered");

        String locationProvider = LocationManager.NETWORK_PROVIDER;
        if (locationManager.isProviderEnabled(locationProvider)) {
            locationManager.removeUpdates(this);
        }
    }

我也添加到清单中了。 PS:此权限是新要求的,因为几天前我一直在同一台设备(Android 4.4)上测试该应用程序并且运行良好。 自 2017 年 8 月 21 日以来,Android 的某些默认权限是否可能发生了变化?如果可以,我该如何更改?

您必须向 android 的 Marshmallow 版本征求 运行 时间的许可。通过这个 link

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

我们使用以下代码来检查位置权限:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

检查权限最好的库 com.github.fccaikai:AndroidPermissionX:1.0.0

示例代码:

final PermissionCompat.Builder builder = new PermissionCompat.Builder(activity);

    builder.addPermissions(new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO
    });

    builder.addPermissionRationale(activity.getString(R.string.permission_rationale));

    builder.addRequestPermissionsCallBack(new OnRequestPermissionsCallBack() {
        @Override
        public void onGrant() {
            Log.d(TAG, "Grant");
        }

        @Override
        public void onDenied(String permission) {
            Log.e(TAG, "Denied");
        }
    });

    builder.build().request();

您将不得不编写一个方法来检查用户是否授予权限。如果没有,则请求用户许可。 Constants.LOCATION_PERMISSION_REQUEST 只是在 onRequestpermission result

中得到回调时要匹配的 requestcode
public static boolean checkLocationPermission(Activity activity){
        if(ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_PERMISSION_REQUEST);
            return false;
        }
        return true;
    }

在用户允许的情况下,您将获得重写方法的回调 "onRequestPermissionsResult()"