我正在尝试调用用户在运行时启用其写入外部存储权限。我使用了以下代码。它显示运行时权限对话框。当我点击允许或拒绝时,它不会调用onReqestPermissionResult(回调方法)方法。
我必须为此做些什么?
public class PermissionCheck extends Activity {
private static final String TAG = "PermissionCheck";
private Fragment mFragment;
private Activity mActivity;
public PermissionCheck(Fragment fragment){
mFragment = fragment;
mActivity = fragment.getActivity();
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
return true;
} else {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
}
}
}
答案 0 :(得分:1)
您正在requestPermissions
中传递另一个活动的实例,因此onRequestPermissionResult
会调用mActivity
,而不是PermissionCheck
。尝试将requestPermissions
更改为:
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
答案 1 :(得分:0)
替换:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
使用:
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
将onRequestPermissionsResult()替换为:
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);