问题描述
|
我使用PhoneStateListener来查找电话何时处于活动呼叫中以及何时呼叫结束并记录它是否处于传入或传出呼叫中。当我安装侦听器时,它会在第一个呼叫中按预期工作。之后,尽管它仍然可以工作,但它会同时返回第一个电话和第二个电话的信息,而在第三个电话中,它将返回3个电话的信息,依此类推。
我如何只获取当前通话的信息?下面的代码,任何帮助表示赞赏
@Override
public void onReceive(Context context,Intent intent) {
mContext = context;
mIntent = intent;
mlocManager= (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener,events);
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state,String incomingNumber) {
Log.e(\"GO\",incomingNumber);
String number = null;
String callState = \"UNKNOWN\";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = \"IDLE\";
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
callState = \"INCOMING\";
number = incomingNumber;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
number = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
callState = \"OUTGOING\";
break;
}
super.onCallStateChanged(state,incomingNumber);
}
};
解决方法
回答我自己的问题。此代码为每个呼叫创建多个侦听器。通话结束后,我必须停止监听器
tm.listen(phoneStateListener,PhoneStateListener.LISTEN_NONE);