• <bdo id='Yo1zX'></bdo><ul id='Yo1zX'></ul>
    <i id='Yo1zX'><tr id='Yo1zX'><dt id='Yo1zX'><q id='Yo1zX'><span id='Yo1zX'><b id='Yo1zX'><form id='Yo1zX'><ins id='Yo1zX'></ins><ul id='Yo1zX'></ul><sub id='Yo1zX'></sub></form><legend id='Yo1zX'></legend><bdo id='Yo1zX'><pre id='Yo1zX'><center id='Yo1zX'></center></pre></bdo></b><th id='Yo1zX'></th></span></q></dt></tr></i><div id='Yo1zX'><tfoot id='Yo1zX'></tfoot><dl id='Yo1zX'><fieldset id='Yo1zX'></fieldset></dl></div>

      <legend id='Yo1zX'><style id='Yo1zX'><dir id='Yo1zX'><q id='Yo1zX'></q></dir></style></legend>

      <tfoot id='Yo1zX'></tfoot>

    1. <small id='Yo1zX'></small><noframes id='Yo1zX'>

        在 Android 上监听音量变化事件

        时间:2023-08-31

          <tbody id='l4trA'></tbody>

            <tfoot id='l4trA'></tfoot>
          1. <i id='l4trA'><tr id='l4trA'><dt id='l4trA'><q id='l4trA'><span id='l4trA'><b id='l4trA'><form id='l4trA'><ins id='l4trA'></ins><ul id='l4trA'></ul><sub id='l4trA'></sub></form><legend id='l4trA'></legend><bdo id='l4trA'><pre id='l4trA'><center id='l4trA'></center></pre></bdo></b><th id='l4trA'></th></span></q></dt></tr></i><div id='l4trA'><tfoot id='l4trA'></tfoot><dl id='l4trA'><fieldset id='l4trA'></fieldset></dl></div>

              <legend id='l4trA'><style id='l4trA'><dir id='l4trA'><q id='l4trA'></q></dir></style></legend>

              <small id='l4trA'></small><noframes id='l4trA'>

                • <bdo id='l4trA'></bdo><ul id='l4trA'></ul>
                  本文介绍了在 Android 上监听音量变化事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有什么方法可以在 Android 上监听音量变化的事件,而不只是接管音量按钮?

                  Is there any way to listen to the event of volume change on Android, without just taking over the volume buttons?

                  我发现唯一有效的是 这里,但只有在音量控制消失后才能使用.

                  The only thing I've found that works is here, but it works only after the volume control has disappeared.

                  并非所有设备都有音量按钮,我需要在音量变化发生时立即捕捉它们,而不是在音量对话框消失后捕捉.

                  Not all devices have volume buttons, and I need to capture the volume changes as soon as they occur, and not after the volume dialog is gone.

                  推荐答案

                  更好,可以注册一个ContentObserver,如下:

                  Better, you can register a ContentObserver as follows:

                    getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, new ContentObserver(){...} );
                  

                  您的 ContentObserver 可能如下所示:

                  Your ContentObserver might look like this:

                  public class SettingsContentObserver extends ContentObserver {
                      private AudioManager audioManager;
                  
                      public SettingsContentObserver(Context context, Handler handler) {
                          super(handler);
                          audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                      }
                  
                      @Override
                      public boolean deliverSelfNotifications() {
                          return false;
                      }
                  
                      @Override
                      public void onChange(boolean selfChange) {
                          int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                  
                          Log.d(TAG, "Volume now " + currentVolume);
                      }
                  }
                  

                  完成后:

                  getApplicationContext().getContentResolver().unregisterContentObserver(mContentObserver);
                  

                  但请注意 - 如果快速按下大量按钮,有时通知似乎会延迟.

                  One caution, though - sometimes the notifications seem to be delayed if there are lots of button presses quickly.

                  这篇关于在 Android 上监听音量变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:更改 UIButton 文本 下一篇:Android:ViewGroup,如何拦截MotionEvent然后派发到目标

                  相关文章

                  最新文章

                  <tfoot id='UNKPA'></tfoot>
                • <i id='UNKPA'><tr id='UNKPA'><dt id='UNKPA'><q id='UNKPA'><span id='UNKPA'><b id='UNKPA'><form id='UNKPA'><ins id='UNKPA'></ins><ul id='UNKPA'></ul><sub id='UNKPA'></sub></form><legend id='UNKPA'></legend><bdo id='UNKPA'><pre id='UNKPA'><center id='UNKPA'></center></pre></bdo></b><th id='UNKPA'></th></span></q></dt></tr></i><div id='UNKPA'><tfoot id='UNKPA'></tfoot><dl id='UNKPA'><fieldset id='UNKPA'></fieldset></dl></div>

                    • <bdo id='UNKPA'></bdo><ul id='UNKPA'></ul>

                      <legend id='UNKPA'><style id='UNKPA'><dir id='UNKPA'><q id='UNKPA'></q></dir></style></legend>

                      <small id='UNKPA'></small><noframes id='UNKPA'>