2016-04-29 151 views
-5

什麼是可用於處理Android Marshmallow運行時權限的最佳Android庫,同時確保向後兼容性,以便應用程序在運行Android IceCreamSandwich或Gingerbread的設備上不會中斷?處理Android運行時權限

+0

您可以檢查我創建的類並在碎片中使用它https://github.com/mptrista/PermissionHelper 它用於碎片並且在請求單個權限的情況下。 – toshkinl

回答

0

您可以處理使用運行權限onRequestPermissionsResult

@Override 
public void onRequestPermissionsResult(int requestCode, 
    String permissions[], int[] grantResults) { 
switch (requestCode) { 
    case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      // permission was granted, yay! Do the 
      // contacts-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 
} 
} 

詳情refer here

+0

我不想爲授予權限編寫太多的代碼。有像Permiso和Nammu這樣的外部庫可以幫助減少處理運行時權限的代碼量。我想知道哪個外部庫以最好的方式處理運行時權限 –

0

使用PermissionUtil庫使生活很容易。這裏是你如何申請許可並作用於用戶的響應:

mRequestObject = PermissionUtil.with(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).onAllGranted(
       new Func() { 
        @Override protected void call() { 
         //Happy Path 
        } 
       }).onAnyDenied(
       new Func() { 
        @Override protected void call() { 
         //Sad Path 
        } 
       }).ask(REQUEST_CODE_STORAGE); 

,並呼籲您的活動的onRequestPermissionsResult這種方法:

mRequestObject.onRequestPermissionsResult(requestCode, permissions, grantResults); 

不要忘記添加體現的權限。