android Pie OS 上广播接收器电话管理器的权限

Permission for Broadcast receiver Telephony Manager on android Pie OS

我正在开发一个应用程序,该应用程序具有 OTP phone 调用以设置用户密码。所以我要做的是自动设置我从 phone 调用 textview 得到的 phone 号码。问题是 PhoneStateReceiver class 扩展了 BroadcastReceiver 不在 Pie 上工作但在 Oreo 上工作。

它在我的 Oreo phone 上工作得很好,但在 Pie phone 上却没有,我没有收到任何错误,除了在 Pie 上没有调用 PhoneStateReceiver 的 onReceive。

public class PhoneStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if((state.equals(TelephonyManager.EXTRA_STATE_RINGING))){
                Toast.makeText(context,"Received State",Toast.LENGTH_SHORT).show();
            }

            if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
                Intent local = new Intent();
                local.setAction("service.to.activity.transfer");
                local.setAction("android.intent.action.PHONE_STATE");
                local.setAction("android.intent.action.NEW_OUTGOING_CALL");
                local.putExtra("number", incomingNumber);
                context.sendBroadcast(local);

            }
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                Intent local = new Intent();
                local.setAction("service.to.activity.transfer");
                local.setAction("android.intent.action.PHONE_STATE");
                local.setAction("android.intent.action.NEW_OUTGOING_CALL");
                local.putExtra("number", incomingNumber);
                context.sendBroadcast(local);

            }
        }
    }

}

这是我的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.jpx.qur">

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

    <application
        android:name=".modules.App"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher_square"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:allowBackup">

        <receiver android:name=".utils.phoneutil.PhoneStateReceiver">
            <intent-filter>

                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>

        </receiver>

    </application>

</manifest>

这就是我在 Activity

上处理寄存器接收器的方式
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp);

        getNumberPhone();

    }

    public void getNumberPhone(){

        IntentFilter filter = new IntentFilter();
        filter.addAction("service.to.activity.transfer");
        BroadcastReceiver updateUIReciver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            //UI update here
            if (intent != null){
                String message = intent.getStringExtra("number");

                if(message != null){
                    if(message.length() == DIGITLENGTH){
                        String asubstring = message.substring(1, message.length());
                        pin1.setText(""+asubstring.charAt(0));
                        pin2.setText(""+asubstring.charAt(1));
                        pin3.setText(""+asubstring.charAt(2));
                        pin4.setText(""+asubstring.charAt(3));
                    }

                }
            }
            }
        };
        registerReceiver(updateUIReciver, filter);

    }

它在我的 Oreo phone 上工作得很好,但在 Pie phone 上却没有,我没有收到任何错误,除了在 Pie 上没有调用 PhoneStateReceiver 的 onReceive。请帮我解决这个问题

在清单中添加 READ_CALL_LOG 权限,并 获取运行时 权限:

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

根据 Android 文档:

限制访问通话记录

Android 9 introduces the CALL_LOG permission group and moves the READ_CALL_LOG, WRITE_CALL_LOG, and PROCESS_OUTGOING_CALLS permissions into this group. In previous versions of Android, these permissions were located in the PHONE permission group.

Restricted access to phone numbers Apps running on Android 9 cannot read phone numbers or phone state without first acquiring the READ_CALL_LOG permission, in addition to the other permissions that your app's use cases require.

Phone numbers associated with incoming and outgoing calls are visible in the phone state broadcast, such as for incoming and outgoing calls and are accessible from the PhoneStateListener class. Without the READ_CALL_LOG permission, however, the phone number field that's provided in PHONE_STATE_CHANGED broadcasts and through PhoneStateListener is empty.

To read phone numbers from phone state, update your app to request the necessary permissions based on your use case:

To read numbers from the PHONE_STATE intent action, you need both the READ_CALL_LOG permission and the READ_PHONE_STATE permission. To read numbers from onCallStateChanged(), you need the READ_CALL_LOG permission only. You don't need the READ_PHONE_STATE permission.

查看完整文档 here