我有一个自定义按钮类,我在按钮点击时请求了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();
}
}
}
}
先谢谢大家:)
答案 0 :(得分:1)
我没有在onRequestPermissionResult()
中获得用户结果
是的,你是。它将转移到onRequestPermissionResult()
表示的任何活动的mContext
。
话虽如此,如果您希望保留更多现有代码,欢迎您将活动转发给onRequestPermissionResult()
结果。