自動獲取手機電池的剩余電量
通過使用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+"%。"); }; }

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.asus.gary_040a.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gary_自動獲取手機電池的剩余電量!" android:textSize="40px" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/myButton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/myTextView02" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50px" /> </LinearLayout>
一、界面布局
一個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+"%。"); };