zoukankan      html  css  js  c++  java
  • Android电池电量更新 BatteryService(转)

    电池的信息,电压,温度,充电状态等等,都是由BatteryService来提供的。BatteryService是跑在system_process当中,在系统初始化的时候启动,如下

         在SystemServer.java中可以看到启动BatteryService的代码:

              Log.i(TAG, "Starting Battery Service.");
             BatteryService battery = new BatteryService(context);
             ServiceManager.addService("battery", battery);

    ========================================================================================
    1. 数据来源
            BatteryService通过JNI(com_android_server_BatteryService.cpp)读取数据。BatteryService通过JNI注册的不仅有函数,还有变量。 如下:

        //##############在BatteryService.java中声明的变量################

        private boolean mAcOnline; 
        private boolean mUsbOnline; 
        private int mBatteryStatus; 
        private int mBatteryHealth; 
        private boolean mBatteryPresent; 
        private int mBatteryLevel; 
        private int mBatteryVoltage; 
        private int mBatteryTemperature; 
        private String mBatteryTechnology;

        //在BatteryService.java中声明的变量,在com_android_server_BatteryService.cpp中共用,即在com_android_server_BatteryService.cpp中其实操作的也是BatteryService.java中声明的变量

       gFieldIds.mAcOnline = env->GetFieldID(clazz, "mAcOnline", "Z"); 
       gFieldIds.mUsbOnline = env->GetFieldID(clazz, "mUsbOnline", "Z"); 
       gFieldIds.mBatteryStatus = env->GetFieldID(clazz, "mBatteryStatus", "I"); 
       gFieldIds.mBatteryHealth = env->GetFieldID(clazz, "mBatteryHealth", "I"); 
       gFieldIds.mBatteryPresent = env->GetFieldID(clazz, "mBatteryPresent", "Z"); 
       gFieldIds.mBatteryLevel = env->GetFieldID(clazz, "mBatteryLevel", "I"); 
       gFieldIds.mBatteryTechnology = env->GetFieldID(clazz, "mBatteryTechnology", "Ljava/lang/String;"); 
       gFieldIds.mBatteryVoltage = env->GetFieldID(clazz, "mBatteryVoltage", "I"); 
       gFieldIds.mBatteryTemperature = env->GetFieldID(clazz, "mBatteryTemperature", "I");

        //上面这些变量的值,对应是从下面的文件中读取的,一只文件存储一个数值。

       

        #define AC_ONLINE_PATH "/sys/class/power_supply/ac/online" 
        #define USB_ONLINE_PATH "/sys/class/power_supply/usb/online" 
        #define BATTERY_STATUS_PATH "/sys/class/power_supply/battery/status" 
        #define BATTERY_HEALTH_PATH "/sys/class/power_supply/battery/health" 
        #define BATTERY_PRESENT_PATH "/sys/class/power_supply/battery/present" 
        #define BATTERY_CAPACITY_PATH "/sys/class/power_supply/battery/capacity" 
        #define BATTERY_VOLTAGE_PATH "/sys/class/power_supply/battery/batt_vol" 
        #define BATTERY_TEMPERATURE_PATH "/sys/class/power_supply/battery/batt_temp" 
        #define BATTERY_TECHNOLOGY_PATH "/sys/class/power_supply/battery/technology"

        Android是运行在Linux内核上面的,/sys/class/power_supply亦是Linux内核下面的目录。至于这些文件时怎么生成的,则是由Platform来控制的。

    -----------------------------------------------------------------------------------------------------------------------------------
    2. 数据传送
            电池的这些信息是通过何种方式,被其他应用所获得的。可以想到的有两种方式,第一种,应用主动从BatteryService获得数据;第二种,BatteryService主动把数据传送给所关心的应用程序。

           BatteryService采用的是第二种方式,所有的电池的信息数据是通过Intent传送出去的。在BatteryService.java中,Code如下:

    Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 
    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 
    
    intent.putExtra("status", mBatteryStatus); 
    intent.putExtra("health", mBatteryHealth); 
    intent.putExtra("present", mBatteryPresent); 
    intent.putExtra("level", mBatteryLevel); 
    intent.putExtra("scale", BATTERY_SCALE); 
    intent.putExtra("icon-small", icon); 
    intent.putExtra("plugged", mPlugType); 
    intent.putExtra("voltage", mBatteryVoltage); 
    intent.putExtra("temperature", mBatteryTemperature); 
    intent.putExtra("technology", mBatteryTechnology); 
    
    ActivityManagerNative.broadcastStickyIntent(intent, null);

    -----------------------------------------------------------------------------------------------------------------------------------
    3. 数据接收
        应用如果想要接收到BatteryService发送出来的电池信息,则需要注册一个Intent为Intent.ACTION_BATTERY_CHANGED的BroadcastReceiver。

        注册方法如下:

          IntentFilter mIntentFilter = new IntentFilter(); 
          mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 
          registerReceiver(mIntentReceiver, mIntentFilter);
    
          private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 
                 @Override 
                 public void onReceive(Context context, Intent intent) { 
                       // TODO Auto-generated method stub 
                         String action = intent.getAction(); 
                         if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 
                               int nVoltage = intent.getIntExtra("voltage", 0); 
                               if(nVoltage!=0){        
                                       mVoltage.setText("V: " + nVoltage + "mV - Success..."); 
                              } 
                                else{ 
                                       mVoltage.setText("V: " + nVoltage + "mV - fail..."); 
                                } 
                       }    
                  }    
          };

    -----------------------------------------------------------------------------------------------------------------------------------
    4. 数据更新
             电池的信息会随着时间不停变化,自然地,就需要考虑如何实时的更新电池的数据信息。在BatteryService启动的时候,会同时通过UEventObserver启动一个onUEvent Thread。

            每一个Process最多只能有一个onUEvent Thread,即使这个Process中有多个UEventObserver的实例。当在一个Process中,第一次Call startObserving()方法后,这个UEvent thread就启动了。
    而一旦这个UEvent thread启动之后,就不会停止。
         //在BatteryService.java中 

         mUEventObserver.startObserving("SUBSYSTEM=power_supply");
    
         private UEventObserver mUEventObserver = new UEventObserver() { 
            @Override 
            public void onUEvent(UEventObserver.UEvent event) { 
                update(); 
            } 
         };

         在UEvent thread中会不停调用 update()方法,来更新电池的信息数据。

    原文地址:http://hi.baidu.com/maoxiaofei_2009/blog/item/39f147894b162518c8fc7a97.html

  • 相关阅读:
    Java实现 LeetCode 440 字典序的第K小数字
    Java实现 LeetCode 438 找到字符串中所有字母异位词
    route命令详解与使用实例
    Google protobuf的安装及使用
    linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析
    _GUN_SOURCE宏
    CodeViz产生函数调用图
    linux下阅读源代码的工具
    linux gcc 编译时头文件和库文件搜索路径
    Makefile第四讲:include 引用其它makefile文件
  • 原文地址:https://www.cnblogs.com/flyme/p/2118541.html
Copyright © 2011-2022 走看看