欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-13 18:21:34
监听电池状态只需要接收intent.action_battery_changed的广播即可,当电池状态发生变化时会发出广播。 1.运行状态如下图: 1.充电中的状态...

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

1.运行状态如下图:

1.充电中的状态

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

2.未充电时的状态

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

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>

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

上一篇:

下一篇: