2

我正在使用广播接收器接收 phone_states 并检查任何呼叫(呼出/呼入)正在更改其状态EXTRA_STATE_IDLE,然后从通话记录中删除通话信息。据我所知,android手机状态是:

  1. EXTRA_STATE_RINGING
  2. EXTRA_STATE_OFFHOOK
  3. EXTRA_STATE_IDLE

我知道来电:

  1. 接到来电时 -> EXTRA_STATE_RINGING=> EXTRA_STATE_OFFHOOK(接听电话后)=> EXTRA_STATE_IDLE(结束通话后)
  2. 当来电被切断时 -> EXTRA_STATE_RINGING=> EXTRA_STATE_IDLE(结束通话后)

所以,实际上我正在清除电话状态时的通话记录历史记录EXTRA_STATE_IDLE。但在这个策略中,我能够清除 2. 场景的日志历史记录,但无法清除 1. 场景。

这是我的代码::

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){              
              Toast.makeText(context, "ringing", 20).show();

              SharedPreferences statePreference=context.getApplicationContext().getSharedPreferences("RingCallState", 0);
              SharedPreferences.Editor editor=statePreference.edit();
              editor.putBoolean("State", true);
              editor.commit();

              context.startActivity(i);
          }

          else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

              Toast.makeText(context, "off hook", 20).show();

              SharedPreferences statePreference=context.getApplicationContext().getSharedPreferences("RingCallState", 0);

              Log.d("statePref OFFHOOK", "state :: "+statePreference.getBoolean("State", false));

              if(!statePreference.getBoolean("State", false)) {

                  SharedPreferences out_statePreference=context.getApplicationContext().getSharedPreferences("OutCallState", 0);
                  SharedPreferences.Editor out_editor=out_statePreference.edit();
                  out_editor.putBoolean("OutState", true);
                  out_editor.commit();

              }                 
          }

          else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
              Toast.makeText(context, "idle", 20).show();

              SharedPreferences statePreference=context.getApplicationContext().getSharedPreferences("RingCallState", 0);

              Log.d("statePref IDLE", "state :: "+statePreference.getBoolean("State", false));

              if(statePreference.getBoolean("State", false))
                {                       
                    SharedPreferences.Editor editor=statePreference.edit();
                    editor.putBoolean("State", false);
                    editor.commit();

                    Log.d("in", "in coming :: "+incomingNumber);

                    new Handler().postDelayed(new Runnable() {
                          public void run() {
                              clearLastCallLog(context, incomingNumber);
                          }
                      }, 4000);

                }


                SharedPreferences out_statePreference=context.getApplicationContext().getSharedPreferences("OutCallState", 0);
                if(out_statePreference.getBoolean("OutState", false))
                {
                    SharedPreferences.Editor out_editor=out_statePreference.edit();
                    out_editor.putBoolean("OutState", false);
                    out_editor.commit();

                    Log.d("out", "out going :: "+outgoingNumber);

                    new Handler().postDelayed(new Runnable() {
                          public void run() {
                              clearLastCallLog(context, outgoingNumber);
                          }
                      }, 4000);
                }

          }

我错过了什么......任何人都可以解释处理接听的来电有什么办法吗?有什么建议请...

4

3 回答 3

0

您可以将状态(int 值)保持为呼叫状态,一旦呼叫响铃(RINGING)将其设为 1,如果呼叫被应答(OFFHOOK)将其设为 2,如果呼叫结束(IDLE)将其设为 3。现在,每当任何一个案例触发时,都会检查最后一次通话状态,这样您就可以轻松跟踪您的 2 个场景。请记住,一旦任何通话结束并完成,请将通话状态重置为 0。

于 2012-09-12T07:07:04.503 回答
0

这是一种hack,我以前从未尝试过,但您可以尝试一下。

让我们取变量:

boolean isIdle=true;
boolean isOffhook=false;

现在,当您进入电话处于“摘机状态”的情况时:

那里:

if(isIdle==true)
    isIdle=false;

isOffhook=true;

现在,手机状态为“空闲”:

在开头添加这些代码:

if(isIdle==false && isOffhook==true){
    // case :there has been a call
    String lastCall=<get the type of call of last call,from call log of phone>; //incoming or outgoing

    if(lastCall=="incoming"){
       // last call was answered incoming call
    }
    isOffhook=false;
}
isIdle=true;
于 2012-09-12T07:09:58.663 回答
0

解决了。虽然晚了,但它可以帮助任何人。问题是每次电话状态改变时都会调用BroadcastReceiver's ,但意图会给出,的传入号码。因此,当接到电话时,来电号码变为空。onReceive(Context context, Intent intent)EXTRA_STATE_RINGINGEXTRA_STATE_IDLE but not for EXTRA_STATE_OFFHOOK

于 2016-10-14T12:44:59.213 回答