在 Android API 23+ 中,有一种新方法来处理被视为“危险”的权限,就是这种情况android.permission.ACCESS_FINE_LOCATION
and android.permission.ACCESS_COARSE_LOCATION
除其他外。
你不需要处理android.permission.ACCESS_COARSE_LOCATION
如果您已经在使用android.permission.ACCESS_FINE_LOCATION
.
请查看官方文档中的此链接,它向您展示了如何在 Android API 23+ 中在运行时处理权限。
https://developer.android.com/training/permissions/requesting.html https://developer.android.com/training/permissions/requesting.html
这个也可能对你有帮助。
https://developer.android.com/guide/topics/location/strategies.html https://developer.android.com/guide/topics/location/strategies.html
如何检查权限:
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION);
如何请求权限:
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= 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_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
请注意,即使您在运行时请求权限,您仍然必须在 android 清单中保留权限条目,如下所示:
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
...
</manifest>
检查并授予权限后,您应该查看上面的位置策略的链接,在那里您可以找到您缺少的内容的详细信息。
基本上,您没有注册侦听器来接收位置更新。在末尾添加此行后onCreate()
您应该开始在日志中看到位置更新的方法。
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);