2017-08-15 29 views
0

以下是我的代碼,我稱這個代碼爲獲取運行時權限。在這種情況下,「shouldshowrequestpermissionrationale始終返回false」。我無法找到解決方案,爲什麼它是這樣的goind。由於此,運行時權限警報不顯示。推薦我一個解決方案請...運行時權限:shouldshowrequestpermissionrationale總是返回false

private void checkRuntimePermission() { 

    Logger.infoLog("checkRuntimePermission"); 
    if(ActivityCompat.checkSelfPermission(this, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED){ 
     Logger.infoLog("checkRuntimePermission first if"); 
     if(ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this,permissionsRequired[0])){ 
      Logger.infoLog("checkRuntimePermission if"); 
      //just request the permission 
      //Show Information about why you need the permission 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Need Multiple Permissions"); 
      builder.setMessage("This app needs Camera and Location permissions."); 
      builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
        ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT); 
       } 
      }); 
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
      builder.show(); 
     }else{ 
      Logger.infoLog("Show request permission rationale false"); 
     } 
    } else { 
     //You already have the permission, just go ahead. 
     Logger.infoLog("Permission given already"); 
     proceedAfterPermission(); 
    } 
} 

回答

0

要調用被問這個問題的方法,「我們應該表現出請求此權限的原因嗎?」

從文檔"This method returns true if the app has requested this permission previously and the user denied the request."https://developer.android.com/training/permissions/requesting.html

如果該值是假的,你還是想請求的權限,但你並不需要以示警告對話框。所以,在else塊只是簡單

ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT); 
1

在谷歌文檔:「如果應用程序已請求此權限以前與用戶拒絕了這一要求該方法返回true」

所以,你應該調用requestPermissions(...)第一,然後使用shouldShowRequestPermissionRationale(...)可以得到你想要的結果。

最好的方法是使用始終requestPermissions(...)onRequestPermissionsResult(...)