gpt4 book ai didi

android - PhoneStateListener 无法使用广播接收器工作

转载 作者:可可西里 更新时间:2023-11-01 11:43:05 27 4
gpt4 key购买 nike

我正在尝试使用 Broadcast Receiver 测试 PhoneStateListener。但我无法这样做。接收类中的日志语句也没有在 Logcat 中打印。

这是 list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suraj.phonelistener">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneStateBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>

这是 PhoneStateBroadcastReceiver.java 文件

package com.example.suraj.phonelistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Log.d("DEBUG","########");
// above line is also not printing in Logcat

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}

public class CustomPhoneStateListener extends PhoneStateListener {

//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Log.d("DEBUG","IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Log.d("DEBUG","OFFHOOK");
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
Log.d("DEBUG","RINGING");
break;
default:
break;
}
}
}
}

一定是什么问题?

最佳答案

这可能是因为你的应用目标API级别是23,也就是android M(6.0)。6.0 中的用户权限不同您可以将 API 级别从 23 更改为任何较低版本或遵循 android M 的规则。这是 link Android M 权限的文章

关于android - PhoneStateListener 无法使用广播接收器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com