The magical power of stickybroadcast in acquiring battery status

Source: Internet
Author: User

The magical power of stickybroadcast in acquiring battery status
today in the development of the time, suddenly encountered such a problem, when your device power is less than 15% when the device's flash is not open, but our platform solution to this piece did not make any hint, so directly caused the user think his flash is broken, So the boss asked us to solve the problem. we all know that the battery power information is obtained by broadcasting. The standard practice is as follows:
Private Broadcastreceiver Mbroadcastreceiver = new Broadcastreceiver () {@Override public void onreceive (context context  , Intent Intent) {String action = intent.getaction ();  if (Action.equals (intent.action_battery_changed)) {int status = Intent.getintextra ("status", 0);  int health = Intent.getintextra ("health", 0);  Boolean present = Intent.getbooleanextra ("Present", false);  int level = Intent.getintextra ("level", 0);  int scale = Intent.getintextra ("scale", 0);  int icon_small = Intent.getintextra ("Icon-small", 0);  int plugged = Intent.getintextra ("plugged", 0);  int voltage = Intent.getintextra ("voltage", 0);  int temperature = Intent.getintextra ("Temperature", 0);    String technology = Intent.getstringextra ("Technology");    String statusstring = "";  Switch (status) {case BatteryManager.BATTERY_STATUS_UNKNOWN:statusString = "UNKNOWN";  Break  Case BatteryManager.BATTERY_STATUS_CHARGING:statusString = "charging";  Break Case Batterymanager.battery_status_discharging:statusstring = "discharging";  Break  Case BatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString = "not charging";  Break  Case BatteryManager.BATTERY_STATUS_FULL:statusString = "full";  Break    } String healthstring = "";  Switch (health) {Case BatteryManager.BATTERY_HEALTH_UNKNOWN:healthString = "UNKNOWN";  Break  Case BatteryManager.BATTERY_HEALTH_GOOD:healthString = "good";  Break  Case BatteryManager.BATTERY_HEALTH_OVERHEAT:healthString = "overheat";  Break  Case BatteryManager.BATTERY_HEALTH_DEAD:healthString = "DEAD";  Break  Case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthString = "VOLTAGE";  Break  Case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthString = "UNSPECIFIED FAILURE";  Break    } String acstring = "";  Switch (plugged) {case BatteryManager.BATTERY_PLUGGED_AC:acString = "plugged AC";  Break  Case BatteryManager.BATTERY_PLUGGED_USB:acString = "plugged USB";  Break }  }

The specific information obtained is as follows:
   "Status" (int type) ... State, the defined value is batterymanager.battery_status_xxx.    "Health" (int type) ... Health, the defined value is batterymanager.battery_health_xxx. "    Present" (Boolean type)    "level" (int type) ... Battery remaining capacity    "scale" (int type) ... Maximum battery value. is usually 100.    "Icon-small" (int type) ... The icon ID.    "plugged" (int type) ... Charging status, the defined value is batterymanager.battery_plugged_xxx.    "voltage" (int type) ... mV.    "Temperature" (int type) ... Temperature, 0.1 degree unit. For example, when 197 is indicated, it means 19.7 degrees.     "Technology" (String type) ... Battery type, for example, Li-ion and so on.
In more detail, we're not going to go into the problem.My first idea was to accept the following broadcast
    <action android:name= "Android.intent.action.ACTION_BATTERY_LOW"/>     <action android:name= " Android.intent.action.ACTION_BATTERY_OKAY "/>

Maintain a status value in the Sharedpreference to monitor, but later found that there is no such needHere's to say our mighty stickybroadcast.in Google's developer documentation, we found
public abstract void Sendstickybroadcast (Intent Intent) Since:api Level 1 Perform a sendbroadcast (Intent) it is "stick Y, "meaning the Intent you were sending stays around after the broadcast was complete, so that others can quickly retrieve t Hat data through the return value of Registerreceiver (Broadcastreceiver, Intentfilter). In all other ways, this behaves the same as Sendbroadcast (Intent). You must hold the Broadcast_sticky permission in order to use this  API. If you don't hold the that permission, SecurityException'll be thrown. Parameters intent the    intent to broadcast;

Specifically, what does that mean, literally?This function sends a intent broadcast in a "sticky" manner, which means that the broadcast will remain stranded (waiting) after he is sent , so that all other registrants who have registered for the broadcast can receive this broadcast quickly. In other ways, he is no different from the general radio. at the same time, if we want to do this, we need to
    

Here, we need to focus on his explanation of parameter intent:
All registrants registered for change intent will be subject to the broadcast. The intent will be retained until the broadcast is re-sent to its prospective registrant.I may not be able to explain it clearly, but there are two points that we have to make clear:all registrants can receive them anytime, anywhere. (at any time, it means that all registrations of the broadcast will be received from the beginning of his issue until his next update)the broadcast will be updated and will be kept up-to-date with the latest broadcasts sent to the recipients. Well, here's the explanation, and then we'll talk about his strength.get the current battery statusBatterymanager sends a "sticky" type of system broadcast that includes information such as current battery status and charge status in Intent.
Because the broadcast type of the battery status is "sticky" type, we do not need to register the corresponding broadcastreceiver. You only need to pass null arguments when calling Registerreceiver, and then the return value of the function intent contains various information about the current battery state.
Of course you can also pass a custom broadcastreceiver, but it's not really necessary.
Example code:
Intentfilter ifilter = new Intentfilter (intent.action_battery_changed); Intent batterystatus = Context.registerreceiver (null, IFilter);

from the returned Intent we can get the current battery information, the way to get it: For example:
int level = Batterystatus.getintextra (Batterymanager.extra_level,-1); int scale = Batterystatus.getintextra ( Batterymanager.extra_scale,-1); float batterypct = level/(float) scale;

so we can get the battery power to determine if we can turn on the Flash .

Recommended reading:Android power management featured monitoring battery charge and charge status monitoring the Battery level and charging state
http://blog.csdn.net/g_rrrr/article/details/7999026








The magical power of stickybroadcast in acquiring battery status

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.