代码之家  ›  专栏  ›  技术社区  ›  Arora

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

  •  0
  • Arora  · 技术社区  · 8 年前

    我有一个自定义按钮类,在该类中我请求了按钮单击的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 回复  |  直到 8 年前
        1
  •  1
  •   CommonsWare    8 年前

    我无法在onRequestPermissionResult()中获取用户结果

    是的,你是。它将去 onRequestPermissionResult() 所代表的任何活动 mContext .

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

    也就是说,欢迎您将活动向前推进 onRequestPermissionResult() 如果希望保留更多现有代码,则将结果添加到视图中。