requestLocationUpdates中的Android权限错误

时间:2017-02-09 09:43:00

标签: android android-permissions locationmanager

我在使用android

的位置权限时遇到错误

,这在onCreate

onCreate(Bundle savedInstanceState){

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    provider = locationManager.getBestProvider(new Criteria(), false);
    }

这是onResume

protected void onResume() {
    super.onResume();


    try {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            getLocationPermission();
        }
        if (permissionGranted) {
            locationManager.requestLocationUpdates(provider, 400, 1, this);

        }
    }catch (Exception e) {

        e.printStackTrace();


    }

这是函数getLocationPermission()

private void getLocationPermission() {
    /*
     * Request location permission, so that we can get the location of the
     * device. The result of the permission request is handled by a callback,
     * onRequestPermissionsResult.
     */
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        permissionGranted = true;
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSION_ACCESS_FINE_LOCATION);

    }
    /*
     * Get the best and most recent location of the device, which may be null in rare
     * cases when a location is not available.
     * Also request regular updates about the device location.
     */
    if (permissionGranted) {

        Log.i("Permission",String.valueOf(permissionGranted));
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        Log.i("Location", location.toString());
    }
}

这是onRequestPermissionsResult

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        for (int result : grantResults) {
            if (result == PackageManager.PERMISSION_DENIED) {
                // User Refused to give permission
                Toast.makeText(getApplicationContext(), "Please We Need Have Your Current Location", Toast.LENGTH_SHORT).show();
            } else if (result == PackageManager.PERMISSION_GRANTED) {
                permissionGranted = true;
            }
        }
    }

这是接受位置许可后的错误

W/System.err: java.lang.IllegalArgumentException: invalid provider: null
W/System.err:     at android.location.LocationManager.checkProvider(LocationManager.java:1704)
W/System.err:     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459)
W/System.err:     at com.osamaa.appdevstart1.MapsActivity.onResume(MapsActivity.java:322)
W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
W/System.err:     at android.app.Activity.performResume(Activity.java:6312)
W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我认为这里的问题与权限和订购locationManger

的方式有关

1 个答案:

答案 0 :(得分:0)

使用此

GoogleApiClient     mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);

            mGooglePlaceApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(connectionPlaceListener)
                    .addOnConnectionFailedListener(connectionFailedListener)
                    .build();
            mGooglePlaceApiClient.connect();

连接回调

 private GoogleApiClient.ConnectionCallbacks connectionPlaceListener = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(Bundle bundle) {
            if (CommonCode.checkAndroidVerSionFor23()) {
                // Marshmallow+
                if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
            }

                LocationServices.FusedLocationApi.requestLocationUpdates(mGooglePlaceApiClient, mLocationRequest, ScreenService.this);
                CommonCode.printLoge(TAG, "Location update started ..............: ");

        }

        @Override
        public void onConnectionSuspended(int i) {
            CommonCode.printLoge("Place service", "Connecting to GoogleApiClient suspended.");
        }
    };