naz89
naz89

Reputation: 71

Detecting End of Call with Broadcast Receiver inside a service

I'm writing an Android app, I need to run a broadcast receiver inside a service, the receiver detects when there is an incoming call and when the call is ended or answered, the service puts the phone to silent mode and the receiver detects when there is an incoming call, if there is an incoming call, the receiver sets the ringer mode to ring and vibrate normally, when the call is answered or over, the receiver sets the ringer to silent mode again.

here is my receiver:

private final BroadcastReceiver CallReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
        amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
        {
            amanager.setRingerMode(2);  //Ringer ON
        }
        if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {
            amanager.setRingerMode(0);  //Ringer Silent
        }
        if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
        {
            amanager.setRingerMode(0);  ////Ringer Silent
        }
    }
       };

Inside the onStartCommand method in my service:

      IntentFilter filter = new IntentFilter();
      filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
      registerReceiver(CallReceiver, filter);

The service is running successfully and the receiver detects when there is an incoming call but the app crashes afterwards, any ideas why this is happening? your help is appreciated!

EDIT: I solved the issue, turns out I wrote the wrong action in the manifest, it doesn't crash after changing it.

BUT now the receiver only detects an incoming call but doesn't detect when the call is answered or over, it did detect it a couple of times but most of the times it doesn't, the ringer is still on after the call is over! any ideas?

Upvotes: 1

Views: 2645

Answers (1)

naz89
naz89

Reputation: 71

Finally figured a way out! I added a delay of one second before setting the ringer mode to silent, I have no idea how this solution worked, but it did!

private final BroadcastReceiver CallReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
        amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

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

        if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            amanager.setRingerMode(0);  //Ringer Silent
        }
        if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            amanager.setRingerMode(0);  ////Ringer Silent
        }
        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
        {
            amanager.setRingerMode(2);  //Ringer ON
        }
    }
       };

Upvotes: 3

Related Questions