我使用后代 - PhoneStateListener 类:
class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
if (incomingNumber!=null)
{
// code for incoming call handling
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
这是我的广播接收器:
class ServiceReceiver extends BroadcastReceiver {
CallStateListener phoneListener;
@Override
public void onReceive(final Context context, Intent intent) {
if (phoneListener == null) {
phoneListener = CallStateListener.getCallStateInstance(context);
TelephonyManagerSingletone.getInstance().getTelephonyManager().listen(CallStateListener.getCallStateInstance(context),
android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
}
}
显现:
<receiver android:name="com.library.ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
但是当我像这样使用它时,我在 CallStateListener 中收到了重复的呼叫。我怎样才能解决这个问题?