从拨号盘呼叫号码 returns 警告:"Call requires permission which may be rejected by user"

Calling number from dialpad returns warning: "Call requires permission which may be rejected by user"

当用户单击具有号码的 TextView 时,我尝试基本上拨打一个号码:

number_title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
                        activity.startActivity(callIntent);
                        //the above line returns the warning given below
                    }
});

我收到的警告:

Call requires permission which may be rejected by user. Code to explicitly check to see if the permission is available

我了解如果应用之前未获得权限,则需要申请权限。我的问题是,如何在调用之前明确请求权限?

使用这个可能会起作用:

 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

这只是检查权限的参考调用示例:

if(!checkSelfPermission(Manifest.permission. CALL_PHONE)==PackageManager.PERMISSION_GRANTED){

    requestPermissions(this,new String {}(Manifest.permission. CALL_PHONE),INT_TO_CHECK RESULT);

}

让我知道这是否可行。

已编辑:对代码进行了更改以进行解释。

好吧,这是关于运行时权限的,因此仅在清单中声明它们是行不通的。相反,您必须在开始通话之前检查权限。

像这样的事情应该可行——如果用户之前没有授予权限(或者如果他撤销了权限),只会询问用户权限:

private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1234;

public void yourfunction() {
    number_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ContextCompat.checkSelfPermission(thisActivity,
                                                Manifest.permission.CALL_PHONE)
                                != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(activity,
                            new String[]{Manifest.permission.CALL_PHONE},
                            MY_PERMISSIONS_REQUEST_CALL_PHONE);
                    } else {
                        executeCall();
                    }
                }
    });
}

private void executeCall() {
    // start your call here
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
            activity.startActivity(callIntent);         
        }
    });
}


@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                executeCall();


            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
    }
}

以上代码主要取自google: http://developer.android.com/training/permissions/requesting.html 如果您想了解更多信息,请访问 link。

编辑: onRequestPermissionsResult 将是您 activity 中的回调函数。您可能需要在那里处理电话。