我是android开发领域的新手,现在我很难获得当前的用户位置。
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>MainActivity:
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
Log.d("Check if permission", "permission is: " + permissionCheck);Logd返回permission is: -1
我做得这么糟糕是什么原因?
使用Android,target-sdk是23
发布于 2016-03-05 20:06:09
checkSelfPermission()将检查用户是否已授予权限。如果检查失败(在本例中为-1),则需要使用requestPermissions()向用户请求权限。
对此有一个很好的解释,并举例说明了如何在android文档(http://developer.android.com/training/permissions/requesting.html#perm-request)中检查、通知和请求权限。
如果链接死了,下面是它们的代码:
// 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.READ_CONTACTS)) {
// 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.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}(来源: Android Developer Docs)
https://stackoverflow.com/questions/35819132
复制相似问题