![](/img/trans.png)
[英]Android 6.0 “gps” location provider requires ACCESS_FINE_LOCATION permission
[英]"gps" location provider requires ACCESS_FINE_LOCATION permission for android 6.0
在android清單文件中,我為gps添加了以下權限以訪問該位置
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我正在使用位置管理器類進行如下位置更新
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 中測試過,它工作正常。
請幫助!!!提前致謝。
從 6.0 開始,一些權限被認為是“危險的”(FINE_LOCATION 就是其中之一)。
為了保護用戶,他們必須在運行時獲得授權,以便用戶知道這是否與他的操作有關。
去做這個 :
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
它將顯示一個對話框,用戶將在其中選擇他是否對您的應用程序進行自動化以使用位置。
然后使用此函數獲取用戶答案:
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
}
}
如果用戶接受一次,那么您的應用程序將記住它,您將不再需要發送此 DialogBox。 請注意,如果他決定,用戶可以稍后禁用它。 然后在請求位置之前,您必須測試是否仍然授予權限:
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
這一切都在 Android 文檔中進行了解釋: http : //developer.android.com/training/permissions/requesting.html
試試這個代碼
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
}
}
自版本 6 以來,我們對 android 權限進行了一些更改。您必須在使用它們時同意這些權限,而不是在安裝應用程序時。 如果您使用 buildtools 版本 23,可能會出現一些問題。 檢查此鏈接以了解。 轉到設備設置>應用程序並選擇您的應用程序,然后手動接受權限並檢查您的問題是否已解決。
我最近也遇到了這個問題。 在 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.