我的广播被调用但没有调用 PhoneStateListener 我的代码在模拟器中运行时工作正常但是当我在实际设备上尝试它时 PhoneStateListener 从来没有接到电话,我对这个问题发疯了这是我的代码:
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
上面的代码在 onReceive() 方法中,这是我的类在 onReceive() 之外扩展 PhoneStateListener 但在广播类中。
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
@Override
public void onCallStateChanged(int state, String phonenumber){
if(phonenumber!=null && phonenumber.length()>0)
incoming_nr=phonenumber;
act=new Call_RecorderActivity();
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_IDLE:
prev_state=state;
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
break;
// Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
// |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
} //switch close
}
}
logcat 在来电时显示此内容
01-22 11:25:12.529: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=168]
01-22 11:25:12.539: I/IncomingCallReceiver(1463): State: RINGING
01-22 11:25:12.539: I/IncomingCallReceiver(1463): Incomng Number: +9184848xxxx2
01-22 11:25:12.779: D/CustomPhoneStateListener(1463): CALL_STATE_IDLE==>+9184848xxxx2
01-22 11:25:16.299: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=92]
01-22 11:25:16.299: I/IncomingCallReceiver(1463): State: OFFHOOK
01-22 11:25:18.849: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=88]
01-22 11:25:18.849: I/IncomingCallReceiver(1463): State: IDLE
任何与之相关的建议都将被接受提前谢谢