Android电池状态监听

如果要监听电池的状态改变,需要动态注册:android.intent.action.BATTERY_CHANGED,收到Action后可以根据对应的Key获取你需要的信息,更详细信息可以参考以下例子中的BatteryChangedReceiver类

package com.example.charginganimation;

import android.app.Activity;
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.view.View;

public class MainActivity extends Activity {
	private CustomClipLoading ccl;
	private BatteryChangedReceiver receiver = new BatteryChangedReceiver();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		registerReceiver(receiver, getFilter());
	}

	private IntentFilter getFilter() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
		filter.addAction(Intent.ACTION_BATTERY_LOW);
		filter.addAction(Intent.ACTION_BATTERY_OKAY);
		return filter;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	class BatteryChangedReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			final String action = intent.getAction();
			if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
				System.out
						.println("BatteryChangedReceiver BATTERY_CHANGED_ACTION---");
				// 当前电池的电压
				int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
						-1);
				// 电池的健康状态
				int health = intent
						.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
				switch (health) {
				case BatteryManager.BATTERY_HEALTH_COLD:
					System.out.println("BATTERY_HEALTH_COLD");
					break;
				case BatteryManager.BATTERY_HEALTH_DEAD:
					System.out.println("BATTERY_HEALTH_DEAD ");
					break;
				case BatteryManager.BATTERY_HEALTH_GOOD:
					System.out.println("BATTERY_HEALTH_GOOD");
					break;
				case BatteryManager.BATTERY_HEALTH_OVERHEAT:
					System.out.println("BATTERY_HEALTH_OVERHEAT");
					break;
				case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
					System.out.println("BATTERY_HEALTH_COLD");
					break;
				case BatteryManager.BATTERY_HEALTH_UNKNOWN:
					System.out.println("BATTERY_HEALTH_UNKNOWN");
					break;
				case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
					System.out.println("BATTERY_HEALTH_UNSPECIFIED_FAILURE");
					break;
				default:
					break;
				}
				// 电池当前的电量, 它介于0和 EXTRA_SCALE之间
				int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
				// 电池电量的最大值
				int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
				// 当前手机使用的是哪里的电源
				int pluged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
						-1);
				switch (pluged) {
				case BatteryManager.BATTERY_PLUGGED_AC:
					// 电源是AC charger.[应该是指充电器]
					System.out.println("BATTERY_PLUGGED_AC");
					break;
				case BatteryManager.BATTERY_PLUGGED_USB:
					// 电源是USB port
					System.out.println("BATTERY_PLUGGED_USB ");
					break;
				default:
					break;
				}
				int status = intent
						.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
				switch (status) {
				case BatteryManager.BATTERY_STATUS_CHARGING:
					// 正在充电
					System.out.println("BATTERY_STATUS_CHARGING ");
					break;
				case BatteryManager.BATTERY_STATUS_DISCHARGING:
					System.out.println("BATTERY_STATUS_DISCHARGING  ");
					break;
				case BatteryManager.BATTERY_STATUS_FULL:
					// 充满
					System.out.println("BATTERY_STATUS_FULL ");
					break;
				case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
					// 没有充电
					System.out.println("BATTERY_STATUS_NOT_CHARGING ");
					break;
				case BatteryManager.BATTERY_STATUS_UNKNOWN:
					// 未知状态
					System.out.println("BATTERY_STATUS_UNKNOWN ");
					break;
				default:
					break;
				}
				// 电池使用的技术。比如,对于锂电池是Li-ion
				String technology = intent
						.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
				// 当前电池的温度
				int temperature = intent.getIntExtra(
						BatteryManager.EXTRA_TEMPERATURE, -1);
				System.out.println("voltage = " + voltage + " technology = "
						+ technology + " temperature = " + temperature
						+ " level = " + level + " scale = " + scale);
			} else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)) {
				// 表示当前电池电量低
				System.out
						.println("BatteryChangedReceiver ACTION_BATTERY_LOW---");
			} else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {
				// 表示当前电池已经从电量低恢复为正常
				System.out
						.println("BatteryChangedReceiver ACTION_BATTERY_OKAY---");
			}
		}

	}
}
时间: 2024-10-10 08:43:46

Android电池状态监听的相关文章

Android USB大容量存储时SD卡状态监听(转)

对SD卡状态监听,到现在为止我知道的有两种方式: 1.注册StorageEventListener来监听sd卡状态 StorageEventListener中有onStorageStateChanged()方法,当sd卡状态改变时,此方法会调用,对各状态的判断一般会用到Environment类,此类中包含的有关sd卡状态的常量有: MEDIA_BAD_REMOVAL:表明SDCard 被卸载前己被移除 MEDIA_CHECKING:表明对象正在磁盘检查 MEDIA_MOUNTED:表明sd对象是

iOS电池的监听

#import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController () //@property(nonatomic,readonly) float     batteryLevel NS_AVAILABLE_IOS(3_0); // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown它返回的是0.00-1.00之间的浮点值. @end @imple

网络状态监听

网络状态监听 定义一个广播接收器和状态变化接口 publicclassNetBroadcastReceiverextendsBroadcastReceiver{ publicstaticArrayList<NetEventHandler> mListeners =newArrayList<NetEventHandler>(); privatestaticString NET_CHANGE_ACTION ="android.net.conn.CONNECTIVITY_CHA

ThinkAndroid框架 网络状态监听原理

ThinkAndroid框架 网络状态监听原理,有需要的朋友可以参考下. 现在用到网络监听的地方越来越广泛,这里提供下ThinkAndroid里的实现方法,还是根据功能原理倒着给出代码: 1.网络状态会发出广播,监听网络状态肯定通过广播接收者实现,广播接受者: /** * @Title NetworkStateReceiver * @Package com.ta.util.netstate * @Description 是一个检测网络状态改变的,需要配置 <receiver * android:

android的电话监听

android的电话监听 新建一个项目,结构图如下: PhoneService: package com.demo.tingdianhua; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.telecom.TelecomManager; import android.telephony.PhoneStateListener; import an

quick-cocos2d-x android返回键监听并实现原生退出对话框

这两天终于闲了一下,就顺手又把quick捡起来又学了学,一直都觉得quick比cocos2dx那套lua绑定要方便许多,今天试了下android返回键的监听,还是挺好弄的,所以就有了这篇. 首先说明一下使用的quick版本--2.2.5. 直接上代码 function MainScene:addBackEvent() if device.platform == "android" then self.touchLayer = display.newLayer() self.touchL

Android短信监听(三)——利用Loader实现短信监听

MainActivity如下: package cc.c; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; import android.content.Loader; import android.database.Curs

Android短信监听(二)——利用ContentObserver实现短信监听

MainActivity如下: package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.app.Activity; /** *

Android短信监听(一)——利用BroadcastReceiver实现短信监听

MainActivity如下: package cc.testsmsbroadcastreceiver; import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用BroadcastReceiver实现