安卓运行时权限 - 如何实现
Android 开发人员文档给出了在运行时请求权限的示例:
// 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.
}
}
此示例中的“MY_PERMISSIONS_REQUEST_READ_CONTACTS”是什么?它说它是一个应用程序定义的int常量,但这是否意味着我应该做一个常量.java并声明一个公共静态int?值应该是多少?
在其他例子中,我看到人们在这里使用1,或0或0xFFEEDDCC,但我找不到它是什么的解释。有人可以向我解释需要去哪里以及为什么吗?(在我的情况下,我需要确保应用程序有权访问精细位置)
ActivityCompat 文档显示“应用程序特定的请求代码,以匹配报告给 onRequestPermissionsResult 的结果”?这对我没有帮助。