如何在Android Oreo和Pie中检测来电

时间:2019-05-02 09:29:13

标签: android broadcastreceiver android-8.0-oreo android-9.0-pie incoming-call

我正在开发一个应用程序,能够检测到棉花糖的来电。但是在Oreo和Pie中,由于广播接收器未触发,因此我无法检测到传入呼叫。请帮帮我。

1 个答案:

答案 0 :(得分:0)

在Android 6.0以上版本中,即使您在AndroidManifest.xml文件中设置了权限,您仍然必须向用户明确要求他们是否属于危险权限类别。这是此类权限的列表:

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
相机
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CELL_BROADCASTS
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG
WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE
要请求这种权限,这里是您可以使用的代码(我在onCreate方法的MainActivity.java中使用了它):

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED) {
        String[] permissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE};
        requestPermissions(permissions, PERMISSION_REQUEST_READ_PHONE_STATE);
    }
}

PERMISSION_REQUEST_READ_PHONE_STATE变量用于确定onRequestPermissionsResult方法中要求的权限。当然,如果不需要根据用户是否批准该权限执行任何逻辑,则可以省略此方法:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_READ_PHONE_STATE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission NOT granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            }

            return;
        }
    }
}

https://dev.to/hitman666/how-to-make-a-native-android-app-that-can-block-phone-calls--4e15的副本

更多信息https://developer.android.com/training/permissions/requesting?hl=es-419