本文介绍了广播接收器调用但PhoneStateListener不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试code以下,BroadcastReceiverCustom正在
所谓但是PhoneStateListenerCustom不是。

现在我只有在BroadcastReceiverCustom做必要的行动,
但maynot要做到这一点最好的地方。
任何建议,为什么不PhoneStateListener被称为?
已经花了很多时间了解可能的原因,也不知道?
清单文件是使用正确的权限正确的。
我看没有运行时异常。

BroadcastReceiverCustom.java

 公共类BroadcastReceiverCustom扩展广播接收器{私有静态最后弦乐TAG =BroadcastReceiverCustom;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        Log.v(TAG,我们现在需要在!!!!!!!!!!!);        TelephonyManager电话=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListenerCustom phoneStateListenerCustom =新PhoneStateListenerCustom();
        telephony.listen(phoneStateListenerCustom,PhoneStateListener.LISTEN_CALL_STATE);
    }
}

PhoneStateListenerCustom.java

 公共类PhoneStateListenerCustom扩展PhoneStateListener {    私有静态最后弦乐TAG =PhoneStateListenerCustom;    公共无效onCallStateChange(INT状态,弦乐incomingNumber){        Log.v(TAG,我们现在需要在!!!!!!!!!!!);
        开关(州){
            案例TelephonyManager.CALL_STATE_RINGING:
                 Log.d(TAG,振铃);
                 打破;
        }        super.onCallStateChanged(州,incomingNumber);    }
}

清单文件

 <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
<接收机器人:名字=。BroadcastReceiverCustom>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.PHONE_STATE/>
    &所述; /意图滤光器>
< /接收器>


解决方案

你不应该实例中的BroadcastReceiver的TelephonyManager和PhoneStateListener。这里是一些例子code:

在活动:

  EndCallListener callListener =新EndCallListener;
TelephonyManager MTM =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener,PhoneStateListener.LISTEN_CALL_STATE);私有类EndCallListener扩展PhoneStateListener {
    @覆盖
    公共无效onCallStateChanged(INT状态,弦乐incomingNumber){
        如果(TelephonyManager.CALL_STATE_RINGING ==州){
            Log.i(LOG_TAG +监听器​​,响个不停,数:+ incomingNumber);
        }
        如果(TelephonyManager.CALL_STATE_OFFHOOK ==州){
            Log.i(LOG_TAG +监听器​​,摘机,数:+ incomingNumber);
        }
        如果(TelephonyManager.CALL_STATE_IDLE ==州){
            Log.i(LOG_TAG +监听器​​,IDLE,数:+ incomingNumber);
        }
    }
}

While debugging the code below, BroadcastReceiverCustom is beingcalled but PhoneStateListenerCustom is not.

For now I and doing required action in BroadcastReceiverCustom only,but maynot be best place to do it.Any suggestions why PhoneStateListener not being called??Already spent lot of time for possible reasons, no idea??Manifest file is all correct with correct permissions.I see no runtime exceptions.

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);

    }
}

Manifest file

<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>
解决方案

you should not instantiate the TelephonyManager and PhoneStateListener in the BroadcastReceiver. here is some example code:

in 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);
        }
    }
}

这篇关于广播接收器调用但PhoneStateListener不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:05