onRequestPermissionsResult在片段下编译

时间:2018-10-18 05:53:22

标签: android android-fragments android-permissions android-6.0-marshmallow

我正在从片段请求我的应用程序的运行时权限。我可以打电话给requestPermissions。

 requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE_REQUEST_CODE);  

但是当我添加onRequestPermissionsResult时,其未编译并显示“ Method不会从其超类覆盖方法。

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
}

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

enter image description here>位于onCreateView内

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getActivity().requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
    }
  

内部片段类

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

}

答案 1 :(得分:0)

在onCreateView()方法中添加以下代码以请求权限

 ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, 1);

AND

    @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]);
        }
    }

还要在清单中添加:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>