以一个显示手机电量和电池状态的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 intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver);
}
private BroadcastReceiver mBroadcastReceiver=new BroadcastReceiver() {
int health,level,status;
@Override
public void onReceive(Context context, Intent intent) {
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");
}
}
}