2015-11-23 43 views
20

ACCESS_FINE_LOCATION允許在Android清單文件我已經添加了以下權限GPS訪問位置「GPS」定位提供商需要在Android 6.0

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

我fecthing位置更新如下使用位置管理類

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        MIN_TIME_BW_UPDATES, 
        MIN_DISTANCE_CHANGE_FOR_UPDATES, 
        this 
       ); 

但上面的線是給一個例外,說明

"gps" location provider requires ACCESS_FINE_LOCATION permission 

這件事只發生在targetSDKVersion 23(即使用android 6.0),我也測試過在android 5.1中正常工作。

請幫忙!!謝謝提前。

+0

你加<使用許可權...標籤到:

public boolean checkLocationPermission() { String permission = "android.permission.ACCESS_FINE_LOCATION"; int res = this.checkCallingOrSelfPermission(permission); return (res == PackageManager.PERMISSION_GRANTED); } 

這一切都對Android文檔解釋AndroidManifest.xml的manifest節點?例如。請參閱http:// stackoverflow。com/a/16691500/1615430 – RookieGuy

+1

是的,我根據需要在清單節點中做得很好。注意這裏問題只存在於android 6.0中。 –

回答

42

由於6.0的一些權限被視爲 「危險」(FINE_LOCATION是其中之一)。

爲了保護用戶,他們必須在運行時被授權,以便用戶知道它是否與他的行爲有關。

要做到這一點:

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 

它會顯示一個對話框中,用戶會選擇羯羊他autorize您的應用程序使用位置或不。

然後通過使用該功能獲得用戶回答:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case 1: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      } else { 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
     return; 
     } 
      // other 'case' lines to check for other 
      // permissions this app might request 
    } 
} 

如果用戶接受了一次,那麼你的應用程序會記住它,你將不需要再發送此對話框。請注意,如果用戶決定,用戶可以稍後禁用它。然後請求該位置之前,你必須以測試權限授予依舊:http://developer.android.com/training/permissions/requesting.html

+0

好的和詳細的答案,太棒了!也會嘗試一下,還沒有檢查Android Marshmellow。這破壞了我想要的很多應用程序的兼容性,所以它們不會在使用以前版本的設備上快速部署? – RookieGuy

+0

我沒有想到這一點,因爲我最近嘗試了一個應用程序,我正在開發:)我想這個更新之前已經授予的危險權限仍將被授予,即使它們是在安裝時被授予的。 –

+0

請注意,用戶現在可以決定在應用程序android設置中禁用權限。 –

5

試試這個代碼

private void marshmallowGPSPremissionCheck() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M 
       && getActivity().checkSelfPermission(
       Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && getActivity().checkSelfPermission(
       Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(
        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, 
          Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSION_LOCATION); 
     } else { 
      // gps functions. 
     } 
    } 

增加,這也..................

@Override 
    public void onRequestPermissionsResult(int requestCode, 
              String[] permissions, int[] grantResults) { 
     if (requestCode == MY_PERMISSION_LOCATION 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      // gps functionality 
     } 
    } 
+0

這裏是什麼getActivity()?實際發生了什麼?請你解釋一下 –

+0

這個人的代碼必須在一個片段中,或類似的東西。您可以使用getActivity()來獲取上下文,以防您在擴展Fragment而不是Activity的類中。 – Fustigador

+0

這段代碼適合你嗎?如果您在活動中使用此操作,請刪除getActivity()。這段代碼實際上在運行時檢查位置權限。 – Nivedh

0

我們因爲Android權限的一些變化版本6.您必須同意使用它們的權限,而不是在安裝應用程序時。如果使用buildtools版本23,則可能會出現一些問題。檢查this link瞭解。進入設備設置>應用程序並選擇您的應用程序,然後手動接受權限並檢查您的問題是否已解決。

0

我最近也遇到過這個問題。在Android 6.0中,您需要在應用程序中使用它們時明確要求所需的權限。如果您不這樣做,位置權限會被自動拒絕。

// Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(thisActivity, 
        Manifest.permission.READ_CONTACTS) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

     } else { 

      // No explanation needed, we can request the permission. 

      ActivityCompat.requestPermissions(thisActivity, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_FINE_LOCATION); 

      // MY_PERMISSIONS_REQUEST_FINE_LOCATION is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 
    } 

此處瞭解詳情:http://developer.android.com/training/permissions/requesting.html

相關問題