Android AudioManager Event Listener?
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SE开发者_运维百科RVICE);
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;
}
Using the above code, I can successfully find the various AudioManager RINGER_MODE* types, however I can't seem to find an EventListener that catches when the state has changed. The application I am writing runs in the background, so I'd like to know the the most timely fashion of checking the state of the AudioManager.RINGER_MODE.
You will need to register a BroadcastReceiver in your code that listens for the RINGER_MODE_CHANGED_ACTION intent. This will let you know that the state has changed. Once you receive the intent, you can call getRingerMode() to find out what the current state is.
did you try listening for AudioManager.RINGER_MODE_CHANGED_ACTION?
精彩评论