public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.d("ervin", incomingNumber);
phone = incomingNumber;
StringBuilder sb = new StringBuilder();
sb.append("phone").append("&").append("com.android.phone").append("&").append(phone);
service.writeData(sb.toString());
}else if(TelephonyManager.CALL_STATE_IDLE== state){
StringBuilder sb = new StringBuilder();
sb.append("endphone").append("&").append("com.android.phone").append("&").append(phone);
service.writeData(sb.toString());
}
}
}, PhoneStateListener.LISTEN_CALL_STATE); //listener
}
这是我的接收器代码收听来电和呼叫结束。 当呼叫到来时,TelephonyManager.CALL_STATE_RINGING将多次调用,而我的Log.d()将多次打印。
我的问题是我怎样才能使TelephonyManager.CALL_STATE_RINGING状态在呼叫来电时调用一次