我是Android开发的新手,现在我很难获得当前的用户位置。
清单:
HAS_PROFILE
MainActivity:
<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"/>
Logd返回int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
Log.d("Check if permission", "permission is: " + permissionCheck);
我做错了什么呢?
使用Android Studio,target-sdk为23
答案 0 :(得分:4)
checkSelfPermission()
将检查用户是否已授予权限。如果检查失败(在您的情况下为-1),则需要使用requestPermissions()
对它的一个很好的解释,以及如何检查,通知和请求权限的示例,在android docs(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)