Android M Permissions:onRequestPermissionsResult()未在自定义按钮类

时间:2016-04-28 12:34:27

标签: android runtime android-6.0-marshmallow user-permissions

我有一个自定义按钮类,我在按钮点击时请求了CALL_PHONE权限。一切都很好,但我没有得到onRequestPermissionResult()的用户结果。下面我发布了我的自定义按钮类。

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();
        }


    }

}
}

先谢谢大家:)

1 个答案:

答案 0 :(得分:1)

  

我没有在onRequestPermissionResult()

中获得用户结果

是的,你是。它将转移到onRequestPermissionResult()表示的任何活动的mContext

恕我直言,你所做的是违反关注点分离。更像是控制器的东西,比如活动或片段,负责检查和请求权限,而不是视图。

话虽如此,如果您希望保留更多现有代码,欢迎您将活动转发给onRequestPermissionResult()结果。