我的广播被调用但没有调用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()方法中,这是我的类扩展PhoneStateListener 在onReceive()之外,但在广播课上。
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
任何与之相关的建议都将被接受 提前致谢
答案 0 :(得分:2)
在调用回调之前,您的服务正在清理。在BroadcastReceiver
退出后,您不应该真正依赖onReceive
中分配的任何内容。您应该将CustomPhoneStateListener
放入Service
或Activity
。然后,您可以使用Intent
启动活动或服务来进行状态监控。
来自BroadcastReceiver
文档,
从onReceive()返回后,BroadcastReceiver不再存在 活跃,其托管过程与其他任何一样重要 在其中运行的应用程序组件。这是特别的 重要的是因为如果那个过程只是托管的那个 BroadcastReceiver(用户拥有的应用程序的常见情况) 从来没有或最近没有与之互动过,然后从...返回 onReceive()系统会认为它的进程是空的 积极地杀死它,以便资源可用于其他更多 重要的过程。
它在仿真器上运行的原因可能是因为仿真器上的进程通常较少,并且出于某种原因,它似乎没有那么积极地杀死进程。
答案 1 :(得分:0)
上述问题的解决方案是:
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
String myNumber = tm.getLine1Number();
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
if(Status!=""){
Toast.makeText(mContext,"Call Ends " + incomingNumber,Toast.LENGTH_LONG).show();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Status = "RINGING";
if (incomingNumber.startsWith("00")) {
Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
callState = "International - Ringing (" + incomingNumber+ ")";
} else {
Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
callState = "Local - Ringing (" + incomingNumber + ")";
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
try{
String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if(dialingNumber==null){
Status = "Recieve";
Toast.makeText(mContext, "Recieve call", Toast.LENGTH_LONG).show();
}else{
Status = "Dialing";
if (dialingNumber.startsWith("00")) {
Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "International - Dialing (" + dialingNumber+ ")";
} else {
Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "Local - Dialing (" + dialingNumber + ")";
}
}
}catch(Exception e){}
break;
}
Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};