问题
I would like to know whether the runtime permission popup is visible or not in the activity. How can i know that? Is there any callback or overrided method for the same.
回答1:
You request permissions yourself so you know when it is shown. You can also add a result callback. See the code below;
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;
}
}
This would generally sit inside some kind of base Activity
class.
来源:https://stackoverflow.com/questions/36311448/runtime-permission-on-android-marshmallow