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

所属分类: 软件编程 / Android 阅读数: 711
收藏 0 赞 0 分享

本文实例讲述了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 byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多