1. MainActivity:
1 package com.example.broadcastdemo; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.BroadcastReceiver; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.IntentFilter; 9 import android.os.BatteryManager; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.widget.TextView; 13 14 public class MainActivity extends AppCompatActivity { 15 16 private static final String TAG = "MainActivity"; 17 private TextView mTextBatteryLevel; 18 private BatteryLevelReceiver mBatteryLevelReceiver; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 initView(); 26 registerBatteryReceiver(); 27 } 28 29 private void initView() { 30 mTextBatteryLevel = (TextView) this.findViewById(R.id.battery_level); 31 } 32 33 /** 34 * 注册广播 35 */ 36 public void registerBatteryReceiver() { 37 // 第二步,要收听的频道是:电量变化 38 IntentFilter intentFilter = new IntentFilter(); 39 // 第三步,设置频道(即表明要监听什么广播 40 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 41 intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); 42 intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); 43 // 第四步,初始化接收者 44 mBatteryLevelReceiver = new BatteryLevelReceiver(); 45 // 第五步,注册广播 46 // 从第二步到第五步便是动态注册 47 // You cannot receive this through components declared 48 // in manifests, only by explicitly registering for it with 49 // {@link Context#registerReceiver(BroadcastReceiver, IntentFilter) 50 // Context.registerReceiver()}. 51 this.registerReceiver(mBatteryLevelReceiver, intentFilter); 52 } 53 54 /** 55 * 第一步,创建一个广播接收者,继承自BroadcastReceiver 56 */ 57 private class BatteryLevelReceiver extends BroadcastReceiver{ 58 59 @Override 60 public void onReceive(Context context, Intent intent) { 61 // * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}: 62 // * integer field containing the current battery level, from 0 to 63 // * {@link #EXTRA_SCALE}. 64 int batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); 65 String action = intent.getAction(); 66 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 67 // 监听电量改变 68 Log.d(TAG, "收到了电量广播 -- action is " + action); 69 Log.d(TAG, "当前电量:" + batteryLevel); 70 if (mTextBatteryLevel != null) { 71 mTextBatteryLevel.setText(Integer.toString(batteryLevel)); 72 } 73 // 通过该方法来将电量转换成百分比 74 int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0); 75 float percent = batteryLevel * 1.0f / maxLevel * 100; 76 Log.d(TAG, "当前电量百分比量:" + percent + "%"); 77 } 78 else if(Intent.ACTION_POWER_CONNECTED.equals(action)){ 79 // 监听充电状态 80 Log.d(TAG, "充电器已经连接上"); 81 } 82 else if(Intent.ACTION_POWER_DISCONNECTED.equals(action)){ 83 // 监听充电线拔出状态 84 Log.d(TAG, "充电器已经断开"); 85 } 86 87 } 88 } 89 90 @Override 91 protected void onDestroy() { 92 super.onDestroy(); 93 // 动态广播注册时要取消广播注册,否则会导致内存泄露 94 if (mBatteryLevelReceiver != null) { 95 this.unregisterReceiver(mBatteryLevelReceiver); 96 } 97 } 98 }
2. activity_main:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/battery_level" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_centerInParent="true"/> 15 16 </RelativeLayout>
3. Manifest:添加权限
1 <uses-permission android:name="android.permission.BATTERY_STATS" 2 tools:ignore="ProtectedPermissions" />