1 / 6

Monitoring battery power

Monitoring battery power. overview. Battery usage is critical on mobile apps Measuring energy usage is a bit tricky since many processes Android provides broadcast sticky intents on the battery level The intent contents various information about the battery state

clive
Download Presentation

Monitoring battery power

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Monitoring battery power

  2. overview • Battery usage is critical on mobile apps • Measuring energy usage is a bit tricky since many processes • Android provides broadcast sticky intents on the battery level • The intent contents various information about the battery state • How can you get the current battery level? • Do you have to wait for a change in battery level to receive a new intent that contains the new information? • No, the OS generates a sticky intent, which will stay around until a new one is generated. • As soon as you register to get a battey level change intent, you will get the most recent intent. • After that, you will get the intents as they are generated • If your app is able to get them?

  3. Make a new app, BatteryLevelFun • Add textView to UI, with Id textView1 • In BatteryLevelFunActivity • Add member variable • TextViewtextView; • IntentFilterbatteryLevelFilter = null; • In onCreate, add • textView = (TextView)findViewById(R.id.textView1); • //register to receive broadcast intents about battery level • batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); • Make onResume • @Override • public void onResume() { • super.onResume(); • registerReceiver(batteryLevelReceiver, batteryLevelFilter); • } • Make onPause • @Override • public void onPause() { • super.onPause(); • unregisterReceiver(batteryLevelReceiver); • } • Object to receive broadcast intents • BroadcastReceiverbatteryLevelReceiver = new BroadcastReceiver() { • public void onReceive(Context context, Intent intent) { • processBatteryIntent(intent); • } • };

  4. Function to process battery intent • DateTimelastTime = null; • double lastLevel; • public void processBatteryIntent(Intent intent) { • DateTimecurrentTime = new DateTime(); // needs joda time • String batteryStr = new String(); • intrawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); • int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); • double level = -1; • if (rawlevel >= 0 && scale > 0) { • level = (rawlevel * 100) / (double)scale; • } • double rate = 0; • if (lastTime!=null && lastLevel!=level) { • rate = (level - lastLevel)/(currentTime.getMillis()-lastTime.getMillis())*1000.0; • batteryStr += String.format("charging rate: %f ", rate); • if (level>lastLevel) • batteryStr += String.format("fully charged in %f hours\n", (100-level)/rate/3600.0); • else • batteryStr += String.format("fully discharged in %f hours\n", (level)/-rate/3600.0); • } • batteryStr += "current level: " + level + " last level: " + lastLevel + " "; • if (lastTime==null || lastLevel!=level) { • lastTime = currentTime; • lastLevel = level; • } • batteryStr += "\nbatterytemperture: "+((double)intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)/10.0) + " degrees C\n"; • int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); • switch (plugged) { • case 0: • batteryStr += "unplugged\n"; • break; • case BatteryManager.BATTERY_PLUGGED_AC: • batteryStr += "Plugged into AC\n"; • break; • case BatteryManager.BATTERY_PLUGGED_USB: • batteryStr += "Plugged into USB\n"; • break; • default: batteryStr += "unknown plugged state\n"; • } • int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); • batteryStr += "battery health: "; • switch (health) { • case BatteryManager.BATTERY_HEALTH_DEAD: • batteryStr += "dead\n"; break; • case BatteryManager.BATTERY_HEALTH_GOOD: • batteryStr += "good\n"; break; • case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: • batteryStr += "over voltage\n"; break; • case BatteryManager.BATTERY_HEALTH_OVERHEAT: • batteryStr += "overheat\n"; break; • case BatteryManager.BATTERY_HEALTH_UNKNOWN: • batteryStr += "unknown\n"; break; • case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: • batteryStr += "some failure\n"; break; • } • int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); • batteryStr += "battery status: "; • switch (status) { • case BatteryManager.BATTERY_STATUS_CHARGING: • batteryStr += "charigng\n"; break; • case BatteryManager.BATTERY_STATUS_DISCHARGING: • batteryStr += "discharging\n"; break; • case BatteryManager.BATTERY_STATUS_FULL: • batteryStr += "full\n"; break; • case BatteryManager.BATTERY_STATUS_NOT_CHARGING: • batteryStr += "not charing\n"; break; • case BatteryManager.BATTERY_STATUS_UNKNOWN: • batteryStr += "unknown\n"; break; • } • int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); • batteryStr += "battery voltage: "+ voltage + "\n"; • String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); • batteryStr += "battery technology: "+ technology +"\n"; • boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); • if (present) • batteryStr += "battery is present\n"; • else • batteryStr += "battery is not present\n"; • textView.setText(batteryStr); • }

  5. test • Run it. Try plugging and unplugging usb cable • It takes a while to get a new update • Try registering broadcast receiver in onCreate and not in onResume and doing nothing in onPause. And then run it and back arrow • In onResume, delete • registerReceiver(batteryLevelReceiver, batteryLevelFilter); • In onPause, delete • unregisterReceiver(batteryLevelReceiver); • In onCreate, add • registerReceiver(batteryLevelReceiver, batteryLevelFilter); • Run it, and use backward arrow. • Might crash • Will give error message about a problem with a broadcast receiver • The problem is that the broadcast receiver is registered, but the app is not running • Get battery level immediately • In onCreate, add • Intent current = registerReceiver(null, batteryLevelFilter); • processBatteryIntent(current);

More Related