Android 6.0 getLastKnowLocationPermission

2019-07-29 02:53发布

I have a error when I'm trying to get last known location in Android 6.0

Location lastLocation =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Error:

java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

My manifest:

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

check permissions:

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

        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            try {
                ActivityCompat.requestPermissions(getActivity(), new String[] {
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION },
                        1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PointDTO point = new PointDTO();

        try {
            Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//            addresses = geocoder.getFromLocation(lat, lon, 1);
//            //        String cityName = addresses.get(0).getAddressLine(0);
//            //        String countryName = addresses.get(0).getAddressLine(2);
//            address= addresses.get(0).getAddressLine(0);
            point.setLat(lastLocation.getLatitude());
            point.setLon(lastLocation.getLongitude());

        } catch (NullPointerException | SecurityException e) {
            e.printStackTrace();
        }

        return point;

What I did wrong? It works in Android Api 23-

UPD:

NPE:

 ActivityCompat.requestPermissions(getActivity(), new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    1);

SecurityException

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

2条回答
在下西门庆
2楼-- · 2019-07-29 03:00

You should do get location work if runtime location permissions are given for marshmallow(android api 23 or above), check this:

This line of code

Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

here will show you error in code for security exception and ask to use runtime permission code everywhere for code, no need to worry you can execute the build with this error info.

private void checkRuntimePermissions(){
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            try {
                ActivityCompat.requestPermissions(getActivity(), new String[] {
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION },
                        1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            getMyLocation();
        }
    }

    private PointDTO getMyLocation(){
        PointDTO point = new PointDTO();
        try {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//            addresses = geocoder.getFromLocation(lat, lon, 1);
//            //        String cityName = addresses.get(0).getAddressLine(0);
//            //        String countryName = addresses.get(0).getAddressLine(2);
//            address= addresses.get(0).getAddressLine(0);
            point.setLat(lastLocation.getLatitude());
            point.setLon(lastLocation.getLongitude());

        } catch (NullPointerException | SecurityException e) {
            e.printStackTrace();
        }

        return point;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQ_ACCESS_COARSE_LOCATION:
            case REQ_ACCESS_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! 
                    checkRuntimePermissions();
                } else {
                    //do other operation because permission not given
                }
                return;
            }
        }
    }
查看更多
Explosion°爆炸
3楼-- · 2019-07-29 03:17

I alos had the same problem while doing these.You should access location after granting the permission

it should be inside method onRequestPermissionsResult()

refer request permission blog

查看更多
登录 后发表回答