Android状态栏入门

1.先说一下手机和平板代码位置: 
com.android.systemui.statusbar.phone  这个是针对手机而需要的status bar
com.android.systemui.statusbar.tablet   这个是针对平板电脑而需要的staus bar(system bar)


2. 再看状态栏是如何启动起来的呢?
我们都知道系统刚启动,在SystemServer会加载系统的各种服务,状态栏也不例外,就是在这个时候创建的,代码如下:
SystemServer.java
startSystemUI(contextF);
static final void startSystemUi(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.SystemUIServic"));
context.startService(intent);
}
如果想直接启动SystemBar,可以用命令: am startservice -n com.android.systemui/.SystemUIService


3. 进入启动的主类看一看吧
SystemUIService.java
public void onCreate() {
        // Pick status bar or system bar.
        IWindowManager wm = IWindowManager.Stub.asInterface(
                ServiceManager.getService(Context.WINDOW_SERVICE));
                
        
        try {	
            SERVICES[0] = wm.canStatusBarHide()
                    ? R.string.config_statusBarComponent
                    : R.string.config_systemBarComponent;
        } catch (RemoteException e) {
            Slog.w(TAG, "Failing checking whether status bar can hide", e);
        }
        ...
        
        mServices[i].start();
  }    


  说明:  
 /*选择使用哪一个组件,看到R.string.config_statusBarComponent不一定都在res/value/string.xml, 因为这个就是在res/value/config.xml里
   
<!-- Component to be used as the status bar service.  Must implement the IStatusBar
	  	interface.  This name is in the ComponentName flattened format (package/class)  -->
	  	<string name="config_statusBarComponent" translatable="false">com.android.systemui.statusbar.phone.PhoneStatusBar</string>
	  	<!-- Component to be used as the system bar service.  Must implement the IStatusBar
	  	interface.  This name is in the ComponentName flattened format (package/class)  -->
		  <string name="config_systemBarComponent" translatable="false">com.android.systemui.statusbar.tablet.TabletStatusBar</string>
*/

4. 我们TV是宽屏,使用的是com.android.systemui.statusbar.tablet.TabletStatusBar,就跟进TabletStatusBar的start()方法吧
TabletStatusBar.java
public void start() {
        super.start(); // will add the main bar view


        // storage
        StorageManager mStorageManager;                
        mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        mStorageManager.registerListener(
                new com.android.systemui.usb.StorageNotification(mContext));
    }
    
    wm.canStatusBarHide() //手机宽高等信息来判断使用哪一种bar
    
5. 还有一个重要的类PowerUI.java,这个主要是根据电量等信息弹出一些提示框,比如电量低,或者充电器有问题等框。
final Object[] SERVICES = new Object[] {
            0, // system bar or status bar, filled in below.
            com.android.systemui.power.PowerUI.class,
        };
        //在此,final代表SERVICES这个指针的指向不可变,但是指针指向的空间保存值可变
        
6. TabletStatusBar.java获得view, 这样你就可以找到你的layout了
protected View makeStatusBarView() {
		mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));


    // This guy will listen for HDMI plugged broadcasts so we can resize the
    // status bar as appropriate.
    mHeightReceiver = new HeightReceiver(mContext);
    mHeightReceiver.registerReceiver();
    loadDimens();


    final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
                context, R.layout.status_bar, null);
    mStatusBarView = sb;


    sb.setHandler(mHandler);
    ...
	}

你可能感兴趣的:(Android状态栏入门)