如何删除 Android Studio 中的 "call requires permission which may.." 错误

how to remove "call requires permission which may.." error in Android Studio

我的代码

private void continueCall() {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + phoneNoToCall));
        startActivity(callIntent);
}

以上要求用户是否在运行时间授予权限CALL_PHONE,我检查为

private void call() {
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
        continueCall();
    } else {
        requestCallPermission();
    }
}

仍然Android工作室显示错误"call requires permission which may..."

如何消除这个错误?或者至少压制它。

我认为您需要 CALL_PRIVILEGED 许可:

CALL_PRIVILEGED

Added in API level 1

String CALL_PRIVILEGED Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed.

Not for use by third-party applications.

Constant Value: "android.permission.CALL_PRIVILEGED"

可以从多个地方调用方法 continueCall()。所以不能保证在 call()

中进行权限检查

所以你只能通过添加来抑制错误:

@SuppressWarnings("MissingPermission")

你需要把

 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + phoneNoToCall));
 startActivity(callIntent);

在 if 块内

private void call() {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + phoneNoToCall));
        startActivity(callIntent);
   } else {
        requestCallPermission();
   }
}