服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android获取手机电池电量用法实例

Android获取手机电池电量用法实例

2021-04-01 15:41Ruthless Android

这篇文章主要介绍了Android获取手机电池电量用法,以完整实例形式较为详细的分析了Android获取手机电量的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android获取手机电池电量用法。分享给大家供大家参考。具体如下:

原理概述:

手机电池电量的获取在应用程序的开发中也很常用,android系统中手机电池电量发生变化的消息是通过intent广播来实现的,常用的intent的action有  intent.action_battery_changed(电池电量发生改变时)、intent.action_battery_low(电池电量达到下限时)、和intent.action_battery_okay(电池电量从低恢复到高时)。

当需要在程序中获取电池电量的信息时,需要为应用程序注册broadcastreceiver组件,当特定的action事件发生时,系统将会发出相应的广播,应用程序就可以通过broadcastreceiver来接受广播,并进行相应的处理。

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <togglebutton android:id="@+id/tb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:texton="停止获取电量信息"
    android:textoff="获取电量信息" />
  <textview android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</linearlayout>

batteryactivity类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.ljq.activity;
import android.app.activity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.widget.compoundbutton;
import android.widget.textview;
import android.widget.togglebutton;
import android.widget.compoundbutton.oncheckedchangelistener;
public class batteryactivity extends activity {
  private togglebutton tb=null;
  private textview tv=null;
  private batteryreceiver receiver=null;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    receiver=new batteryreceiver();
    tv=(textview)findviewbyid(r.id.tv);
    tb=(togglebutton)findviewbyid(r.id.tb);
    tb.setoncheckedchangelistener(new oncheckedchangelistener(){
      public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) {
        //获取电池电量
        if(ischecked){
          intentfilter filter=new intentfilter(intent.action_battery_changed);
          registerreceiver(receiver, filter);//注册broadcastreceiver
        }else {
          //停止获取电池电量
          unregisterreceiver(receiver);
          tv.settext(null);
        }
      }
    });
  }
  private class batteryreceiver extends broadcastreceiver{
    @override
    public void onreceive(context context, intent intent) {
      int current=intent.getextras().getint("level");//获得当前电量
      int total=intent.getextras().getint("scale");//获得总电量
      int percent=current*100/total;
      tv.settext("现在的电量是"+percent+"%。");
    }
  }
}

运行结果:

Android获取手机电池电量用法实例

希望本文所述对大家的android程序设计有所帮助。

延伸 · 阅读

精彩推荐