【Android开发学习32】BroadcastReceiver之电源管理

直接上代码:

/* ========================== Battery Start =============================*/
	
	int tBatteryLevel = 0, tBatteryCurLevel = 0;
	int tBatteryChargeFlag = -1;
	
	private void Battery(){
		int tTempFlag = 0;
		
		ImageButton tTempButton = (ImageButton)findViewById(R.id.top_battery);
		tTempButton.setVisibility(View.INVISIBLE);
		
		IntentFilter   mIntentFilter = new IntentFilter();  
		mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);    
		
		
		// 声明消息处理过程
		BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				String action = intent.getAction();
				if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
					//电池电量,数字 
					int tTempLevel = intent.getIntExtra("level", 0);
					//电池最大容量
					int tTempScale = intent.getIntExtra("scale", 100);
					//电池状态,返回是一个数字     
					// BatteryManager.BATTERY_STATUS_CHARGING 表示是充电状态     
					// BatteryManager.BATTERY_STATUS_DISCHARGING 放电中     
					// BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充电     
					// BatteryManager.BATTERY_STATUS_FULL 电池满
					int tTempStatus = intent.getIntExtra("status", 0);
					
					tTempLevel = (tTempLevel * 100) / tTempScale;
					if (tTempLevel > 95)
						tBatteryLevel = 5;
					else if(tTempLevel > 80)
						tBatteryLevel = 4;
					else if(tTempLevel > 60)
						tBatteryLevel = 3;
					else if(tTempLevel > 40)
						tBatteryLevel = 2;
					else if(tTempLevel > 20)
						tBatteryLevel = 1;
					else
						tBatteryLevel = 0;
					
					if (tTempStatus == BatteryManager.BATTERY_STATUS_CHARGING)
						tBatteryChargeFlag = 1;
					else
						tBatteryChargeFlag = 0;
				}
			}
			
		};

		registerReceiver(mIntentReceiver, mIntentFilter); 
	}
/* ========================== Battery End   =============================*/


 

 

另外还有一份别人的参考代码:

检测到现在在电源状态:

IntentFilter   mIntentFilter = new IntentFilter();

mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);  
registerReceiver(mIntentReceiver, mIntentFilter);

 //声明消息处理过程  
   private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {  
       @Override  
       public void onReceive(Context context, Intent intent) {  
           String action = intent.getAction();  
           //要看看是不是我们要处理的消息  
           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {             
               //电池电量,数字  
               Log.d("Battery", "" + intent.getIntExtra("level", 0));                 
               //电池最大容量  
               Log.d("Battery", "" + intent.getIntExtra("scale", 0));                 
               //电池伏数  
               Log.d("Battery", "" + intent.getIntExtra("voltage", 0));                 
               //电池温度  
               Log.d("Battery", "" + intent.getIntExtra("temperature", 0));  
                 
               //电池状态,返回是一个数字  
               // BatteryManager.BATTERY_STATUS_CHARGING 表示是充电状态  
               // BatteryManager.BATTERY_STATUS_DISCHARGING 放电中  
               // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充电  
               // BatteryManager.BATTERY_STATUS_FULL 电池满  
               Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));  
                
               //充电类型 BatteryManager.BATTERY_PLUGGED_AC 表示是充电器,不是这个值,表示是 USB  
               Log.d("Battery", "" + intent.getIntExtra("plugged", 0));  
                
               //电池健康情况,返回也是一个数字  
               //BatteryManager.BATTERY_HEALTH_GOOD 良好  
               //BatteryManager.BATTERY_HEALTH_OVERHEAT 过热  
               //BatteryManager.BATTERY_HEALTH_DEAD 没电  
               //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 过电压  
               //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知错误  
               Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));  
           }  
       }  
   };


 

 

 

本文博客源地址:http://blog.csdn.net/ypist

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值