The method of battery information update (based on batteryservice implementation) of Android programming _android

Source: Internet
Author: User

This article is an example of how to update the battery power information of Android programming. Share to everyone for your reference, specific as follows:

Battery information, voltage, temperature, charging status, etc., are provided by Batteryservice. Batteryservice is run in the system_process, when the system initialization, the following

You can see the code to start Batteryservice in Systemserver.java:

LOG.I (TAG, "Starting Battery Service.");
Batteryservice battery = new Batteryservice (context);
Servicemanager.addservice ("Battery", battery);

1. Data sources

Batteryservice reads data through JNI (com_android_server_batteryservice.cpp). Batteryservice not only has functions, but also variables, that are registered through JNI. As follows:

############# #在BatteryService. Variables declared in 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 the Batteryservice.java are used in com_android_server_batteryservice.cpp, that is, in Com_android_server_ In fact, BatteryService.cpp is also a variable 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 these variables, which are read from the file below, store a value in a file.

#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 is running on top of the Linux kernel, and/sys/class/power_supply is also the directory underneath the Linux kernel. As for how these files are generated, it is controlled by platform.

2. Data transfer

This information about the battery is obtained by other applications in what way. Can think of two ways, the first one, the application of the initiative to obtain data from the Batteryservice, the second, Batteryservice actively transfer data to the application concerned.

Batteryservice uses the second way, all the battery information data is transmitted through the intent. In Batteryservice.java, the code is as follows:

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. Data reception

Application if you want to receive the battery information sent by batteryservice, you need to register a intent for intent.action_battery_changed Broadcastreceiver.

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 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. Data Update

The battery's information changes over time, and naturally, you need to consider how to update the battery's data in real time. When the Batteryservice is started, a onuevent Thread is launched at the same time through the ueventobserver.

Each process can have at most one onuevent Thread, even though there are multiple instances of ueventobserver in this process. When the first call Startobserving () method in a process, the uevent thread starts.

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

In the Batteryservice.java 
mueventobserver.startobserving ("subsystem=power_supply");
Private Ueventobserver Mueventobserver = new Ueventobserver () { 
  @Override public 
  void Onuevent ( Ueventobserver.uevent event) { 
   update (); 
  } 
};

The update () method is called continuously in uevent thread to update the battery's information data.

I hope this article will help you with the Android program.

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.