Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 590
akd Avatar asked Sep 20 '11 10:09

akd


People also ask

What is broadcast listener service?

Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Does broadcast receiver work in background?

The solution to this problem is a Broadcast Receiver and it will listen in on changes you tell it to. A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.

What is Audio Manager in Android?

AudioManager is a class provided by Android which can be used to control the ringer volume of your Android device. With the help of this Audio Manager class, you can easily control the ringer volume of your device. Audio Manager Class can be used by calling the getSystemService() method in Android.

What is custom broadcast receiver in android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.


1 Answers

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);
like image 131
Ovidiu Latcu Avatar answered Nov 25 '22 05:11

Ovidiu Latcu