2

私は子孫を使用します - 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);
  }

これは私のBroadcastReceiverです:

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 で重複した呼び出しを受け取ります。どうすればこれを修正できますか?

4

2 に答える 2

0

バグなのか機能なのかは不明ですが、API19以降でブロードキャストで発生します。

解決策を探す人のために、以前の状態を追跡できます。異なる場合にのみ処理を行います。

于 2016-01-23T12:28:44.860 に答える