2016-04-28 155 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。 –