在Android位置权限上强制关闭 [英] Force close on Android Location Permissions

查看:494
本文介绍了在Android位置权限上强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用网络以及GPS获取用户的位置.这就是我的流程-

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

  1. 获取LocationManager
  2. 检查是否启用了Network_Provider
  3. 如果启用,请使用网络检查权限,获取权限并获取位置.
  4. 如果没有,请检查GPS_Provider,如果未启用,请通过设置活动启用GPS,否则获得许可并使用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.

这是相同的代码-

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);
    }

原因:java.lang.SecurityException:网络"位置提供程序 需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限. 在android.os.Parcel.readException(Parcel.java:1684) 在android.os.Parcel.readException(Parcel.java:1637) 在 android.location.ILocationManager $ Stub $ Proxy.requestLocationUpdates(ILocationManager.java:614) 在 android.location.LocationManager.requestLocationUpdates(LocationManager.java:887) 在 android.location.LocationManager.requestSingleUpdate(LocationManager.java:695) 在 com.pavanbawdane.weatherinformation.MainActivity.getLocation(MainActivity.java:140) 在 com.pavanbawdane.weatherinformation.MainActivity.getLocationMgr(MainActivity.java:99) 在 com.pavanbawdane.weatherinformation.MainActivity.onCreate(MainActivity.java:81)

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)

推荐答案

requestPermissions()是异步的.在该方法返回时,您还没有权限.到那个时候,甚至还没有要求向用户授予权限.

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.

如果您确定您不持有该权限,并且需要致电requestPermissions(),则您尚无法尝试获取该位置.当得知用户是否授予您权限时,您需要最早在onRequestPermissionsResult()中执行此操作.

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.

这篇关于在Android位置权限上强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆