2016-04-21 159 views
1

我正在使用xamarin plateform的android應用程序。我已經從應用程序清單啓用了應用程序的相機功能。運行應用程序用戶後,從應用程序權限屏幕中禁用相機。那麼,我如何才能讓該用戶從應用程序權限中禁用此功能?如何獲得相機權限是在Android應用程序的應用程序權限啓用?

我想下面的代碼來獲取它,但每次我只得到「授予」的結果。如果用戶禁用權限,那麼我應該得到「拒絕」的結果。

var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName); 

enter image description here

回答

0

在Android棉花糖,你需要在運行時要求的權限。您可以使用Permissions Plugin for Xamarin來提示您需要的權限。

閱讀在Requesting Runtime Permissions in Android Marshmallow

這裏更多的細節是一個例子:

var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera); 
if (status == PermissionStatus.Granted) 
{ 
    //Permission was granted 
} 

您可以在Permissions Plugin for Xamarin ReadMe

+0

感謝您的答覆。我已經安裝了它。你知道我應該用什麼代碼來獲得許可結果嗎? – anand

+0

感謝您回覆Giorgi。但Permission.Camera不可用。在權限下,只有兩個值被授權和拒絕可用。我試着用Manifest.Permission.Camera,但它的拋出錯誤,它是不正確的值,應該在這種方法paas。 – anand

+0

我已經完成了編碼部分,但在每種情況下仍然獲得「授予」的價值。如果我從應用程序權限屏幕禁用相機權限,那麼我仍然得到授予結果。你有什麼想法我可能做錯了什麼? – anand

2

申請查看更多詳細信息,你需要

如果您的應用程序的權限尚未擁有所需的權限,應用程序必須調用其中一個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

相關問題