自动获取手机电池的剩余电量
通过使用BroadcastReceiver的特性来获取手机电池的电量,注册BroadcastReceiver时设置的IntentFilter来获取系统发出的Intent.ACTION_BATTERY_CHANGED,然后以此来获取电池的电量。
运行截图:
程序结构
package com.example.asus.gary_040a;import android.app.Dialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;import org.w3c.dom.Text;public class MainActivity extends AppCompatActivity { private int intLevel; private int intScale; private Button mButton01; private TextView tv; //创建BroadcastReceiver private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //如果捕捉到的Action是ACTION_BATTERY_CHANGED则运行onBatteryInforECEIVER() if(intent.ACTION_BATTERY_CHANGED.equals(action)) { //获得当前电量 intLevel = intent.getIntExtra("level",0); //获得手机总电量 intScale = intent.getIntExtra("scale",100); // 在下面会定义这个函数,显示手机当前电量 onBatteryInfoReceiver(intLevel, intScale); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton01 = (Button) findViewById(R.id.myButton1); mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // 注册一个BroadcastReceiver,作为访问电池计量之用 registerReceiver(mBatInfoReveiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } }); } // 拦截到ACTION_BATTRY_CHANGED后要执行的动作 private void onBatteryInfoReceiver(int intLevel, int intScale) { // TODO Auto-generated method stub int percent = intLevel*100/ intScale; //得到的person就是百分比电量 //不乘100得到的percent为0 tv=(TextView) findViewById(R.id.myTextView02); tv.setText("现在的电量是"+percent+"%。"); };}
一、界面布局
一个Button按钮,一个TextView文本框
点击Button时会在TextView上显示(手机电量)提示
二、实现程序功能
1、如果捕捉的Action是ACTION_BATTERY_CHANGED则运行onBatteryInfoReceiver()显示当前手机电量
private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //如果捕捉到的Action是ACTION_BATTERY_CHANGED则运行onBatteryInforECEIVER() if(intent.ACTION_BATTERY_CHANGED.equals(action)) { //获得当前电量 intLevel = intent.getIntExtra("level",0); //获得手机总电量 intScale = intent.getIntExtra("scale",100); // 在下面会定义这个函数,显示手机当前电量 onBatteryInfoReceiver(intLevel, intScale); } } };
2、对Button按钮添加单击后的事件响应动作,注册系统BroadcastReceiver广播事件来访问电池电量
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton01 = (Button) findViewById(R.id.myButton1); mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // 注册一个BroadcastReceiver,作为访问电池计量之用 registerReceiver(mBatInfoReveiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } }); }
3、定义方法onBatteryInfoReceiver(),通过这个方法能在TextView文本框上显示手机剩余电量
// 拦截到ACTION_BATTRY_CHANGED后要执行的动作 private void onBatteryInfoReceiver(int intLevel, int intScale) { // TODO Auto-generated method stub int percent = intLevel*100/ intScale; //得到的person就是百分比电量 //不乘100得到的percent为0 tv=(TextView) findViewById(R.id.myTextView02); tv.setText("现在的电量是"+percent+"%。"); };