Android 9 来电监听incomingNumber为空

为了增强用户隐私,Android 9 引入了若干行为变更,如限制后台应用访问设备传感器、限制通过 Wi-Fi 扫描检索到的信息,以及与通话、手机状态和 Wi-Fi 扫描相关的新权限规则和权限组。

限制访问电话号码

在未首先获得 READ_CALL_LOG 权限的情况下,除了应用的用例需要的其他权限之外,运行于 Android 9 上的应用无法读取电话号码或手机状态。

与来电和去电关联的电话号码可在手机状态广播(比如来电和去电的手机状态广播)中看到,并可通过 PhoneStateListener 类访问。 但是,如果没有 READ_CALL_LOG 权限,则 PHONE_STATE_CHANGED 广播和 PhoneStateListener 提供的电话号码字段为空。

要从手机状态中读取电话号码,请根据您的用例更新应用以请求必要的权限:

要通过 PHONE_STATE Intent 操作读取电话号码,同时需要 READ_CALL_LOG 权限和 READ_PHONE_STATE 权限。
要从 onCallStateChanged() 中读取电话号码,只需要 READ_CALL_LOG 权限。 不需要 READ_PHONE_STATE 权限。

	<!--读取电话的状态信息的权限-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!--读取通话记录的权限-->
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener {
    private static final String TAG = "MyPhoneStateListener";
    
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d(TAG ,"电话挂断...");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d(TAG ,"正在通话...");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(TAG ,"电话响铃...");
                break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android上的来电通知是指在接到来电时,系统会在屏幕上显示一个通知,告诉用户来电的号码或联系人信息。以下是实现Android来电通知的步骤: 1. 在AndroidManifest.xml文件中添加权限: ```xml <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ``` 2. 创建一个BroadcastReceiver类,用于接收来电广播并显示通知: ```java public class CallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); showNotification(context, incomingNumber); } } private void showNotification(Context context, String incomingNumber) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText(incomingNumber) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_CALL) .setFullScreenIntent(fullScreenPendingIntent, true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build()); } } ``` 3. 在AndroidManifest.xml文件中注册BroadcastReceiver: ```xml <receiver android:name=".CallReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> ``` 4. 创建一个通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Incoming calls", NotificationManager.IMPORTANCE_HIGH); channel.setDescription("Notifications for incoming calls"); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 以上是实现Android来电通知的基本步骤,您可以根据需要进行自定义和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值