Ringer mode change listener Broadcast receiver?

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

  • Unwanted padding around an ImageView
  • How to implement a two-finger double-click in Android?
  • While I make the source of Android 6.0, it failed
  • How to display AdMob ad at bottom of screen in Android app with Xamarin/Monogame?
  • Android Studio: Background task running indefinitely
  • CountDownTimer.cancel() is not working in Android
  • Related posts:

    Send HTML mail using Android intent
    Determine device public ip
    Error: Your cpu does not support required features(vt-x or svm)
    Instrumentation run failed due to 'java.io.FileNotFoundException'
    Apps from Recent Apps back out to stock launcher instead of custom launcher
    Why does using float instead of double not improve Android performance?
  • Shared element transition don't have the expected behaviour
  • Cordova Facebook Connect fail on init
  • React Native Android Webview Video
  • Change Checkbox value without triggering onCheckChanged
  • Programmatically Restart a React Native App
  • Android Google Maps V2 change My Location Icon
  • 2 Solutions collect form web for “Ringer mode change listener Broadcast receiver?”

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.