Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onReceive getting called multiple times

I read this post: Broadcast receiver onReceive() getting called multiple times

But I didn't find the needed answer.

I've created a small utility function that overrides the onReceive() of BroadcastReceiver and rejects an incoming call based on certain conditions (which works fine).

Now once all the conditions are matched and I reject the call, I would like to store that number in the database (which again is an easy task). I would like to save the number in the database once I reject the call. But in the logs I observe that once there is an incoming call, the onReceive function gets called multiple times. If this is the case, I do not want multiple entries in my DB.

Is there any way by which the onReceive() would be called only once ? Or any workaround ?

Thanks for any help

like image 565
Darshan Bidkar Avatar asked Jan 30 '26 19:01

Darshan Bidkar


1 Answers

Your receiver will get called for three different state.on ringing, on hook and on idle state.

Check the phone state in onReceive.You may want to cut the call and store in db if its state is ringing.

public void onReceive(final Context context, Intent intent) {
   Bundle bundle = intent.getExtras();

    if (null == bundle) {
        return;
    }

    String state = bundle.getString(TelephonyManager.EXTRA_STATE);

    if (TelephonyManager.EXTRA_STATE_RINGING.equalsIgnoreCase(state)) {
    //cut the call and store in db
    return;
    }


    } 
    if(TelephonyManager.EXTRA_STATE_IDLE.equalsIgnoreCase(state)) {
    return;
    }
     if(TelephonyManager.EXTRA_STATE_OFFHOOK.equalsIgnoreCase(state)){
     return;
     }

   }
like image 167
Rasel Avatar answered Feb 01 '26 13:02

Rasel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!