Android的广播机制基础1---动态注册监听的使用,以获得电池的使用状态为例

2019-07-14 03:35发布

以一个显示手机电量和电池状态的Demo为例。
1.要获得电池的使用状态,需在AndroidManifest.xml中添加使用权限: <uses-permission android:name="android.permission.BATTERY_STATS"/> 2.广播有发送方和接收方,接收方为BroadCastReceiver的实例或其子类
3.首先我们写出activity的布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> //电池电量 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/current" android:textSize="30sp"/> <ProgressBar android:layout_width="match_parent" android:layout_height="20dp" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:id="@+id/progressbar"/> //电池充电状态 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/battryeStatus"/> //电池健康状态 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/batteryHealth" android:textSize="30sp"/> LinearLayout> 4.activity实现部分 package com.example.apple.broadcast.BatteryMonitor; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.ProgressBar; import android.widget.TextView; import com.example.apple.broadcast.R; /** * Created by apple on 2018/6/6. */ public class BatteryMonitorActivity extends AppCompatActivity { private TextView tv_level,tv_status,tv_health; private ProgressBar progressbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.battry_layout); //初始化控件 tv_level=(TextView)findViewById(R.id.current); tv_health=(TextView)findViewById(R.id.batteryHealth); tv_status=(TextView)findViewById(R.id.battryeStatus); progressbar=(ProgressBar)findViewById(R.id.progressbar); progressbar.setMax(100); } //注册广播 @Override protected void onResume() { super.onResume(); //IntentFilter传递相应的action IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); //注册传入参数为BroadcastReceiver和Intentfilter的实例 registerReceiver(mBroadcastReceiver,intentFilter); } //取消广播 @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mBroadcastReceiver); } //BroadcastReceiver的实例,也可以用BroadcastReceiver的子类实现,书写一个内部类,要重写父类的onReceive方法 private BroadcastReceiver mBroadcastReceiver=new BroadcastReceiver() { //Int 类型值用来判断电池的状态 int health,level,status; @Override public void onReceive(Context context, Intent intent) { //获取广播的action String action=intent.getAction(); //如果电池电量改变 if(action==Intent.ACTION_BATTERY_CHANGED){ level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); //电池的充电状态,默认为未充电 status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,BatteryManager.BATTERY_STATUS_DISCHARGING); //获取电池的健康状态 health=intent.getIntExtra(BatteryManager.EXTRA_HEALTH,BatteryManager.BATTERY_HEALTH_GOOD); } changeBattry(health,level,status); } }; //改变布局,将状态变化显示在布局文件中 private void changeBattry(int health, int level, int status) { tv_level.setText("当前电量为:"+level+"%"); progressbar.setProgress(level); if(status==BatteryManager.BATTERY_STATUS_CHARGING){ tv_status.setText("充电状况:正在充电"); }else if(status==BatteryManager.BATTERY_STATUS_DISCHARGING){ tv_status.setText("充电状况:未充电"); } if(health==BatteryManager.BATTERY_HEALTH_GOOD){ tv_health.setText("电池状况:Good"); }else if(health==BatteryManager.BATTERY_HEALTH_UNKNOWN){ tv_health.setText("电池状况:unkown"); }else if(health==BatteryManager.BATTERY_HEALTH_DEAD){ tv_health.setText("电池状况:Dead"); } } } 这里写图片描述