0

我想知道运行时权限弹出窗口在活动中是否可见。我怎么知道?是否有任何回调或重写方法。

4

1 回答 1

1

您自己请求权限,以便知道何时显示。您还可以添加结果回调。请参阅下面的代码;

public void requestPermissions(List<String> permissions, ActivityCompat.OnRequestPermissionsResultCallback onRequestPermissionsResultCallback) {
    String[] params = permissions.toArray(new String[permissions.size()]);
    requestPermissions(params, onRequestPermissionsResultCallback);
}

public void requestPermissions(String[] permissions, ActivityCompat.OnRequestPermissionsResultCallback onRequestPermissionsResultCallback) {
    this.onRequestPermissionsResultCallback = onRequestPermissionsResultCallback;
    ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_PERMISSIONS);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CODE_PERMISSIONS && onRequestPermissionsResultCallback != null) {
        this.onRequestPermissionsResultCallback.onRequestPermissionsResult(requestCode, permissions, grantResults);
        this.onRequestPermissionsResultCallback = null;
    }
}

这通常位于某种基Activity类中。

于 2016-03-30T14:18:52.520 回答