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

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

问题描述

我编程的小部件需要每当用户更改铃声音量或振动设置进行更新。

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.

我可以尝试捕捉,当用户presses音量加/音量减键物理性,但不幸的是也有很多其他的选择来改变音量,而无需使用钥匙。

I could try to capture when the user presses the volume up/volume down phisical keys, but unfortunately there are many other options for changing the volume without using the 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.

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

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