Android Developer Documentation給在運行時請求權限的這個例子:Android的運行permissions-如何實現
// 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常量,但這是否意味着我應該創建一個Constants.java並聲明一個公共靜態int?價值應該是什麼?
在其他例子中,我看到人們在這裏使用1,或者0或者0xFFEEDDCC,但是我找不到它是什麼的解釋。有人可以向我解釋什麼需要去這裏,爲什麼? (在我的情況下,我需要確保應用程序有權訪問正確的位置)
ActivityCompat文檔中提到「特定於應用程序的請求代碼與報告給onRequestPermissionsResult的結果相匹配」?這對我沒有幫助。
[Android權限 - 艙單或android.manifest]的可能的複製(http://stackoverflow.com/questions/34901870/android-permissions-manifest-or-android-manifest) – JpCrow
您可以檢查我創建了一個類,並使用它的碎片github.com/mptrista/PermissionHelper這是一個片段,並在請求一個許可的情況下。 – toshkinl