+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
电池充电的广播接收,只能通过动态注册,不能通过静态注册:
public class MainActivity extends AppCompatActivity {
private BatteryReceiver batteryReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryReceiver = new BatteryReceiver();
}
@Override
protected void onResume() {
super.onResume();
//1.创建IntentFilter,对应<intent-filter><action/>
//直接指定
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//2.动态注册Context对象registerReceiver
//!!!哪个Context注册,那么,这个Receiver作用范围
registerReceiver(batteryReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
//onPause 里面一定要取消注册:不然在退出的时候会报错:
unregisterReceiver(batteryReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
batteryReceiver=null;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.kodulf.broadcastrecevierdemo.receivers;
/**
* Created by Administrator on 15-12-15.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
/**
* 动态注册的案例,
* 使用电池电量检测;
* !!!注意,电池电量只能够使用动态注册,如果使用静态的就会把系统弄的太慢了。
*/
public class BatteryRecevier extends BroadcastReceiver {
private String TAG="BatteryRecevier";
@Override
public void onReceive(Context context, Intent intent) {
//1,定义action 内容,也就是电池变化的ACTION
String action = intent.getAction();
//电池电量变化,是Android 基本的状态,所以定义在Intent 中
//包含一个ACTION
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
//2.获取电量相关信息
//标准的广播信息,都存储在Intent 参数中,
//通过BatteryManager来获取;内部包含了常量定义
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
int state = intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
Log.d("TAG","level "+level);
Log.d("TAG","scale "+scale);
Log.d("TAG","health "+health);
Log.d("TAG","state "+state);
Log.d("TAG","plugged "+plugged);
//检测是否街上电源了
switch(plugged){
case BatteryManager.BATTERY_PLUGGED_AC:
//接上电源插座,交流电
break;
case BatteryManager.BATTERY_PLUGGED_USB:
//接上电脑USB充电
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
//无线充电
break;
}
//----------------------------------
//检测status
switch(state){
case BatteryManager.BATTERY_STATUS_CHARGING:
//充电中
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
//耗电
break;
case BatteryManager.BATTERY_STATUS_FULL:
//充满电
break;
}
}
}
}
电池充电的广播接收,只能通过动态注册,不能通过静态注册:
public class MainActivity extends AppCompatActivity {
private BatteryReceiver batteryReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryReceiver = new BatteryReceiver();
}
@Override
protected void onResume() {
super.onResume();
//1.创建IntentFilter,对应<intent-filter><action/>
//直接指定
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//2.动态注册Context对象registerReceiver
//!!!哪个Context注册,那么,这个Receiver作用范围
registerReceiver(batteryReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
//onPause 里面一定要取消注册:不然在退出的时候会报错:
unregisterReceiver(batteryReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
batteryReceiver=null;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.kodulf.broadcastrecevierdemo.receivers;
/**
* Created by Administrator on 15-12-15.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
/**
* 动态注册的案例,
* 使用电池电量检测;
* !!!注意,电池电量只能够使用动态注册,如果使用静态的就会把系统弄的太慢了。
*/
public class BatteryRecevier extends BroadcastReceiver {
private String TAG="BatteryRecevier";
@Override
public void onReceive(Context context, Intent intent) {
//1,定义action 内容,也就是电池变化的ACTION
String action = intent.getAction();
//电池电量变化,是Android 基本的状态,所以定义在Intent 中
//包含一个ACTION
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
//2.获取电量相关信息
//标准的广播信息,都存储在Intent 参数中,
//通过BatteryManager来获取;内部包含了常量定义
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
int state = intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
Log.d("TAG","level "+level);
Log.d("TAG","scale "+scale);
Log.d("TAG","health "+health);
Log.d("TAG","state "+state);
Log.d("TAG","plugged "+plugged);
//检测是否街上电源了
switch(plugged){
case BatteryManager.BATTERY_PLUGGED_AC:
//接上电源插座,交流电
break;
case BatteryManager.BATTERY_PLUGGED_USB:
//接上电脑USB充电
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
//无线充电
break;
}
//----------------------------------
//检测status
switch(state){
case BatteryManager.BATTERY_STATUS_CHARGING:
//充电中
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
//耗电
break;
case BatteryManager.BATTERY_STATUS_FULL:
//充满电
break;
}
}
}
}