深入理解状态栏

本文探讨了Android系统状态栏的各个方面,包括SystemUIService的启动、状态栏创建的细节、通知显示流程以及系统图标显示的过程。重点分析了SystemUI的架构,涉及到的关键组件如IStatusBar、StatusBarManagerService和CommandQueue的作用和交互。通过对源码的分析,揭示了状态栏管理和通知显示的安全措施。
摘要由CSDN通过智能技术生成

本文是《深入理解 Android 卷 III》 中 深入理解状态栏一章中的读书笔记。

SystemUI 在源码中的位置 frameworks/base/packages/SystemUI 中。


一、初识 SystemUI

主要讲状态栏和导航栏,它们都运行在 SystemUIService 中

1、SystemUIService 启动过程

在负责启动系统服务的 ServerThread 中,调用 

ActivityManagerService.self().systemReady(new Runnable() {
            public void run() {
               ...

                if (!headless) startSystemUi(contextF);

然后启动 SystemUIService

 static final void startSystemUi(Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.systemui",
                    "com.android.systemui.SystemUIService"));
        //Slog.d(TAG, "Starting service: " + intent);
        context.startServiceAsUser(intent, UserHandle.OWNER);
    }


  在 SystemUIServie 中会启动

 /**
     * The class names of the stuff to start.
     */
    final Object[] SERVICES = new Object[] {
            0, // system bar or status bar, filled in below.
            com.android.systemui.power.PowerUI.class,
            com.android.systemui.media.RingtonePlayer.class,
            com.android.systemui.settings.SettingsUI.class,
        };


在 SystemUI 中,分离方案实现者是 PhoneStatusBar, 而集成布局方案的实现者则是 TabletStatusBar. 二者的本质功能是一致的,即提供虚拟按键、显示通知信息等,区别仅在与布局不同、以及由此所衍生的定制行为而已。

屏幕换的在 720 dp 以内时,使用分离的布局方案,判断条件在 PhoneWindowManager.setInitialDisplaySize() 中。



二、状态栏的创建

状态栏和导航栏的启动都是用 PhoneStatusBar.start() 完成的。

1、创建过程

 PhoneStatuBar.addStatusBarWindow()  中
    private void addStatusBarWindow() {
        // Put up the view
        // 状态栏的高度,默认是 25dp, 位置在 frameworks/base/core/res/res/values/dimen.xml 中
        // R.dimen.status_bar_height
        final int height = getStatusBarHeight();

        // Now that the status bar window encompasses the sliding panel and its
        // translucent backdrop, the entire thing is made TRANSLUCENT and is
        // hardware-accelerated.
        final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                height,
                WindowManager.LayoutParams.TYPE_STATUS_BAR, // 窗口类型
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE   // 状态栏不接受按键事件
                    // 状态栏接受导致设备唤醒的事件
                    | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
                    // 允许状态栏支持触摸事件序列的拆分
                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
                PixelFormat.TRANSLUCENT);   // 状态栏的 Surface 像素格式为支持透明度
        // 启动硬件加速
        lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;

        lp.gravity = getStatusBarGravity();
        lp.setTitle("StatusBar");
        lp.packageName = mContext.getPackageName();

        // 创建 StatusBar 和 导航栏
        makeStatusBarView();
        // 添加到窗口,创建过程完成
        mWindowManager.addView(mStatusBarWindow, lp);
    }

2. 布局

在 makeStatusBarView() 方法中,布局文件是 R.layout.super_status_bar.xml 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值