2016-04-28 129 views
0

好吧,我有在如下面的代码和警告: enter image description hereAndroid Studio添加权限检查错误?

当我同意添加此类检查,AS插入下面的代码:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
       ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return TODO; 
     } 

而我只是想知道,为什么AND运算符正在使用?
如果用户只拒绝上面的权限之一,那么这样的检查将不起作用,就像我一样。

还是我想念什么?

回答

1

您可以通过不同的方式获取位置。从GPS使用LocationManager.GPS_PROVIDER,从使用LocationManager.NETWORK_PROVIDERLocationManager.PASSIVE_PROVIDER(GPS)的网络获得。

需要GPS_PROVIDERPASSIVE_PROVIDER。需要

NETWORK_PROVIDER

根据doc

注:如果您使用的是两个NETWORK_PROVIDER和GPS_PROVIDER,然后 您需要请求只有ACCESS_FINE_LOCATION许可,因为 它包括许可为两个提供者。 (许可 ACCESS_COARSE_LOCATION包括许可只针对NETWORK_PROVIDER。)

所以大概的Android Studio不,检查您是否同时使用,因此起到安全检查都

+0

好吧,所以Android Studio添加的权限检查是错误的?在这种情况下,应该有OR运算符而不是AND。 –