Android电池电量监听的示例代码

Android电池电量监听的示例代码,第1张

概述监听电池状态只需要接收Intent.ACTION_BATTERY_CHANGED的广播即可,当电池状态发生变化时会发出广播。

监听电池状态只需要接收Intent.ACTION_BATTERY_CHANGED的广播即可,当电池状态发生变化时会发出广播。

1.运行状态如下图:

1.充电中的状态

 

2.未充电时的状态

 

2.实现代码如下,各个状态通过名字就很容易知道意思,BatteryManager类中定义了电池状态。

public class MainActivity extends Activity {   private static final String TAG = "MainActivity";      private TextVIEw mTvVoltage;   private TextVIEw mTvTemperature;   private TextVIEw mTvLevel;   private TextVIEw mTvstatus;   private TextVIEw mTvHealth;   private TextVIEw mTvTechnology;    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);          mTvVoltage = (TextVIEw)findVIEwByID(R.ID.tv_voltage);     mTvTemperature = (TextVIEw)findVIEwByID(R.ID.tv_temperature);     mTvLevel = (TextVIEw)findVIEwByID(R.ID.tv_level);     mTvstatus = (TextVIEw)findVIEwByID(R.ID.tv_status);     mTvHealth = (TextVIEw)findVIEwByID(R.ID.tv_health);     mTvTechnology = (TextVIEw)findVIEwByID(R.ID.tv_technology);          this.registerReceiver(this.mBatteryReceiver,new IntentFilter(          Intent.ACTION_BATTERY_CHANGED));   }    @OverrIDe   public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.main,menu);     return true;   }    private broadcastReceiver mBatteryReceiver = new broadcastReceiver() {      @OverrIDe     public voID onReceive(Context arg0,Intent arg1) {        int voltage=arg1.getIntExtra(BatteryManager.EXTRA_VolTAGE,0);       mTvVoltage.setText("电压:" + voltage / 1000 + "." + voltage % 1000 + "V");               int temperature=arg1.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);       mTvTemperature.setText("温度:" + temperature / 10 + "." + temperature % 10 + "℃");       if (temperature >= 300) {         mTvTemperature.setTextcolor(color.RED);       } else {         mTvTemperature.setTextcolor(color.BLUE);       }              int level=arg1.getIntExtra(BatteryManager.EXTRA_LEVEL,0);       int scale=arg1.getIntExtra(BatteryManager.EXTRA_SCALE,0);       int levelPercent = (int)(((float)level / scale) * 100);       mTvLevel.setText("电量:" + levelPercent + "%");       if (level <= 10) {         mTvLevel.setTextcolor(color.RED);       } else {         mTvLevel.setTextcolor(color.BLUE);       }               int status = arg1.getIntExtra(BatteryManager.EXTRA_STATUS,BatteryManager.BATTERY_STATUS_UNKNowN);       String strStatus = "未知状态";;       switch (status) {       case BatteryManager.BATTERY_STATUS_CHARGING:         strStatus = "充电中……";         break;       case BatteryManager.BATTERY_STATUS_disCHARGING:         strStatus = "放电中……";         break;       case BatteryManager.BATTERY_STATUS_NOT_CHARGING:         strStatus = "未充电";         break;       case BatteryManager.BATTERY_STATUS_FulL:         strStatus = "充电完成";         break;       }       mTvstatus.setText("状态:" + strStatus);               int health = arg1.getIntExtra(BatteryManager.EXTRA_HEALTH,BatteryManager.BATTERY_HEALTH_UNKNowN);       String strHealth = "未知 :(";;       switch (status) {       case BatteryManager.BATTERY_HEALTH_GOOD:         strHealth = "好 :)";         break;       case BatteryManager.BATTERY_HEALTH_OVERHEAT:         strHealth = "过热!";         break;       case BatteryManager.BATTERY_HEALTH_DEAD: // 未充电时就会显示此状态,这是什么鬼?         strHealth = "良好";         break;       case BatteryManager.BATTERY_HEALTH_OVER_VolTAGE:         strHealth = "电压过高!";         break;       case BatteryManager.BATTERY_HEALTH_UnspecIFIED_FAILURE:         strHealth = "未知 :(";         break;       case BatteryManager.BATTERY_HEALTH_ColD:         strHealth = "过冷!";         break;       }       mTvHealth.setText("健康状况:" + strHealth);              String technology = arg1.getStringExtra(BatteryManager.EXTRA_TECHNolOGY);       mTvTechnology.setText("电池技术:" + technology);     }   }; }

3.Layout布局如下,很简单只有几个TextVIEw:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"         xmlns:tools="http://schemas.androID.com/tools"         androID:layout_wIDth="match_parent"         androID:layout_height="match_parent"         androID:paddingBottom="@dimen/activity_vertical_margin"         androID:paddingleft="@dimen/activity_horizontal_margin"         androID:paddingRight="@dimen/activity_horizontal_margin"         androID:paddingtop="@dimen/activity_vertical_margin"         tools:context=".MainActivity" >    <TextVIEw androID:ID="@+ID/tv_battery_status"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:textcolor="#0000FF"        androID:textStyle="bold"        androID:text="@string/battery_status" />    <linearLayout androID:layout_wIDth="match_parent"          androID:layout_height="wrap_content"          androID:orIEntation="vertical"          androID:layout_below="@ID/tv_battery_status" >     <TextVIEw androID:ID="@+ID/tv_voltage"          androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     <TextVIEw androID:ID="@+ID/tv_temperature"          androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     <TextVIEw androID:ID="@+ID/tv_level"          androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     <TextVIEw androID:ID="@+ID/tv_status"         androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     <TextVIEw androID:ID="@+ID/tv_health"         androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     <TextVIEw androID:ID="@+ID/tv_technology"          androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content" />     </linearLayout> </relativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android电池电量监听的示例代码全部内容,希望文章能够帮你解决Android电池电量监听的示例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存