我试图通过网络和GPS来获取用户的位置。我的流程是这样的-
问题-当我试图请求权限时,应用程序进入关闭模式。也许,我问错地方了?请放点光。
这是同样的密码-
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) at android.os.Parcel.readException(Parcel.java:1637)在android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:614) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:887)在android.location.LocationManager.requestSingleUpdate(LocationManager.java:695) at com.pavanbawdane.weatherinformation.MainActivity.getLocation(MainActivity.java:140)在com.pavanbawdane.weatherinformation.MainActivity.getLocationMgr(MainActivity.java:99) at com.pavanbawdane.weatherinformation.MainActivity.onCreate(MainActivity.java:81)
发布于 2017-05-26 21:46:46
requestPermissions()是异步的。当该方法返回时,您还没有权限。到那时,用户甚至还没有被要求授予您权限。
如果确定您不拥有该权限,并且需要调用requestPermissions(),则无法尝试获取该位置。当您被告知用户是否授予您权限时,您需要最早在onRequestPermissionsResult()中这样做。
请参阅这个示例应用程序,查看如何请求权限,然后请求位置。
发布于 2017-05-26 22:11:26
不要在locationManager.requestSingleupdate()方法中调用getLocation()。
请求权限后,重写onRequestPermissionResult()方法。在该方法中,您必须检查是否授予了权限。
如果权限被授予,则调用locationManager.requestSingleupdate()。
发布于 2017-05-26 23:24:51
您必须使用这个名为德克斯特的非常棒的库,它可以轻松地帮助您处理请求许可的问题,并在许可确认或拒绝时执行操作。
https://stackoverflow.com/questions/44210341
复制相似问题