Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...