Monitoring battery use New app Battery Monitor Add
Monitoring battery use
• • • New app, Battery. Monitor Add permission: BATTERY_STATS The battery is monitored by receiving broadcasts of battery state information Register to receive broadcasts In on. Create add – Intent. Filter battery. Level. Filter = new Intent. Filter(Intent. ACTION_BATTERY_CHANGED); – register. Receiver(battery. Level. Receiver, battery. Level. Filter); Add broadcast receiver as activity member variable. – Broadcast. Receiver battery. Level. Receiver = new Broadcast. Receiver() { – public void on. Receive(Context context, Intent intent) { – context. unregister. Receiver(this); – int rawlevel = intent. get. Int. Extra(Battery. Manager. EXTRA_LEVEL, -1); – int scale = intent. get. Int. Extra(Battery. Manager. EXTRA_SCALE, -1); – int level = -1; – if (rawlevel >= 0 && scale > 0) { – level = (rawlevel * 100) / scale; – } – Log. e("DEBUG", "Battery Level Remaining: " + level + "%"); – }; • run
Show more battery things • • • Add member variable – Text. View text. View; – String battery. Str = new String(); Add Text. View to layout – Set Misc. Layout. Height = fill_parent – Set Misc. Layout. Width = fill_parent In on. Create, add – text. View = (Text. View)find. View. By. Id(R. id. Text. View 01); – text. View. set. Movement. Method(new Scrolling. Movement. Method()); In battery. Level. Receiver. on. Receive, add to beginning – battery. Str += "------------------------n"; – Calendar calendar = new Gregorian. Calendar(); – battery. Str += "time: "+calendar. get(Calendar. HOUR)+": "+calendar. get(Calendar. MINUTE)+": "+calendar. get(Calendar. SECOND) +"n"; Change Log. e("DEBUG", "Battery Level Remaining: " + level + "%"); , to – battery. Str += "Battery Level Remaining: " + level + "%n"; • At the end of battery. Level. Receiver. on. Receive, add – text. View. set. Text(battery. Str); • Run – Plug and unplug until textview is full and check that view is scrollable
Battery plugged state • In battery. Level. Receiver. on. Reciever, before text. View. set. Text…, add – int plugged = intent. get. Int. Extra(Battery. Manager. EXTRA_PLUGGED, -1); – switch (plugged) { – case 0: – battery. Str += "unpluggedn"; – break; – case Battery. Manager. BATTERY_PLUGGED_AC: – battery. Str += "Plugged into ACn"; – break; – case Battery. Manager. BATTERY_PLUGGED_USB: – battery. Str += "Plugged into USBn"; – break; – default: battery. Str += "unknown plugged staten"; – }
• • • • • • Battery status int status = intent. get. Int. Extra(Battery. Manager. EXTRA_STATUS, -1); switch (status) { case Battery. Manager. BATTERY_STATUS_CHARGING: battery. Str += "battery is chargingn"; break; case Battery. Manager. BATTERY_STATUS_DISCHARGING: battery. Str += "battery is dischargingn"; break; case Battery. Manager. BATTERY_STATUS_FULL: battery. Str += "battery is fulln"; break; case Battery. Manager. BATTERY_STATUS_NOT_CHARGING: battery. Str += "battery is not chargingn"; break; case Battery. Manager. BATTERY_STATUS_UNKNOWN : battery. Str += "battery status is unknownn"; break; default: battery. Str += "battery status should not happenn"; break; }
Battery type, temperature, voltage, • String tech = intent. get. String. Extra(Battery. Manager. EXTRA_TECHNOLOGY); • battery. Str += "technology: " + tech +"n"; • int temp = intent. get. Int. Extra(Battery. Manager. EXTRA_TEMPERATURE, -1); • battery. Str += "temp: " + temp+"n"; • int voltage = intent. get. Int. Extra(Battery. Manager. EXTRA_VOLTAGE, -1); • battery. Str += "voltage: " + voltage+"n";
• • • • • • • Battery health int health = intent. get. Int. Extra(Battery. Manager. EXTRA_HEALTH, -1); switch (health) { /*case Battery. Manager. BATTERY_HEALTH_COLD: battery. Str += "health = coldn"; break; //in documentation, but not supported */ case Battery. Manager. BATTERY_HEALTH_DEAD: battery. Str += "health = deadn"; break; case Battery. Manager. BATTERY_HEALTH_GOOD: battery. Str += "health = goodn"; break; case Battery. Manager. BATTERY_HEALTH_OVERHEAT: battery. Str += "health = overheatn"; break; case Battery. Manager. BATTERY_HEALTH_OVER_VOLTAGE: battery. Str += "health = over voltagen"; break; case Battery. Manager. BATTERY_HEALTH_UNKNOWN: battery. Str += "health = unknownn"; break; case Battery. Manager. BATTERY_HEALTH_UNSPECIFIED_FAILURE: battery. Str += "health = unspecified failuren"; break; default: battery. Str += "health state: should not happenn"; break; }
More fun with battery level • Let’s estimate battery charge/discharge rate • Add member variables – long last. Time = 0; – double last. Level; • After level is computed, add – double rate = 0; – double termination. Time=0; – if (last. Time!=0) { • rate = (level - last. Level)/(calendar. get. Time. In. Millis()-last. Time)*1000. 0; • if (rate>0) { – termination. Time = (100 -level)/rate/3600; – battery. Str += "charged in "+termination. Time+" hoursn"; • } • if (rate<0) { • } – termination. Time = -level/rate/3600; – battery. Str += "discharged in "+termination. Time+" hoursn"; – } – last. Time = calendar. get. Time. In. Millis(); – last. Level = level;
Battery Monitor Service • Make new class – Battery. Monitor. Service – Derived from android. app. Service • To manifest, add service, i. e, – In Application tab • Application nodes, add • Select radio button “Create a new element at the top level, in application” • Select “service” • Select ok – With the new service highlighted, • In name (on the right), add – Battery. Monitor. Service • Or, select “browse” and (wait) and select Battery. Monitor. Service
In Battery. Monitor. Service • • @Override public void on. Create() { – super. on. Create(); – Log. d("Battery. Monitor. Service", "Created"); – Intent. Filter battery. Level. Filter = new Intent. Filter(Intent. ACTION_BATTERY_CHANGED); register. Receiver(battery. Level. Receiver, battery. Level. Filter); } • • private final Binder binder = new Local. Binder(); public class Local. Binder extends Binder { • • – Battery. Monitor. Service get. Service() { • • – • • } Log. d("Battery. Monitor. Service", "localbinder"); return Battery. Monitor. Service. this; } Change on. Bind, to @Override public IBinder on. Bind(Intent intent) { – – // TODO Auto-generated method stub return binder; • } • • @Override public void on. Destroy() { – – – • } super. on. Destroy(); Log. d("Battery. Monitor. Service", "Destroyed"); unregister. Receiver(battery. Level. Receiver);
In Battery. Monitor. Service • Move broadcast receiver from activity to service • Move member variables – String battery. Str = new String(); – long last. Time = 0; – double last. Level; • Broadcast. Receiver battery. Level. Receiver = new Broadcast. Receiver() { • public void on. Receive(Context context, Intent intent) { • ……
In Battery. Monitor • • • Start service. In On. Create, add – start. Service(new Intent(this, Battery. Monitor. Service. class)); – bind. Service(new Intent(this, Battery. Monitor. Service. class), connection. To. My. Service, BIND_AUTO_CREATE); Add member variable – boolean started = false; After on. Create, add – private Battery. Monitor. Service battery. Monitor. Service = null; – private Service. Connection connection. To. My. Service = new Service. Connection() { • @Override • public void on. Service. Connected(Component. Name class. Name, IBinder raw. Binder) { – battery. Monitor. Service = ((Battery. Monitor. Service. Local. Binder)raw. Binder). get. Service(); – started = true; • } • @Override • public void on. Service. Disconnected(Component. Name name) { – battery. Monitor. Service = null; – started = false; • } – };
In Battery. Monitor • We need to get the battery. Str from the service and post it on the textview. – We can use a intent to send the message to get the string – Intent. Filter intent. Filter = new Intent. Filter(); – intent. Filter. add. Action("edu. udel. eleg 454. New. Battery. Monitor. String. Is. Ready"); – register. Receiver(new Broadcast. Receiver() { • public void on. Receive(Context context, Intent intent) { – Log. d("DEBUG", "Received intent "+intent); – text. View. set. Text(battery. Monitor. Service. battery. Str); • } – }, intent. Filter); • • • Let’s get the string when the UI becomes active after being out of focus @Override public void on. Resume() { – super. on. Resume(); – if (started) { • text. View. set. Text(battery. Monitor. Service. battery. Str); – } } // note: battery. Monitor. Service is only valid after the connection with the service has been established. See connection. To. My. Service • •
In Battery. Monitor. Service • At the end of Broadcast. Receiver battery. Level. Receiver = new Broadcast. Receiver() { …. – Remove – text. View. set. Text(battery. Str); • Add – Intent broadcast. Intent = new Intent(); – broadcast. Intent. set. Action("edu. udel. eleg 454. New. Battery. Monitor. String. Is. Ready"); – send. Broadcast(broadcast. Intent); • Run and play with gps on, wifi, etc. and see impact on battery usage
- Slides: 14