Android 8.0 中Systemui中的常见修改(一)亮度条

Android8.0中下拉状态栏里面的进度条是下面的样子:

我们需要定制成下面的样子(在原生的基础上我们还增加了自动调节亮度的功能):

这里面把代码直接贴出来了,方便大家的使用和修改。如果有什么问题可以在留言哦!

frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java

///xiangzaixiansheng for UI@{
import com.android.systemui.settings.ToggleSlider;
import android.widget.CheckBox;
///}@

public QSPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        setOrientation(VERTICAL);

        mBrightnessView = LayoutInflater.from(context).inflate(
                R.layout.quick_settings_brightness_dialog, this, false);
        // addView(mBrightnessView);xiangzaixiansheng

        setupTileLayout();

        mPageIndicator = LayoutInflater.from(context).inflate(
                R.layout.qs_page_indicator, this, false);
        addView(mPageIndicator);
        //移动位置xiangzaixiansheng
        addView(mBrightnessView);
        //}@
        if (mTileLayout instanceof PagedTileLayout) {
            ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
        }
       
        addDivider();

        mFooter = new QSSecurityFooter(this, context);
        addView(mFooter.getView());

        updateResources();

       /* mBrightnessController = new BrightnessController(getContext(),
                findViewById(R.id.brightness_icon),
                findViewById(R.id.brightness_slider));**/
       //修改样式 xiangzaixiansheng
        mBrightnessController = new BrightnessController(getContext(),
                (ImageView) findViewById(R.id.brightness_icon),
                (ToggleSlider) findViewById(R.id.brightness_slider),
                (CheckBox) findViewById(R.id.brightness_auto));
    }

frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java

///xiangzaixiansheng for  UI@{  
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.CheckBox;
///}@

 ///xiangzaixiansheng for UI 修改值为true@{
 private static final boolean SHOW_AUTOMATIC_ICON = true;
 ///}@

 ///xiangzaixiansheng for UI@{  
 private CheckBox mAutoBrightness;
 ///}@


 private final Runnable mUpdateModeRunnable = new Runnable() {
        @Override
        public void run() {
            if (mAutomaticAvailable) {
                int automatic;
                automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_MODE,
                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
                        UserHandle.USER_CURRENT);
                mAutomatic = automatic != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
                mHandler.obtainMessage(MSG_UPDATE_ICON, mAutomatic ? 1 : 0).sendToTarget();
            } else {
                mHandler.obtainMessage(MSG_SET_CHECKED, 0).sendToTarget();
                mHandler.obtainMessage(MSG_UPDATE_ICON, 0 /* automatic */).sendToTarget();
            }
 ///xiangzaixiansheng for ui @{    
             if (mAutoBrightness != null) {
                mAutoBrightness.setChecked(Settings.System.getInt(
                    mContext.getContentResolver(),
                    SCREEN_BRIGHTNESS_MODE,
                    SCREEN_BRIGHTNESS_MODE_MANUAL) != SCREEN_BRIGHTNESS_MODE_MANUAL);
            }
            ///M:}@   
        }
    };


 ///xiangzaixiansheng for ui@{    
       public BrightnessController(Context context, ImageView icon,
            ToggleSlider control, CheckBox autoBrightness) {
        this(context, icon, control);
        mAutoBrightness = autoBrightness;
        mAutoBrightness.setChecked(Settings.System.getInt(
            mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
            SCREEN_BRIGHTNESS_MODE_MANUAL) != SCREEN_BRIGHTNESS_MODE_MANUAL);
        mAutoBrightness.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
                Settings.System.putInt(mContext.getContentResolver(),
                    SCREEN_BRIGHTNESS_MODE,
                    isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC :
                    SCREEN_BRIGHTNESS_MODE_MANUAL);
                    }
                });
    }
    ///}@

   public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
        mChangeCallbacks.add(cb);
    }

frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml

添加下面的文字和相应的checkbox即可。

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_gravity="center_vertical"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        android:text="@string/status_bar_settings_auto_brightness_label"
        />
    <CheckBox
        android:id="@+id/brightness_auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        />

我们还需要改变滑动亮度调节时的样式:

现在是:

做成在调节亮度时,不会启动BrightnessMirrorController

这里面我们frameworks/base/packages/SystemUI/res/layout/brightness_mirror.xml修改background的值。

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:elevation="2dp">
        <include layout="@layout/quick_settings_brightness_dialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>

然后在frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/BrightnessMirrorController.java

    private ViewPropertyAnimator outAnimation(ViewPropertyAnimator a) {
       ///xiangzaixiansheng @{
        return a.alpha(1.0f)
       ///}@
                .setDuration(TRANSITION_DURATION_OUT)
                .setInterpolator(Interpolators.ALPHA_OUT)
                .withEndAction(null);
    }

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android 8.0 及以上版本,为了增强应用程序的安全性,Android 引入了后台限制,禁止未在前台运行的应用程序启动服务。如果您想在后台启动服务,需要使用 `startForegroundService()` 方法。这个方法会启动一个前台服务,然后你可以在服务启动后在通知栏显示一个通知,以此来告知用户服务正在运行。 以下是一个使用 `startForegroundService()` 的示例代码: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建一个 NotificationChannel NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT); // 向系统注册 NotificationChannel NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } // 创建一个 Intent,启动你的服务 Intent serviceIntent = new Intent(this, YourService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 在 Android 8.0 及以上版本上,需要调用 startForegroundService() 方法启动服务。 startForegroundService(serviceIntent); } else { // 在 Android 8.0 以下版本上,可以直接调用 startService() 方法启动服务。 startService(serviceIntent); } ``` 注意:如果你使用的是 `startForeground()` 方法,会在 Android 8.0 及以上版本上抛出 `IllegalStateException` 异常,因为 Android 8.0 及以上版本禁止在后台启动服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值