如何在适配器中获得权限回调

时间:2018-02-21 08:01:48

标签: android permissions

我有ArrayAdapter我需要选择图片...但首先我需要检查权限是否被授予...我如何获得callback OnPermissionsResult在我的适配器中。

1 个答案:

答案 0 :(得分:2)

首先在适配器中创建一个方法

public void getResult(Boolean value) {
  if (value == true) {
  //do smth 
  } else {
  //do smth else
  }
}

然后在您的活动中

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

          // 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.  

                adapter.getResult(true);  // call method via adapter instance       
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                adapter.getResult(false);
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
相关问题