Android 开发-电池(Battery)电量等数据的监听,用Service,Broadcaset,以及Thread的方式实现

案例介绍:

实现一个电池属性实时监听器。这些熟悉包括温度,电池电量....等等。而且,使用Service,Broadcast,以及Thread的方式来实现。

实现原理:

1.在Service中开启线程;

2.在Service中,监听

ACTION_BATTERY_CHANGED

实现一个BroadcastReceiver的

onReceive(Context ctx,Intent intent)
中,就包含了电池属性相关的值。

3.在线程中,将intent的值写入到文件。

4.在ui中,可以读取文件,进行显示。

核心代码:

MyBatteryService.java


public class MyBatteryService extends Service {

	private final static String TAG = "BatteryService";
	
	public static final String LOGFILEPATH = "BATTERYSERVICE/battery.csv";
	
	private final static String[] batteryExtraKeys = {"level", "scale", "voltage", "temperature", "plugged", "status", "health", "present", "technology", "icon-small"};

	private File mBatteryLogFile;
	private int mCount;
	private Intent mLastBatteryIntent;
    private boolean mQuitThread;
    private boolean mThreadRunning = false;

    @Override
    public void onCreate() {
    	super.onCreate();
		if (!mThreadRunning) {
			mCount = 0;
			mLastBatteryIntent = null;
			mQuitThread = false;
	        Thread thr = new Thread(null, mTaskRunnable, "BatteryService");
	        thr.start();
			registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	        Toast.makeText(this, "Battery Service started", Toast.LENGTH_SHORT).show();
		}
    }
	
	@Override
	public void onDestroy() {
    	Log.i(TAG, "onDestroy");
        mQuitThread = true;
        notifyService();
        
    	super.onDestroy();
    	unregisterReceiver(mBatInfoReceiver);
        Toast.makeText(this, "Battery Service stopped", Toast.LENGTH_SHORT).show();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context ctx, Intent intent) {
			try {
            	mCount += 1;
				mLastBatteryIntent = (Intent) intent.clone();
				notifyService();
			}
			catch (Exception e) {
				Log.e(TAG,e.getMessage(), e);
			}
		}

	};

	private void logBattery(Intent batteryChangeIntent) {
		if (batteryChangeIntent == null)
			return;
		try {
			FileWriter out = null;
			if (mBatteryLogFile != null) {
				try {
					out = new FileWriter(mBatteryLogFile, true);
				}
				catch (Exception e) {}
			}
			if (out == null) {
				File root = FileUtils.getLogFilePath();//  Environment.getExternalStorageDirectory();
				if (root == null)
					throw new Exception("external storage dir not found");
				mBatteryLogFile = new File(root, MyBatteryService.LOGFILEPATH);
				boolean fileExists = mBatteryLogFile.exists();
				if (!fileExists) {
					mBatteryLogFile.getParentFile().mkdirs();
					mBatteryLogFile.createNewFile();
				}
				if (!mBatteryLogFile.exists()) 
					throw new Exception("creation of file '"+mBatteryLogFile.toString()+"' failed");
				if (!mBatteryLogFile.canWrite()) 
					throw new Exception("file '"+mBatteryLogFile.toString()+"' is not writable");
				out = new FileWriter(mBatteryLogFile, true);
				if (!fileExists) {
					String header = createHeadLine();
					out.write(header);
					out.write("\n");
				}
			}
			if (mLastBatteryIntent != null) {
				String extras = createBatteryInfoLine(mLastBatteryIntent);
				out.write(extras);
				out.write("\n");
			}
			out.flush();
			out.close();
		} catch (Exception e) {
			Log.e(TAG,e.getMessage(),e);
		}
	}

    private String createHeadLine() {
    	StringBuffer result = new StringBuffer();
    	result.append("Nr;TimeMillis");
    	for (String key : batteryExtraKeys)
			result.append(";").append(key);
		return result.toString();
	}

	private String createBatteryInfoLine(Intent batteryIntent) {
    	StringBuffer result = new StringBuffer();
    	result.append(Integer.toString(mCount)).append(";").append(Long.toString(System.currentTimeMillis()));
    	Bundle extras = batteryIntent.getExtras();
    	for (String key : batteryExtraKeys)
			result.append(";").append(extras.get(key));
		return result.toString();
	}

    Runnable mTaskRunnable = new Runnable() {
		public void run() {
            mThreadRunning = true;
            Log.i(TAG,"STARTING BATTERYSERVICE TASK");
            while (!mQuitThread) {
				Log.i(TAG,"STARTING BATTERYSERVICE TASK begin logfile");
				logBattery(mLastBatteryIntent);
                synchronized (MyBatteryService.this) {
                	try {
                    	MyBatteryService.this.wait();
                	} catch (Exception ignore) {}
                }
            }
            mThreadRunning = false;
			logBattery(mLastBatteryIntent);
            Log.i(TAG,"LEAVING BATTERYSERVICE TASK");
        }
    };
	
	public void notifyService() {
		synchronized (MyBatteryService.this) {
			MyBatteryService.this.notifyAll();
		}
	}
}

说明:

1. 定义MyBatteryService类,用于在后台进行电量属性的获取;

1. 在MyBatteryService中,注册BroadcastReceiver:

registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

2. mBatInfoReceiver是一个BroadcastReceiver, mLastBatteryIntent保存了接收到的数据;

3. mTaskRunnable是工作线程,通过logBattery(mLastBatteryIntent);将接收到的数据写入文件;

写入的文件内容,大概如下:

Nr;TimeMillis;level;scale;voltage;temperature;plugged;status;health;present;technology;icon-small

1;1650438867518;72;100;3983;364;2;2;2;true;Li-ion;17303668

2;1650438876180;72;100;4003;363;2;2;2;true;Li-ion;17303668

3;1650438886226;72;100;4000;364;2;2;2;true;Li-ion;17303668

4;1650438896285;72;100;4011;365;2;2;2;true;Li-ion;17303668

5;1650438906325;72;100;3996;365;2;2;2;true;Li-ion;17303668

6;1650438916364;72;100;4028;364;2;2;2;true;Li-ion;17303668

7;1650438926414;72;100;4018;365;2;2;2;true;Li-ion;17303668

8;1650438936468;72;100;4026;365;2;2;2;true;Li-ion;17303668

9;1650438946513;72;100;4006;365;2;2;2;true;Li-ion;17303668

10;1650438956576;72;100;4018;365;2;2;2;true;Li-ion;17303668

另外,也可以通过系统提供的BatteryManager来实现类似的功能:

BatteryManager batteryManager = (BatteryManager)context.getSystemService(context.BATTERY_SERVICE);

int level = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

项目传送门:

Android-电池管理-实时获取电池电量,温度...等属性。-Android文档类资源-CSDN下载


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以通过注册广播接收器来监听电池电量和插拔状态的变化。具体实现如下: 1. 在 AndroidManifest.xml 文件中添加如下权限: ```xml <uses-permission android:name="android.permission.BATTERY_STATS" /> ``` 2. 创建一个广播接收器 BatteryReceiver,实现电量和插拔状态的监听: ```java public class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 获取当前电量 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); // 获取电量最大值 int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); // 计算电量百分比 int batteryPercent = level * 100 / scale; // 获取充电状态 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; // 获取充电方式 int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean isUSBCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean isACCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; // 处理逻辑 if (isCharging) { if (isUSBCharge) { // USB 充电 } else if (isACCharge) { // 交流电充电 } else { // 无线充电 } } else { // 电池供电 } } } ``` 3. 在需要监听电量和插拔状态的页面或服务中注册广播接收器: ```java BatteryReceiver batteryReceiver = new BatteryReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryReceiver, filter); ``` 注意:注册广播接收器后一定要记得在对应的生命周期(如 Activity 的 onDestroy() 方法)中取消注册,避免内存泄漏: ```java unregisterReceiver(batteryReceiver); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值