从活动外部捕获onRequestPermissionsResult()

时间:2018-09-19 13:18:18

标签: java android android-permissions

我目前正在尝试学习android以及如何制作android应用程序。 经过大量研究,我找不到正确处理权限的好方法。

我想要的是能够从任何活动中请求权限,并检测用户是否接受或拒绝了它。

我在这里:

我有一个MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
   final Activity activity = this;
   super.onCreate(savedInstanceState);

   new PermissionHandler(activity).setPermissionListener(new PermissionHandler.PermissionAccordedListener() {
      @Override public void permissionAccorded() {
          // Permission Accorded 
      }
   }).askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

   //...
}

还有一个PermissionHandler.java类

public class PermissionHandler implements  ActivityCompat.OnRequestPermissionsResultCallback {
    private final Activity activity;
    private final int CODE = 12;

    public interface PermissionAccordedListener {
        void permissionAccorded();
    }

    public PermissionHandler setPermissionListener(PermissionAccordedListener permissionListener) {
        this.permissionAccorded = permissionListener;
        return this;
    }

    private PermissionAccordedListener permissionAccorded;

    public PermissionHandler(Activity activity) {
        this.activity = activity;
    }

    public void askForPermission(String permission) {
        checkForPermission(activity, permission);

    }

    private void checkForPermission(Activity activity, String permission) {
        if (ContextCompat.checkSelfPermission(activity,
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    permission)) {
                // 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.
                ActivityCompat.requestPermissions(activity,
                        new String[]{permission},
                        CODE);
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(activity,
                        new String[]{permission},
                        CODE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            if (permissionAccorded != null) {
                permissionAccorded.permissionAccorded();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (permissionAccorded != null) {
                        permissionAccorded.permissionAccorded();
                    }
                } else {
                    // permission denied! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request.
        }
    }

}

除了onRequestPermissionsResult从未触发之外,它的工作原理非常好。

首先,这是个好方法吗? 如果是,我在做什么错? 如果没有,那么那里是否有任何资源可用于此类工作?

1 个答案:

答案 0 :(得分:0)

您可以使用处理权限的Dexter库,也可以处理多个权限 请参阅文档以获取更多详细信息 https://github.com/Karumi/Dexter