2016-04-29 78 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); 

不要忘记添加体现的权限。