是否有针对音量变化的广播动作? [英] Is there a broadcast action for volume changes?

查看:62
本文介绍了是否有针对音量变化的广播动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小窗口小部件,每当用户更改铃声音量或振动设置时,都需要对其进行更新.

I'm programming a small widget that needs to be updated whenever the user changes the ringer volume or the vibrate settings.

捕获android.media.VIBRATE_SETTING_CHANGED可以很好地用于振动设置,但是当铃声音量改变时,我还没有找到任何通知的方式,尽管我可以尝试在用户按下音量增大/减小物理按键时进行捕捉,还有许多其他选项可以在不使用这些键的情况下更改音量.

Capturing android.media.VIBRATE_SETTING_CHANGED works just fine for the vibrate settings, but I haven't found any way of getting notified when the ringer volume changes and although I could try to capture when the user presses the volume up/volume down physical keys, there are many other options for changing the volume without using these keys.

您知道是否为此定义了任何广播操作,或者是否有任何广播操作来创建或解决该问题?

Do you know if there's any broadcast action defined for this or any way to create one or to solve the problem without it?

推荐答案

没有广播操作,但我确实发现您可以连接内容观察器以在设置更改时得到通知,其中流的数量是其中一些设置.注册android.provider.Settings.System.CONTENT_URI,以接收所有设置更改的通知:

There is no broadcast action, but I did find you can hook up a content observer to get notified when the settings change, volume of streams being some of those settings. Register for the android.provider.Settings.System.CONTENT_URI to be notified of all settings changes:

mSettingsContentObserver = new SettingsContentObserver( new Handler() ); 
this.getApplicationContext().getContentResolver().registerContentObserver( 
    android.provider.Settings.System.CONTENT_URI, true, 
    mSettingsContentObserver );

内容观察者可能看起来像这样:

The content observer might look something like this:

public class SettingsContentObserver extends ContentObserver {

   public SettingsContentObserver(Handler handler) {
      super(handler);
   } 

   @Override
   public boolean deliverSelfNotifications() {
      return super.deliverSelfNotifications(); 
   }

   @Override
   public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      Log.v(LOG_TAG, "Settings change detected");
      updateStuff();
   }
}

并确保在某个时候注销内容观察者.

And be sure to unregister the content observer at some point.

这篇关于是否有针对音量变化的广播动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆