Android电池充电显示数据
程序员文章站
2024-03-22 18:25:40
...
今天给自己的手机充电,老是判断是交流电充电还是USB充电,于是想做个APP显示充电时的数据,不说了,直接上代码。
package com.example.powerbattery;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView t1;
String plug;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.tx1);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
// 是否在充电
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING||
status == BatteryManager.BATTERY_STATUS_FULL;
// 怎么充
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCarge =chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB){
plug = "USB";
}else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC){
plug = "交流电";
}
//当前剩余电量
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
//电量最大值
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Double i = batteryStatus.getDoubleExtra(BatteryManager.EXTRA_SCALE, -1);
//电量百分比
float batteryPct = level / (float)scale;
int temperature= batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
// integer containing the current battery voltage level.
int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
String healthStr = "";
switch (health){
case BatteryManager.BATTERY_HEALTH_COLD:
healthStr="cold";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthStr="dead";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthStr="good";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthStr="voer voltage";
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthStr="unknown";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthStr="unspecified failure";
break;
}
t1.setText("是否在充电:" + isCharging);
t1.append("\n是否USB充电:" + usbCharge);
t1.append("\n是否交流电充电:" + acCarge);
t1.append("\n充电方式:" + plug);
t1.append("\n电量百分比:" + (batteryPct * 100) + "%");
t1.append("\n电池状态:"+healthStr);
t1.append("\n电池温度:"+temperature);
t1.append("\n电池电压:"+voltage);
}
}
结果图:
下一篇: 轻量型注意力模块:ULSAM
推荐阅读
-
Android电池充电显示数据
-
Android 根据EditText搜索框ListView动态显示数据
-
Android 根据EditText搜索框ListView动态显示数据
-
Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解
-
Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解
-
android要做附近商家显示,显示多少米范围内商家,php数据库如何设计坐标的
-
android要做附近商家显示,显示多少米范围内商家,php数据库如何设计坐标的
-
Android编程使用ListView实现数据列表显示的方法
-
Android编程使用ListView实现数据列表显示的方法
-
Android listview数据显示及提示信息的实例