14
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

switch (am.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        Log.i("MyApp","Silent mode");
    break;

    case AudioManager.RINGER_MODE_VIBRATE:
        Log.i("MyApp","Vibrate mode");
    break;

    case AudioManager.RINGER_MODE_NORMAL:
        Log.i("MyApp","Normal mode");
    break;
}

From the code above I can get the ringer mode. What I would liek to do is listen the ringer mode changes and call a function.

What I have been told is that I can register the AudioManager. RINGER_MODE_CHANGED_ACTION and listen the change intent in broadcastreceiver onReceive method. It sounds clear. But I am new to android and really dont know how to write it. Is there any one can just write a piece of code and show how exactly it works instead of saying use this or that :) Thank you

zeeali
  • 1,524
  • 20
  • 31
akd
  • 6,538
  • 16
  • 70
  • 112

3 Answers3

21

Use the following code inside the onCreate() method of your Activity or Service that you want to process the broadcast:

      BroadcastReceiver receiver=new BroadcastReceiver(){
          @Override
          public void onReceive(Context context, Intent intent) {
               //code...
          }
      };
      IntentFilter filter=new IntentFilter(
                      AudioManager.RINGER_MODE_CHANGED_ACTION);
      registerReceiver(receiver,filter);
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Thank you very much. that is the answer. But I get it worked in the Activity but I couldnt get it worked in a Service class. If you can give an example of working in service. that would be great as well. Thank you very much again. – akd Sep 20 '11 at 11:28
  • I am not sure actually. When I placed the code which you sent inside to onCreate method of activity and inside onReceive I can send data to the server by calling a method. However When I placed the code inside the Service class' onCreate method. and run I cant see anything on the server. I got registered the service in manifest file as well. Basically, What I would like to do is even though the application is killed it will be able to listen ringer mode and sends data to the server. I think I shuldnt place it in an activity if I want it to listen all the time. is that right? – akd Sep 20 '11 at 11:57
  • not really.. I expected it to run when the app is run. You are right it doesnt strat. I use this startService(new Intent(this, MyService.class)); in my mainactivity and it works perfect. is there a way to run the service with boot up start up of the phone? – akd Sep 20 '11 at 13:17
  • have a look at this http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ – Ovidiu Latcu Sep 20 '11 at 13:22
  • This broadcast works fine but when device reboots, it gets called automatically even if ringer mode is still same. – zeeali Apr 07 '16 at 11:05
  • It's because it's sticky. – Ov3r1oad Sep 07 '16 at 08:55
  • Just to put it out there, `registerReceiver()` is a method from an `activity`. – Morgan Koh Mar 20 '18 at 07:24
13

Another solution is to add a receiver with an action in Manifest:

<receiver android:name=".receivers.RingerModeStateChangeReceiver" >
    <intent-filter>
        <action android:name="android.media.RINGER_MODE_CHANGED" />
    </intent-filter>
</receiver>

and your class RingerModeStateChangeReceiver should extend BroadcastReceiver.

zeeali
  • 1,524
  • 20
  • 31
wlk
  • 5,695
  • 6
  • 54
  • 72
0

Here's an update version in Kotlin. Place this under your onCreate() lifecycle.

this.activity?.registerReceiver(object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == AudioManager.RINGER_MODE_CHANGED_ACTION) {
                // Set Player Volume
            }
        }
    }, IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION))
Morgan Koh
  • 2,297
  • 24
  • 24