Android M 权限:自定义按钮中未调用 onRequestPermissionsResult() class

Android M Permissions: onRequestPermissionsResult() not being called in custom button class

我有一个自定义按钮 class,我在其中请求了对按钮 click.Everything 的 CALL_PHONE 权限很好,但我没有在 onRequestPermissionResult() 中获得用户结果。在下方我正在发布我的自定义按钮 class。

public class CustomCallExecutiveButton extends Button implements View.OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback {

Context mContext;
private Button mCallButton;
private int REQUEST_CALL = 1;

public CustomCallExecutiveButton(Context context) {

    super(context);
    mContext = context;
    init();
}

public CustomCallExecutiveButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    init();
}

public CustomCallExecutiveButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomCallExecutiveButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    mContext = context;
    init();
}


private void init() {
    setOnClickListener(this);
}

@Override
public void onClick(View v) {

    CheckCallPermission();


}
public void CheckCallPermission() {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();
    } else {
        intentToCall();
    }
}

public void intentToCall() {
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + "9069118117"));
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mContext.startActivity(callIntent);
}

public void requestCameraPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext,Manifest.permission.CALL_PHONE)) {
        openCallPermissionDailog();

    } else {
        openCallPermissionDailog();
    }
}

public void openCallPermissionDailog() {

    //Requesting camera and storage permission dailog
    android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(mContext);
    alert.setTitle("Permission");
    alert.setMessage("Phone State Permission Needed To Make Call.");
    alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //when user clicks on yes onRequestPermissionResult tells whether user granted permission or not.
            ActivityCompat.requestPermissions((Activity) mContext,
                    new String[]{Manifest.permission.CALL_PHONE},
                    REQUEST_CALL);
        }
    });

    alert.show();
}

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

    if (requestCode == REQUEST_CALL) {

        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Camera permission has been granted, preview can be displayed
            intentToCall();
        }


    }

}
}

在此先感谢大家:)

I am not getting the user result in onRequestPermissionResult()

是的,你是。它将转到 mContext.

表示的任何 activity 的 onRequestPermissionResult()

恕我直言,您的行为违反了关注点分离。更像是控制器的东西,例如 activity 或片段,负责检查和请求权限,而不是视图。

也就是说,如果您希望保留更多现有代码,欢迎 activity 将 onRequestPermissionResult() 结果转发到视图。