android下拉栏添加媒体音量调节

参考下拉栏的屏幕亮度调节,添加媒体音量调节。

平台信息:

MSM8909.LA.3.1.2_AOSP

PLATFORM_VERSION=9

BUILD_ID=PKQ1.190903.001

效果图

布局文件与音量图标

java 复制代码
Index: frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(版本 12429)
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:systemui="http://schemas.android.com/apk/res-auto"
+    android:layout_height="wrap_content"
+    android:layout_width="match_parent"
+    android:layout_gravity="center_vertical"
+    android:paddingLeft="16dp"
+    android:paddingRight="16dp">
+
+    <ImageView
+        android:id="@+id/media_volume_icon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginEnd="8dp"
+        android:src="@drawable/ic_media_stream"
+        android:contentDescription="@null"
+        android:visibility="visible" />
+
+    <SeekBar
+        android:id="@+id/media_volume_seekbar"
+        android:layout_width="0dp"
+        android:layout_height="48dp"
+        android:layout_gravity="center_vertical"
+        android:layout_weight="1" />
+
+</LinearLayout>
Index: frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(版本 12429)
@@ -0,0 +1,25 @@
+<!--
+    Copyright (C) 2017 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        android:tint="?android:attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,3l0.01,10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55C7.79,13 6,14.79 6,17c0,2.21 1.79,4 4.01,4S14,19.21 14,17V7h4V3H12zM10.01,19c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C12.01,18.1 11.11,19 10.01,19z"/>
+</vector>

实现

java 复制代码
Index: frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
===================================================================
--- frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12424)
+++ frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12429)
@@ -20,10 +20,14 @@
 import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
 
 import android.annotation.Nullable;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.media.AudioManager;
 import android.metrics.LogMaker;
 import android.os.Handler;
 import android.os.Message;
@@ -32,6 +36,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
+import android.widget.SeekBar;
+import android.widget.SeekBar.OnSeekBarChangeListener;
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -62,7 +68,7 @@
 
     protected final Context mContext;
     protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
-    protected final View mBrightnessView;
+    protected final View mBrightnessView, mMediaVolumeView;
     private final H mHandler = new H();
     private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
     private final QSTileRevealController mQsTileRevealController;
@@ -87,6 +93,9 @@
     private BrightnessMirrorController mBrightnessMirrorController;
     private View mDivider;
 
+    private AudioManager mAudioManager;
+    private SeekBar mMediaVolumeSeekBar;
+
     public QSPanel(Context context) {
         this(context, null);
     }
@@ -101,6 +110,12 @@
             R.layout.quick_settings_brightness_dialog, this, false);
         addView(mBrightnessView);
 
+        mMediaVolumeView = LayoutInflater.from(mContext).inflate(
+            R.layout.quick_settings_media_volume_dialog, this, false);
+        addView(mMediaVolumeView);
+
         mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
                 R.layout.qs_paged_tile_layout, this, false);
         mTileLayout.setListening(mListening);
@@ -123,9 +138,42 @@
                 findViewById(R.id.brightness_icon),
                 findViewById(R.id.brightness_slider));
 
+        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+        mMediaVolumeSeekBar = findViewById(R.id.media_volume_seekbar);
+        mMediaVolumeSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setOnSeekBarChangeListener(mSeekListener);
+        IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
+        mContext.registerReceiver(mMediaVolumeReceiver, filter);
+
         updateResources();
     }
 
+    private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
+        @Override
+        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
+        }
+
+        @Override
+        public void onStartTrackingTouch(SeekBar seekBar) { }
+
+        @Override
+        public void onStopTrackingTouch(SeekBar seekBar) { }
+    };
+
+    private final BroadcastReceiver mMediaVolumeReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int currentMediaVolumeValue = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+            mMediaVolumeSeekBar.setProgress(currentMediaVolumeValue);
+        }
+    };
+
     protected void addDivider() {
         mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
         mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
相关推荐
金融RPA机器人丨实在智能5 分钟前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿6 分钟前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc35 分钟前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20351 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106321 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
vistaup1 小时前
OKHTTP 默认构建包含 android 4.4 的TLS 1.2 以及设备时间不对兼容
android·okhttp
常利兵1 小时前
ButterKnife在Android 35 + Gradle 8.+环境下的适配困境与现代化迁移指南
android
撩得Android一次心动1 小时前
Android LiveData 全面解析:使用Java构建响应式UI【源码篇】
android·java·android jetpack·livedata
熊猫钓鱼>_>2 小时前
移动端开发技术选型报告:三足鼎立时代的开发者指南(2026年2月)
android·人工智能·ios·app·鸿蒙·cpu·移动端
Rainman博12 小时前
WMS-窗口relayout&FinishDrawing
android