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

Posted

技术标签:

【中文标题】Android M 权限:onRequestPermissionsResult() 未在自定义按钮类中调用【英文标题】:Android M Permissions: onRequestPermissionsResult() not being called in custom button class 【发布时间】:2016-04-28 12:34:27 【问题描述】:

我有一个自定义按钮类,我在其中请求按钮单击的 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】:

我没有在 onRequestPermissionResult() 中获得用户结果

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

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

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

【讨论】:

这意味着我无法在自定义类中获得结果。我需要在使用自定义类的所有活动中实现 onRequestPermissionResult。

以上是关于Android M 权限:onRequestPermissionsResult() 未在自定义按钮类中调用的主要内容,如果未能解决你的问题,请参考以下文章

Android M(6.0) 权限爬坑之旅

Android M 动态权限获取

Android 6(M) 权限问题(创建目录不起作用)

Android M请求权限非活动

Android M:以编程方式撤销权限

Android M:以编程方式撤销权限