gpt4 book ai didi

android - 调用了 BroadcastReceiver 但未调用 PhoneStateListener

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:29 25 4
gpt4 key购买 nike

在调试下面的代码时,BroadcastReceiverCustom 正在已调用但 PhoneStateListenerCustom 未调用。

现在我只在 BroadcastReceiverCustom 中执行所需的操作,但可能不是最好的地方。有什么建议为什么不调用 PhoneStateListener 吗?由于可能的原因已经花了很多时间,不知道?? list 文件都是正确的,具有正确的权限。我没有看到运行时异常。

BroadcastReceiverCustom.java

public class BroadcastReceiverCustom extends BroadcastReceiver {

private static final String TAG = "BroadcastReceiverCustom";

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

Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListenerCustom phoneStateListenerCustom = new PhoneStateListenerCustom();
telephony.listen(phoneStateListenerCustom, PhoneStateListener.LISTEN_CALL_STATE);
}
}

PhoneStateListenerCustom.java

public class PhoneStateListenerCustom extends PhoneStateListener {

private static final String TAG = "PhoneStateListenerCustom";

public void onCallStateChange(int state, String incomingNumber){

Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
}

super.onCallStateChanged(state, incomingNumber);

}
}

list 文件

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".BroadcastReceiverCustom">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

最佳答案

您不应在 BroadcastReceiver 中实例化 TelephonyManager 和 PhoneStateListener。这是一些示例代码:

在 Activity 中:

EndCallListener callListener = new EndCallListener;
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG + "Listener", "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG + "Listener", "OFFHOOK, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(LOG_TAG + "Listener", "IDLE, number: " + incomingNumber);
}
}
}

关于android - 调用了 BroadcastReceiver 但未调用 PhoneStateListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4700109/

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