android 异常_基于Android 9.0 电池温度异常提醒

1e248d87779f5cae1fac387e6e0f3da1.png

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、 Framework 层字符串添加

二、Service 中实时监测 电池异常温度并弹窗提醒用户

检测电池温度,提示用户温度异常,请注意

Android电池信息状态主要是在frameworks/base/services/core/java/com/android/server/BatteryService.java,本文也是基于此BatteryService实时监测 电池温度,及时提醒用户,为了安全起见,在电池温度异常时候,请勿继续充电。

一、 Framework 层字符串添加

1.添加弹窗字符串资源

alps/frameworks/base/core/res/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> ... ...  "Battery temperature is too low, in order to protect battery life, charging function will be temporarily stopped.""Battery temperature is too high, in order to protect battery life, charging function will be temporarily stopped.""Battery temperature is too high. For safety reasons, the phone will shut down automatically later."Warning"OK"  ... ...

2.添加自定义电池异常温度定义

自定义温度 主要修改alps/frameworks/base/core/res/res/values/config.xml

<?xml version="1.0" encoding="utf-8"?> ... ...  6805500 ... ...

3. 在symbols 文件中添加对应java-symbol方便Framework代码引用

在symbols文件中为所有string,int值注册,方便Framework层代码的引用。

alps/frameworks/base/core/res/res/values/symbols.xml

<?xml version="1.0" encoding="utf-8"?>... ...  

二、Service 中实时监测 电池异常温度并弹窗提醒用户

BatteryService后台服务可以实时监测 电池问题,当电池温度异常时候,我们要及时提醒用户,如果此时用户正在充电,提示请勿充电等。

实现方法如下:

//add NTC Test Caseimport android.app.AlertDialog;import android.view.WindowManager;import android.content.DialogInterface;//add NTC Test Case... ... /** * 

BatteryService monitors the charging status, and charge level of the device * battery. When these values change this service broadcasts the new values * to all {@link android.content.BroadcastReceiver IntentReceivers} that are * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED * BATTERY_CHANGED} action.

*

The new values are stored in the Intent data and can be retrieved by * calling {@link android.content.Intent#getExtra Intent.getExtra} with the * following keys:

*

"scale" - int, the maximum value for the charge level

*

"level" - int, charge level, from 0 through "scale" inclusive

*

"status" - String, the current charging status.
*

"health" - String, the current battery health.
*

"present" - boolean, true if the battery is present
*

"icon-small" - int, suggested small icon to use for this state

*

"plugged" - int, 0 if the device is not plugged in; 1 if plugged * into an AC power adapter; 2 if plugged in via USB.

*

"voltage" - int, current battery voltage in millivolts

*

"temperature" - int, current battery temperature in tenths of * a degree Centigrade

*

"technology" - String, the type of battery installed, e.g. "Li-ion"

* *

* The battery service may be called by the power manager while holding its locks so * we take care to post all outcalls into the activity manager to a handler. * * FIXME: Ideally the power manager would perform all of its calls into the battery * service asynchronously itself. *

*/public final class BatteryService extends SystemService { // add NTC Test Case 定义电池温度检测所需变量 private int mConfigWarningLowBatteryTemperature; private int mConfigWarningHightBatteryTemperature; private String mWarningLowBatteryTemperature; private String mWarningHightBatteryTemperature; private String mShutdownBatteryTemperatureMsg; private AlertDialog mbatteryTemperatureDialog = null; //add NTC Test Case ... ... public BatteryService(Context context) { super(context); ... ... //add NTC Test Case BatteryService 构造获取电池检测所需资源 mConfigWarningLowBatteryTemperature= mContext.getResources().getInteger( com.android.internal.R.integer.config_warningLowBatteryTemperature); mConfigWarningHightBatteryTemperature= mContext.getResources().getInteger( com.android.internal.R.integer.config_warningHightBatteryTemperature); mWarningHightBatteryTemperature = mContext.getResources().getString( com.android.internal.R.string.warningHightBatteryTemperature); mShutdownBatteryTemperatureMsg = mContext.getResources().getString( com.android.internal.R.string.shutdownBatteryTemperatureMsg); mWarningLowBatteryTemperature = mContext.getResources().getString( com.android.internal.R.string.warningLowBatteryTemperature); //add NTC Test Case } private void processValuesLocked(boolean force) { ... ... shutdownIfNoPowerLocked(); shutdownIfOverTempLocked(); //add NTC Test Case //调用自定义电池温度异常检测方法 warningBatteryTemperature(); //add NTC Test Case ... ... } ... ... //add NTC Test Case 自定义判断电池温度是否异常 private void warningBatteryTemperature(){ if(mPlugType != BATTERY_PLUGGED_NONE && mHealthInfo!=null && mbatteryTemperatureDialog == null){ if (mHealthInfo.batteryTemperature >= mConfigWarningHightBatteryTemperature) { mHandler.post(new Runnable() { public void run() { if(mHealthInfo.batteryTemperature >= (mShutdownBatteryTemperature-10)){ batteryTemperatureDialog(mShutdownBatteryTemperatureMsg); }else{ batteryTemperatureDialog(mWarningHightBatteryTemperature); } } }); } if (mHealthInfo.batteryTemperature < mConfigWarningLowBatteryTemperature) { mHandler.post(new Runnable() { public void run() { batteryTemperatureDialog(mWarningLowBatteryTemperature); } }); } } } // 自定义电池温度Dialog弹窗 private void batteryTemperatureDialog(String batteryTemperature) { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle(mContext.getResources().getString( com.android.internal.R.string.batteryTemperatureWarning)); builder.setCancelable(false); builder.setMessage(batteryTemperature); builder.setIconAttribute(android.R.attr.alertDialogIcon); builder.setPositiveButton(com.android.internal.R.string.batteryTemperatureOk, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); mbatteryTemperatureDialog = null; } }); mbatteryTemperatureDialog = builder.create(); mbatteryTemperatureDialog.getWindow().setType( WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); if (mbatteryTemperatureDialog != null&& !mbatteryTemperatureDialog.isShowing()) { mbatteryTemperatureDialog.show(); } } //add NTC Test Case ... ...}

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值