-
-
Save tieubao/8c4f9c73f295037310ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BatteryActivity extends Activity { | |
//UI Elements | |
private TextView mTextViewLevel; | |
private TextView mTextViewTemperature; | |
private TextView mTextViewVoltage; | |
private TextView mTextViewHealth; | |
//Battery details | |
private int level; | |
private int voltage; | |
private int temperature; | |
private int healthState; | |
private int chargePlug; | |
private int status; | |
boolean celsius = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_battery); | |
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); | |
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { | |
@Override | |
public void onLayoutInflated(WatchViewStub stub) { | |
//ui elements | |
mTextViewLevel = (TextView) stub.findViewById(R.id.level); | |
mTextViewTemperature = (TextView) stub.findViewById(R.id.temperature); | |
mTextViewVoltage = (TextView) stub.findViewById(R.id.voltage); | |
mTextViewHealth = (TextView) stub.findViewById(R.id.health); | |
//update battery details | |
updateBattery(); | |
} | |
}); | |
} | |
private void updateBattery() { | |
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | |
Intent batteryStatus = registerReceiver(null, ifilter); | |
//Level | |
level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | |
//Health | |
healthState = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); | |
int healthId = getResources().getIdentifier("battery_health_" + healthState, | |
"string", this.getPackageName()); | |
String health = getString(healthId); | |
//Battery status | |
//status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); | |
//int resIdCharging = getResources().getIdentifier("charging_" + status,"string", this.getPackageName()); | |
//String charging = getString(resIdCharging); | |
//Temperature | |
temperature = batteryStatus.getIntExtra( | |
BatteryManager.EXTRA_TEMPERATURE, 0); | |
if (celsius){ | |
temperature= temperature/10; | |
}else{ | |
temperature= temperature/10; | |
//temperature= (int)( (float)((9.0f * temperature) / 5.0f) + 32.0f); | |
temperature= (int)( 9.0 * temperature / 5.0 + 32.0); | |
} | |
//Voltage | |
voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); | |
//Charge | |
//chargePlug = batteryStatus.getIntExtra( | |
// BatteryManager.EXTRA_PLUGGED, -1); | |
//int resId = getResources().getIdentifier("charge_" + chargePlug, | |
// "string", this.getPackageName()); | |
//String charge = getString(resId); | |
if (mTextViewLevel!=null) mTextViewLevel.setText(""+level+"%"); | |
if (mTextViewTemperature!=null) mTextViewTemperature.setText(temperature+"°C"); | |
if (mTextViewVoltage!=null) mTextViewVoltage.setText(voltage+"mV"); | |
if (mTextViewHealth!=null) mTextViewHealth.setText(health); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment