Android battery power update-batteryservice

Source: Internet
Author: User

Batteryservice provides batteryservice information, voltage, temperature, and charging status. Batteryservice runs in system_process and is started during system initialization.

In systemserver. Java, you can seeCode:

 
Log. I (TAG ,"Starting battery service."); Batteryservice battery =NewBatteryservice (context); servicemanager. addservice ("Battery", Battery );

========================================================== ========================================================== ==========

1. Data Source

Batteryservice reads data through JNI (com_android_server_batteryservice.cpp. Batteryservice not only registers functions but also variables through JNI. As follows:

// ############## Variables declared in 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; 

// The variables declared in batteryservice. Java are used in com_android_server_batteryservice.cpp. That is, the variables declared in com_android_server_batteryservice.cpp are also the variables declared in 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 ");

// The values of the preceding variables are read from the following files. A file stores a value.

     # 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_policy_path "/Sys/class/Power_Supply/battery/technology" 

Android runs on the Linux kernel, and/sys/class/Power_Supply is also the directory under the Linux kernel. Platform controls how these files are generated.

Certificate -----------------------------------------------------------------------------------------------------------------------------------

2. Data Transmission

The information about the battery is obtained by other applications. You can think of two ways: first, the application actively obtains data from the batteryservice; second, the batteryservice actively transmits data to the application of interest.Program.

Batteryservice uses the second method. All battery information data is transmitted through intent. In batteryservice. Java, the Code is as follows:

 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 ); 

Certificate -----------------------------------------------------------------------------------------------------------------------------------

3. receive data

If the application wants to receive batteryservice batteryinformation, it needs to register a broadcastreceiver whose intent is intent. action_battery_changed.

The registration method is as follows:

Intentfilter mintentfilter = New Intentfilter (); mintentfilter. addaction (intent. action_battery_changed); registerreceiver (mintentreceiver, mintentfilter ); Private Broadcastreceiver mintentreceiver = New Broadcastreceiver () {@ override Public   Void Onreceive (context, 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 ..." );}}}};

Certificate -----------------------------------------------------------------------------------------------------------------------------------

4. Data Update

Battery information changes constantly over time. Naturally, you need to consider how to update battery data in real time. When batteryservice is started, an onuevent thread is started through ueventobserver at the same time.

Each process can have only one onuevent thread at most, even if the process has multiple ueventobserver instances. In a process, after the first call startobserving () method, the uevent thread starts.

Once the uevent thread is started, it will not stop.

// In batteryservice. Java

Mueventobserver. startobserving ("Subsystem = Power_Supply");PrivateUeventobserver mueventobserver =NewUeventobserver (){@ OverridePublic VoidOnuevent (ueventobserver. ueventEvent) {Update ();}};

The Update () method is constantly called in uevent thread to update battery information.

 

Address: http://hi.baidu.com/maoxiaofei_2009/blog/item/39f147894b162518c8fc7a97.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.