0

I'm developing a spam call filtering app and I'm trying to silence ringer for incoming (spam) call. The problem is none of AudioManager.setStreamMute nor AudioManager.setRingerMode is working in Lollipop. Here is my code snippet:

public class CallReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);

    if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      audioManager.setStreamMute(AudioManager.STREAM_RING, false);
      Log.i(TAG, "unmute");

    } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      audioManager.setStreamMute(AudioManager.STREAM_RING, true);
      Log.i(TAG, "mute");

    }
  }

When there's an incoming call, the mute part always gets executed but it sometimes succeeds and sometimes fails to mute the ringer. I can't find any rule. And audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) doesn't work either. This seems work fine when tested on emulators < 5, so I guess it's somehow related to Lollipop not having silent mode but interruptions filter. Commercial spam call filters are woking fine, so can somebody let me know how I could silence incoming calls with Lollipop?

K J
  • 4,505
  • 6
  • 27
  • 45
  • Not sure if it's related but for me the I get multiple onReceive calls only in Lollipop which leads to my app behaving weird. Since i'm just expecting one call. – gerfmarquez Feb 24 '15 at 16:50

1 Answers1

1

I've got the same issue for android 5. AudioManager.setStreamMute with the value false to unmute is never working.

I tried AudioManager.setStreamSolo and it worked for me.

//to mute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, true);

//unmute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, false);

It mutes all other streams except one you want to play. In may case I needed to play my own audio instead of ringtone.

But you can try to play a silence audio to hack if you need absolute silence.

Infinity
  • 91
  • 1
  • 3
  • 1
    Global.app() refers getApplicationContext(). Global is a singleton class for accessing application context everywhere rather than just activity or service context, which must be declared in the manifest as . – Infinity Dec 08 '15 at 12:06