Android Developer Documentation给出了在运行时请求权限的示例:
// 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”是什么?它说这是一个app定义的int常量,但这是否意味着我应该创建一个Constants.java并声明一个公共的静态int?价值应该是多少?
在其他示例中,我看到人们在这里使用1,或0或0xFFEEDDCC,但我无法找到它的解释.有人可以向我解释一下需要去哪里以及为什么? (在我的情况下,我需要确保应用程序有权访问正常位置)
ActivityCompat文档说“应用程序特定的请求代码与报告给onRequestPermissionsResult的结果相匹配”?这对我没有帮助.