广播接收器调用两次EXTRA_STATE_RINGING状态,导致不变数据

时间:2014-05-02 09:59:25

标签: android broadcastreceiver telephonymanager

我正试图在通话时保存通话详细信息,因此我实施了一个收听PHONE STATE的广播接收器。调用问题出现时,它进入EXTRA_STATE_RINGING两次,我实现逻辑,所以我的逻辑调用两次导致不变数据。

private static final String ACTION_IN = "android.intent.action.PHONE_STATE";

以下是BroadCastReceiver的onReceieve()

的代码
public void onReceive(Context context, Intent intent) {
    ctx = context;
    if (intent.getAction().equals(ACTION_IN)) {
        Log.v("onReceive", "ACTION IN");
        if ((bundle = intent.getExtras()) != null) {
            Log.v("onReceive", "Bundle != NULL");
            state = bundle.getString(TelephonyManager.EXTRA_STATE);
            Log.v("onReceive", "state: "+state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            //businesslogic
            }
            }

            }
            }

我在清单中有以下权限

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  

我的接收器在清单中被定义为

<receiver android:name="IncomingCallInterceptor" >                    
<intent-filter android:priority="999">                                                 
             <action android:name="android.intent.action.PHONE_STATE"/> 
        </intent-filter>       
    </receiver>

1 个答案:

答案 0 :(得分:1)

尝试这一点,它没有在EXTRA_STATE_RINGING上实现业务逻辑,只有在用户断开调用(CALL_STATE_IDLE)后才设置。

public void onReceive(Context context, Intent intent) {
    ctx = context;
    if (intent.getAction().equals(ACTION_IN)) {
        Log.v("onReceive", "ACTION IN");
        if ((bundle = intent.getExtras()) != null) {
            Log.v("onReceive", "Bundle != NULL");
            state = bundle.getString(TelephonyManager.EXTRA_STATE);
            Boolean singlecallstate=false;
    switch (state) {
        case TelephonyManager.EXTRA_STATE_RINGING:
           singlecallstate=true;
           //any other code you want
        case TelephonyManager.CALL_STATE_IDLE:
       if(singlecallstate){
           //business logic
          singlecallstate=false;
          }
    }
  }
}