Android获取手机电池电量用法实例
作者:Ruthless 时间:2021-08-05 23:14:34
本文实例讲述了Android获取手机电池电量用法。分享给大家供大家参考。具体如下:
原理概述:
手机电池电量的获取在应用程序的开发中也很常用,Android系统中手机电池电量发生变化的消息是通过Intent广播来实现的,常用的Intent的Action有 Intent.ACTION_BATTERY_CHANGED(电池电量发生改变时)、Intent.ACTION_BATTERY_LOW(电池电量达到下限时)、和Intent.ACTION_BATTERY_OKAY(电池电量从低恢复到高时)。
当需要在程序中获取电池电量的信息时,需要为应用程序注册BroadcastReceiver组件,当特定的Action事件发生时,系统将会发出相应的广播,应用程序就可以通过BroadcastReceiver来接受广播,并进行相应的处理。
main.xml布局文件:
<?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类:
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,手机,电量
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
举例详解用Java实现web分页功能的方法
2021-07-27 10:50:19
WPF ProgressBar实现实时进度效果
2023-01-14 08:05:49
![](https://img.aspxhome.com/file/2023/0/102230_0s.png)
C#中调用SAPI实现语音合成的2种方法
2023-07-07 09:36:00
Android横竖屏幕切换小结
2023-04-15 08:03:20
Java 常见的限流算法详细分析并实现
2022-01-05 22:11:09
![](https://img.aspxhome.com/file/2023/3/71923_0s.png)
进度条ProgressBar及ProgressDialog(实例)
2021-09-24 01:07:42
springboot:接收date类型的参数方式
2023-03-19 12:48:31
![](https://img.aspxhome.com/file/2023/0/85520_0s.png)
C#实现获取机器码的示例详解
2021-11-13 06:57:19
![](https://img.aspxhome.com/file/2023/6/80096_0s.png)
一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
2022-06-08 16:36:41
![](https://img.aspxhome.com/file/2023/7/98197_0s.png)
Android自定义实现可滑动按钮
2021-10-21 08:24:11
C#如何使用Bogus创建模拟数据示例代码
2023-10-11 10:57:50
![](https://img.aspxhome.com/file/2023/6/94606_0s.png)
jar包手动添加到本地maven仓库的步骤详解
2023-11-23 05:09:37
![](https://img.aspxhome.com/file/2023/5/59785_0s.png)
Spring Bean创建流程分析讲解
2022-04-20 22:53:07
![](https://img.aspxhome.com/file/2023/7/71177_0s.png)
Java语言实现简单FTP软件 FTP上传下载队列窗口实现(7)
2021-07-25 04:01:58
![](https://img.aspxhome.com/file/2023/3/63343_0s.png)
Java DOM4J方式生成XML的方法
2022-07-19 02:32:42
SpringBoot整合SpringTask实现定时任务的流程
2022-03-28 22:24:40
C#利用缓存分块读写大文件
2022-10-20 11:18:22
Android编程基础之获取手机屏幕大小(DisplayMetrics应用)示例
2023-09-26 17:57:43
![](https://img.aspxhome.com/file/2023/3/85983_0s.jpg)
Java中的异常处理用法及其架构和使用建议
2023-02-05 21:27:16
![](https://img.aspxhome.com/file/2023/5/74885_0s.jpg)
idea打包java可执行jar包的实现步骤
2022-07-30 02:57:48
![](https://img.aspxhome.com/file/2023/3/86843_0s.png)