Asking for permissions while using LocationManager

半城伤御伤魂 提交于 2019-11-28 14:10:43

With Android Marshmallow, you have to explicitly request permission from the user even if you have specified those in Manifest file. So, you have to request for location permissions this way : First you create a request code for location

public static final int LOCATION_REQUEST_CODE = 1001; //Any number

And then check if already permission is granted or not, if not then the code will request for permission which will show a native popup asking to deny/allow the location permission

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST_CODE);
        } else {
           locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
        }

The above code should be written before requesting any location preferably in onCreate() of the activity. Then based on the action taken by the user on the popup , you'll get a callback where you can perform according to your requirement.

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                  locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                }
            }
        }
    }

Also, wherever you are trying to fetch the location, you should check whether the location permission has been granted to your application or not and then fetch the location.

 if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                          locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                    }

You can request Manifest.permission.ACCESS_FINE_LOCATION or Manifest.permission.ACCESS_COARSE_LOCATION or both. It depends upon your requirement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!