强制关闭Android位置权限

时间:2017-05-26 21:27:39

标签: android gps

我尝试使用网络和GPS来获取用户的位置。这就是我的流程 -

  1. 获取LocationManager
  2. 检查Network_Provider是否已启用
  3. 如果启用,请检查权限,获取权限并使用网络获取位置。
  4. 如果没有,请检查GPS_Provider,如果未启用,则从设置活动启用GPS,否则获取权限并使用GPS获取位置。
  5. 问题 - 当我尝试请求权限时,应用程序将生效为关闭模式。也许,我在错误的地方询问权限?请多点亮一点。

    这里有相同的代码 -

    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:&#34; network&#34;位置提供者   需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限。                                                                                            在android.os.Parcel.readException(Parcel.java:1684)                                                                                            在android.os.Parcel.readException(Parcel.java:1637)                                                                                            在   android.location.ILocationManager $存根$ 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)

3 个答案:

答案 0 :(得分:0)

requestPermissions()是异步的。到该方法返回时,您还有权限。到那时,用户甚至没有被要求授予您许可。

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

请参阅this sample app,了解如何申请权限,然后申请地址。

答案 1 :(得分:0)

请勿在{{1​​}}方法中致电locationManager.requestSingleupdate()

请求权限后,覆盖onRequestPermissionResult()方法。 在该方法中,您必须检查是否授予了权限。

如果授予了权限,则拨打getLocation()

答案 2 :(得分:-1)

你必须使用这个名为Dexter的真棒库 这很容易帮助您处理请求许可,并在许可确认或被拒绝时采取行动。