申請查看更多詳細信息,你需要
如果您的應用程序的權限尚未擁有所需的權限,應用程序必須調用其中一個requestPermissions()方法請求適當的權限。您的應用會傳遞所需的權限,並且還會指定一個整數請求代碼,用於標識此權限請求。此方法異步運行:它立即返回,並且在用戶響應對話框後,系統調用應用程序的回調方法和結果,並將相同的請求代碼傳遞給requestPermissions()。*
int MY_PERMISSIONS_REQUEST_Camera=101;
// Here, thisActivity is the current activity
if (ContextCompat.CheckSelfPermission(thisActivity,
Manifest.Permission.Camera)
!= Permission.Granted) {
// Should we show an explanation?
if (ActivityCompat.ShouldShowRequestPermissionRationale(thisActivity,
Manifest.Permission.Camera)) {
// 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.Camera},
MY_PERMISSIONS_REQUEST_Camera);
// MY_PERMISSIONS_REQUEST_Camera is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
處理的權限請求響應
當你的應用程序請求的權限,系統會顯示一個對話框給用戶。當用戶響應時,系統調用您的應用程序的OnRequestPermissionsResult()方法,並將其傳遞給用戶響應。您的應用必須重寫該方法才能確定是否授予了該權限。該回調將傳遞給您傳遞給requestPermissions()的相同請求代碼。例如,如果一個應用程序請求訪問攝像頭可能有以下回調方法
public override void OnRequestPermissionsResult(int requestCode,
string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_Camera: {
// If request is cancelled, the result arrays are empty.
if (grantResults.Length > 0 && grantResults[0] == Permission.Granted) {
// permission was granted, yay! Do the
// camera-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
例如上述基於谷歌原許可documentions
感謝您的答覆。我已經安裝了它。你知道我應該用什麼代碼來獲得許可結果嗎? – anand
感謝您回覆Giorgi。但Permission.Camera不可用。在權限下,只有兩個值被授權和拒絕可用。我試着用Manifest.Permission.Camera,但它的拋出錯誤,它是不正確的值,應該在這種方法paas。 – anand
我已經完成了編碼部分,但在每種情況下仍然獲得「授予」的價值。如果我從應用程序權限屏幕禁用相機權限,那麼我仍然得到授予結果。你有什麼想法我可能做錯了什麼? – anand