简体   繁体   中英

Delete answered incoming call from call log in android

I'm using Broadcast receiver to receive phone_states and checking any call(out/in coming) is being changed it's state to EXTRA_STATE_IDLE and then deleting the call info from call log. As I know android phone states are:

  1. EXTRA_STATE_RINGING
  2. EXTRA_STATE_OFFHOOK
  3. EXTRA_STATE_IDLE

Here for incoming calls I know:

  1. When incoming call is been received -> EXTRA_STATE_RINGING => EXTRA_STATE_OFFHOOK (After Answering call) => EXTRA_STATE_IDLE (After End call)
  2. When incoming call is been cut off -> EXTRA_STATE_RINGING => EXTRA_STATE_IDLE (After End call)

so, actually I'm clearing call log history when the phone state is in EXTRA_STATE_IDLE . But in this strategy i'm able to clear log history for 2. scenario but unable for 1. scenario.

Here is my code::

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);
                }

          }

What am I missing... can anyone explain is there any thing to do for handling answered incoming calls?? Any suggestion please...

You can maintain a state (int value) as a call state once call is ringing(RINGING) make it as a 1, if call is answered(OFFHOOK) make it as 2, if call is ended(IDLE) make it as 3. Now, whenever any one of the cases fires check the last call state, In this way you can easily track your 2 scenario. Remember once any call get ended and you are done, reset the call state as 0.

This is a type of hack,i haven't tried it ever before but you can give it a try.

Lets take variables:

boolean isIdle=true;
boolean isOffhook=false;

now,when you get into the case where phone is in "offhook state":

there:

if(isIdle==true)
    isIdle=false;

isOffhook=true;

Now,for the phone state is "Idle":

add these code at the begining:

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;

Solved it. Though it's late, it can help anyone. The issue is the BroadcastReceiver 's onReceive(Context context, Intent intent) is called every time the phone state is changed, but the intent gives the incoming number for EXTRA_STATE_RINGING , EXTRA_STATE_IDLE but not for EXTRA_STATE_OFFHOOK . so, when a call is received the incoming number becomes null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM