Force close on Android Location Permissions

独自空忆成欢 提交于 2019-12-11 16:54:38

问题


I'm trying to get the user's location using Network as well as GPS. This is how my flow goes -

  1. Get LocationManager
  2. Check if Network_Provider is enabled
  3. If enabled, check for permissions, acquire permissions and get the location using network.
  4. If not, check for GPS_Provider, if not enabled, enable GPS from settings activity else acquire permissions and get the location using GPS.

The problem - When I'm trying to request the permissions, the app goes into force close mode. Perhaps, I'm asking permissions in the wrong place? Please throw some light.

Here's the code for same -

private void getLocationMgr() {
                LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                Log.v(TAG, "Network enabled? " + isNetworkEnabled);
                if (isNetworkEnabled) {
                    getLocation(locationManager, LocationManager.NETWORK_PROVIDER);
                } else {
                    boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                    if (isGPSEnabled == false){ // go to GPS settings}
                    else {
                    getLocation(locationManager, LocationManager.GPS_PROVIDER);
                    }
               }
        }

private void getLocation(final LocationManager locationManager, final String provider) {
        // Getting permissions from user for location access
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERM_FINE_LOCATION);
        }

        locationManager.requestSingleUpdate(provider, new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                Log.v(TAG, "Provider: " + provider);
                Log.v(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
                Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                try {
                    List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),
                            location.getLongitude(), 1);
                    if (addresses.size() > 0) {
                        Log.v(TAG, "City : " + addresses.get(0).getLocality());
                        Log.v(TAG, "Country: " + addresses.get(0).getCountryCode());
                        setCityExecute(addresses.get(0).getLocality(), addresses.get(0).getCountryCode());
                    } else {
                        Log.v(TAG, "Unable to get city");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
            }
        }, null);
    }

Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. at android.os.Parcel.readException(Parcel.java:1684) at android.os.Parcel.readException(Parcel.java:1637) at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:614) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:887) at android.location.LocationManager.requestSingleUpdate(LocationManager.java:695) at com.pavanbawdane.weatherinformation.MainActivity.getLocation(MainActivity.java:140) at com.pavanbawdane.weatherinformation.MainActivity.getLocationMgr(MainActivity.java:99) at com.pavanbawdane.weatherinformation.MainActivity.onCreate(MainActivity.java:81)


回答1:


requestPermissions() is asynchronous. By the time that method returns, you do not have permission yet. The user has not even been asked to grant you the permission by that time.

If you determine that you do not hold the permission, and you need to call requestPermissions(), you cannot try getting the location yet. You need to do that in onRequestPermissionsResult() at the earliest, when you are informed about whether the user granted you permission.

See this sample app to see how to request permissions and then request a location.




回答2:


Don't call locationManager.requestSingleupdate() in getLocation() method.

After requesting permission, override the onRequestPermissionResult() method. Within that method, you have to check the permission is granted or not.

If the permission is granted then call locationManager.requestSingleupdate().




回答3:


you Have to use this awesome library called Dexter which easily help you to handle asking for permission and do action if permission confirmed or if refused.



来源:https://stackoverflow.com/questions/44210341/force-close-on-android-location-permissions

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