2016-10-22 97 views
0

我正在開發一個應用程序,該應用程序顯示用於添加新筆記的筆記和按鈕的自定義列表視圖。當用戶點擊按鈕時,應用程序會顯示一個全屏對話框來添加註釋以及一些額外的細節。用戶也可以上傳附件並點擊附件按鈕。但問題出在Android M(API 23)此任務需要運行時權限。從Android中的自定義對話框請求運行時權限

根據Google Developers,許可請求的結果將在onRequestPermissionsResult()方法中傳遞。我不知道如何在我用來顯示全屏對話框的方法中獲得此結果。

這是我的方法看起來像:

private void showCreateNoteDialog() { 

    //create dialog body... 
    final Dialog createDialog = new Dialog(NotesActivity.this,R.style.MyFullscreenDialog); 
    createDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    createDialog.setContentView(inflater.inflate(R.layout.customdialog_createNote, null)); 
    createDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);} 

編輯:

的許可需要上傳文件讀從SD卡或外部存儲

+0

你不必問權限,以顯示一個對話框。試圖顯示對話框時,你的應用程序崩潰了嗎? –

+0

你試圖問什麼許可? – Sanjeet

+0

@SrikarReddy對不起,我的問題的種類描述,但我需要從用戶的SD卡讀取上傳附件與注意的權限。 – Intimate

回答

0

嘛運行時權限的集成可以通過一些有用的庫更容易,我最喜歡的一個是PermissionUtils

所有你需要做的是編譯依賴於應用級build.gradle

dependencies { 
    compile 'rebus:permission-utils:1.0.3' 
} 

然後在活動

PermissionManager.with(YourActivity.this) 
    .permission(PermissionEnum.READ_PHONE_STATE, PermissionEnum.READ_EXTERNAL_STORAGE) // You can put all permissions here 
    .askagain(true) 
    .ask(); 

和多數民衆贊成它的onCreate。你可以找到更多的信息here

希望它有幫助。

0

嘗試類似這樣的事情。

if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.READ_EXTERNAL_STORAGE) 
    != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_EXTERNAL_STORAGE)) { 

     // Show an explanation 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_EXTERNAL_STORAGE}, 
      MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 

     // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

這是如何實現運行許可。欲瞭解更多信息,請查詢official developer site。另外,請看Normal and Dangerous Permissions以瞭解更多關於在運行時需要詢問哪些權限的信息。

+0

獲取結果怎麼樣?如何在我們的方法中獲得結果? – Intimate

+0

一旦請求被用戶接受,您可以使用'FileInputStream'從SD卡讀取數據。我希望這可以幫助http://stackoverflow.com/a/10550036/4402462 –

0

你不能得到的結果直在你showCreateNoteDialog方法,但是這是你可以做的(僞代碼):

showCreateNoteDialog() { 
    /* some code goes here */ 
    if (checkPermission) { 
     newMethodCalledFromNoteDialog(); 
    } else { 
     requestPermission(); 
     // return? 
    } 
} 

newMethodCalledFromNoteDialog() { 
     // part of code which needs permission 
     // some code depending on previous code 
} 

onRequestPermissionsResult() { 
    if (permissionGranted) { 
     newMethodCalledFromNoteDialog(); 
    } 
} 
相關問題