麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Audio系列之音量鍵

2019-11-09 18:59:34
字體:
來源:轉載
供稿:網友

根據按鍵派發策略,派發順序是Activity,然后是PhoneWindow。

如果activity需要處理音量鍵,可以在應用中重寫Activity 的onKeyDown()方法以截獲音量鍵將其用作其他功能,如相機的音量快捷鍵。Activity#onKeyDown()方法源碼

/**     * Called when a key was PRessed down and not handled by any of the views     * inside of the activity. So, for example, key presses while the cursor     * is inside a TextView will not trigger the event (unless it is a navigation     * to another object) because TextView handles its own key presses.     *     * <p>If the focused view didn't want this event, this method is called.     *     * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}     * by calling {@link #onBackPressed()}, though the behavior varies based     * on the application compatibility mode: for     * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,     * it will set up the dispatch to call {@link #onKeyUp} where the action     * will be performed; for earlier applications, it will perform the     * action immediately in on-down, as those versions of the platform     * behaved.     *     * <p>Other additional default key handling may be performed     * if configured with {@link #setDefaultKeyMode}.     *     * @return Return <code>true</code> to prevent this event from being propagated     * further, or <code>false</code> to indicate that you have not handled     * this event and it should continue to be propagated.     * @see #onKeyUp     * @see android.view.KeyEvent     */    public boolean onKeyDown(int keyCode, KeyEvent event)  {        if (keyCode == KeyEvent.KEYCODE_BACK) {            if (getApplicationInfo().targetSdkVersion                    >= Build.VERSION_CODES.ECLAIR) {                event.startTracking();            } else {                onBackPressed();            }            return true;        }        if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {            return false;        } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {            Window w = getWindow();            if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&                    w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,                            Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {                return true;            }            return false;        } else {            // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*            boolean clearSpannable = false;            boolean handled;            if ((event.getRepeatCount() != 0) || event.isSystem()) {                clearSpannable = true;                handled = false;            } else {                handled = TextKeyListener.getInstance().onKeyDown(                        null, mDefaultKeySsb, keyCode, event);                if (handled && mDefaultKeySsb.length() > 0) {                    // something useable has been typed - dispatch it now.                    final String str = mDefaultKeySsb.toString();                    clearSpannable = true;                    switch (mDefaultKeyMode) {                    case DEFAULT_KEYS_DIALER:                        Intent intent = new Intent(Intent.ACTION_DIAL,  Uri.parse("tel:" + str));                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        startActivity(intent);                        break;                    case DEFAULT_KEYS_SEARCH_LOCAL:                        startSearch(str, false, null, false);                        break;                    case DEFAULT_KEYS_SEARCH_GLOBAL:                        startSearch(str, false, null, true);                        break;                    }                }            }            if (clearSpannable) {                mDefaultKeySsb.clear();                mDefaultKeySsb.clearSpans();                Selection.setSelection(mDefaultKeySsb,0);            }            return handled;        }    }

Activity.onKeyDown()對一般按鍵有默認的一些處理,但是我們可以復寫這個方法以處理我們想要處理的按鍵。如果不做處理,會讓PhoneWindow的onKeyDown處理,下面是PhoneWindow#onKeyDown()方法源碼:

protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {        /* ****************************************************************************         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES.         *         * If your key handling must happen before the app gets a crack at the event,         * it goes in PhoneWindowManager.         *         * If your key handling should happen in all windows, and does not depend on         * the state of the current application, other than that the current         * application can override the behavior by handling the event itself, it         * should go in PhoneFallbackEventHandler.         *         * Only if your handling depends on the window, and the fact that it has         * a DecorView, should it go here.         * ****************************************************************************/        final KeyEvent.DispatcherState dispatcher =                mDecor != null ? mDecor.getKeyDispatcherState() : null;        //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount()        //        + " flags=0x" + Integer.toHexString(event.getFlags()));        switch (keyCode) {            case KeyEvent.KEYCODE_VOLUME_UP:            case KeyEvent.KEYCODE_VOLUME_DOWN:            case KeyEvent.KEYCODE_VOLUME_MUTE: {                int direction = 0;                switch (keyCode) {                    case KeyEvent.KEYCODE_VOLUME_UP:                        direction = AudioManager.ADJUST_RAISE;                        break;                    case KeyEvent.KEYCODE_VOLUME_DOWN:                        direction = AudioManager.ADJUST_LOWER;                        break;                    case KeyEvent.KEYCODE_VOLUME_MUTE:                        direction = AudioManager.ADJUST_TOGGLE_MUTE;                        break;                }                // If we have a session send it the volume command, otherwise                // use the suggested stream.                if (mMediaController != null) {                    mMediaController.adjustVolume(direction, AudioManager.FLAG_SHOW_UI);                } else {                    MediaSessionLegacyHelper.getHelper(getContext()).sendAdjustVolumeBy(                            mVolumeControlStreamType, direction,                            AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE                                    | AudioManager.FLAG_FROM_KEY);                }                return true;            }            // These are all the recognized media key codes in            // KeyEvent.isMediaKey()            case KeyEvent.KEYCODE_MEDIA_PLAY:            case KeyEvent.KEYCODE_MEDIA_PAUSE:            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:            case KeyEvent.KEYCODE_MUTE:            case KeyEvent.KEYCODE_HEADSETHOOK:            case KeyEvent.KEYCODE_MEDIA_STOP:            case KeyEvent.KEYCODE_MEDIA_NEXT:            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:            case KeyEvent.KEYCODE_MEDIA_REWIND:            case KeyEvent.KEYCODE_MEDIA_RECORD:            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {                if (mMediaController != null) {                    if (mMediaController.dispatchMediaButtonEvent(event)) {                        return true;                    }                }                return false;            }            case KeyEvent.KEYCODE_MENU: {                onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event);                return true;            }            case KeyEvent.KEYCODE_BACK: {                if (event.getRepeatCount() > 0) break;                if (featureId < 0) break;                // Currently don't do anything with long press.                if (dispatcher != null) {                    dispatcher.startTracking(event, this);                }                return true;            }        }        return false;    }在PhoneWindow#onKeyDown()中最終調用mMediaController.adjustVolume(direction, AudioManager.FLAG_SHOW_UI);調節音量,源碼如下:

/**     * Adjust the volume of the output this session is playing on. The direction     * must be one of {@link AudioManager#ADJUST_LOWER},     * {@link AudioManager#ADJUST_RAISE}, or {@link AudioManager#ADJUST_SAME}.     * The command will be ignored if the session does not support     * {@link VolumeProvider#VOLUME_CONTROL_RELATIVE} or     * {@link VolumeProvider#VOLUME_CONTROL_ABSOLUTE}. The flags in     * {@link AudioManager} may be used to affect the handling.     *     * @see #getPlaybackInfo()     * @param direction The direction to adjust the volume in.     * @param flags Any flags to pass with the command.     */    public void adjustVolume(int direction, int flags) {        try {            mSessionBinder.adjustVolume(direction, flags, mContext.getPackageName());        } catch (RemoteException e) {            Log.wtf(TAG, "Error calling adjustVolumeBy.", e);        }    }在PhoneWindow#onKeyDown()的flag有幾個,其中AudioManager.FLAG_SHOW_UI告訴AudioService需要彈出一個音量控制板。而PhoneWindow#onKeyUp()中對應的有AudioManager.FLAG_PLAY_SOUND,這是告訴AudioService松開音量鍵后有提示音,還有更多的flag自己去看。

每個流類型(StreamType)都有獨立的音量值。而PhoneWindow最終設置的音量所屬類型由PhoneWindow#mVolumeControlStream決定,在Activity#setVolumeControlStream()可以設置綁定的PhoneWindow的mVolumeControlStream。

音量按鍵有三個值,分別是KeyEvent.KEYCODE_VOLUME_UP(加), KeyEvent.KEYCODE_VOLUME_DOWN(減), KeyEvent.KEYCODE_VOLUME_MUTE(靜音)

音量改變后,會發廣播,下面是AudioService#setIndex()源碼:

mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION);mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index);                mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex);                mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS,                        mStreamVolumeAlias[mStreamType]);                sendBroadcastToAll(mVolumeChanged);


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福利在线小视频 | 91精品国产777在线观看 | 中文字幕欧美日韩 | 性高湖久久久久久久久aaaaa | 91看片在线播放 | 久久精品成人影院 | 久久久久久久免费视频 | 免费视频www在线观看 | 草久在线观看视频 | 激情亚洲一区二区三区 | 中文黄色一级片 | 撅高 自己扒开 调教 | 一本免费视频 | 亚洲视频成人 | 成人毛片100部免费观看 | a视频在线播放 | 免费网址黄 | 色网站在线免费观看 | 欧产日产国产精品v | 黄色毛片免费视频 | 黄色免费不卡视频 | 小情侣嗯啊哦视频www | 亚洲午夜天堂吃瓜在线 | 2019亚洲日韩新视频 | 91精品国产乱码久久久久 | 精品亚洲夜色av98在线观看 | chinese中国真实乱对白 | 中文字幕一区久久 | 鲁丝片一区二区三区免费入口 | 毛片视频免费播放 | 91重口视频| 久久精品视频黄色 | 福利免费在线观看 | 国产精品久久久久久久久久免 | 91女上位 在线播放 性欧美日本 | 成人午夜一区 | 日韩精品久久久久久久电影99爱 | 成人在线视频免费看 | 一级黄色免费观看 | 鲁丝一区二区三区不属 | av性色全交蜜桃成熟时 |