Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------
- File: BatteryView.java
- ---------------------------
- package com.skovmandsit.batteryapp;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.BatteryManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v4.content.LocalBroadcastManager;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class BatteryView extends Activity {
- private BatteryReceiver batteryReceiver;
- private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- public TextView txChargeState, txInformationAge, txPercentCharged;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_battery_view);
- txChargeState = (TextView) findViewById(R.id.chargeState);
- txInformationAge = (TextView) findViewById(R.id.informationAge);
- txPercentCharged = (TextView) findViewById(R.id.percentCharged);
- // Create the battery receiver
- batteryReceiver = new BatteryReceiver();
- batteryReceiver.setupTextViews(txChargeState,txInformationAge,txPercentCharged);
- }
- @Override
- protected void onResume() {
- super.onResume();
- // Register the broadcastreceiver for the battery status:
- if (batteryReceiver != null) {
- registerReceiver(batteryReceiver, intentFilter);
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- // Register the broadcastreceiver here:
- if (batteryReceiver != null) {
- unregisterReceiver(batteryReceiver);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.battery_view, menu);
- return true;
- }
- public void updateTextFields() {
- txChargeState.setText( batteryReceiver.getChargeStateString() );
- txPercentCharged.setText( batteryReceiver.getCurrentCharge() + "%" );
- txInformationAge.setText( batteryReceiver.getInformationFreshnessString() );
- }
- }
- ---------------------------
- File: BatteryReceiver.java
- ---------------------------
- package com.skovmandsit.batteryapp;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.BatteryManager;
- import android.support.v4.content.LocalBroadcastManager;
- import android.widget.TextView;
- public class BatteryReceiver extends BroadcastReceiver {
- protected enum batteryStates { CHARGING, DISCHARGING, FULL, UNKNOWN }
- protected enum powerSources { WIRELESS, AC, USB, UNKNOWN }
- protected batteryStates currentBatteryState = batteryStates.UNKNOWN;
- protected powerSources currentPowerSource = powerSources.UNKNOWN;
- protected boolean informationIsFresh = false;
- protected int currentCharge = 0;
- public TextView txChargeState, txInformationAge, txPercentCharged;
- public void setupTextViews(TextView mTxChargeState, TextView mTxInformationAge, TextView mTxPercentCharged) {
- txChargeState = mTxChargeState;
- txInformationAge = mTxInformationAge;
- txPercentCharged = mTxPercentCharged;
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- // Get battery level and power source ...
- currentBatteryState = returnBatteryState(intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1));
- currentPowerSource = returnPowerSource(intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1));
- // Get information age ...
- if (isInitialStickyBroadcast()) {
- informationIsFresh = false;
- } else {
- informationIsFresh = true;
- }
- // Get current charge ...
- currentCharge = returnCurrentCharge(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1));
- txChargeState.setText( getChargeStateString() );
- txPercentCharged.setText( getCurrentCharge() + "%" );
- txInformationAge.setText( getInformationFreshnessString() );
- }
- // Private methods:
- private int returnCurrentCharge(int level, int scale) {
- return (int) ((level / (float)scale) * 100);
- }
- private batteryStates returnBatteryState(int batteryState) {
- switch (batteryState) {
- case BatteryManager.BATTERY_STATUS_CHARGING:
- return batteryStates.CHARGING;
- case BatteryManager.BATTERY_STATUS_DISCHARGING:
- return batteryStates.DISCHARGING;
- case BatteryManager.BATTERY_STATUS_FULL:
- return batteryStates.FULL;
- default:
- return batteryStates.UNKNOWN;
- }
- }
- private powerSources returnPowerSource(int powerSource) {
- switch (powerSource) {
- case BatteryManager.BATTERY_PLUGGED_USB:
- return powerSources.USB;
- case BatteryManager.BATTERY_PLUGGED_AC:
- return powerSources.AC;
- case BatteryManager.BATTERY_PLUGGED_WIRELESS:
- return powerSources.WIRELESS;
- default:
- return powerSources.UNKNOWN;
- }
- }
- // Public methods / Interface:
- public String getChargingMethod() {
- // Should all be of a kind that says "Charging using: ...."
- switch (currentPowerSource) {
- case USB:
- return "USB";
- case AC:
- return "AC outlet";
- case WIRELESS:
- return "wireless charger";
- default:
- return "unknown method";
- }
- }
- public String getBatteryState() {
- // Should all be of a kind that says "Battery is: ...."
- return currentBatteryState.toString().toLowerCase();
- }
- public String getChargeStateString() {
- switch (currentBatteryState) {
- case CHARGING:
- return "Battery is charging using " + getChargingMethod();
- case DISCHARGING:
- return "Battery is discharging";
- case FULL:
- return "Battery is full";
- default:
- return "Battery and charge state unknown";
- }
- }
- public String getInformationFreshnessString() {
- if (informationIsFresh)
- return "Information is fresh.";
- else
- return "Information is from before the app started";
- }
- public int getCurrentCharge() {
- return currentCharge;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement