Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 534|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-9-3 11:13:33 | 显示全部楼层 |阅读模式

    极力推荐Android 开发大总结文章:欢迎收藏
    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"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        ... ...
        <!-- add NTC Test Case-->
    	<string name="warningLowBatteryTemperature">"Battery temperature is too low, in order to protect battery life, charging function will be temporarily stopped."</string>
    	<string name="warningHightBatteryTemperature">"Battery temperature is too high, in order to protect battery life, charging function will be temporarily stopped."</string>
    	<string name="shutdownBatteryTemperatureMsg">"Battery temperature is too high. For safety reasons, the phone will shut down automatically later."</string>
    	<string name="batteryTemperatureWarning">Warning</string>
    	<string name="batteryTemperatureOk">"OK"</string>
    	<!-- add NTC Test Case-->	
        ... ...
    </resources>
    
    2.添加自定义电池异常温度定义

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

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        ... ...
        <!-- Shutdown if the battery temperature exceeds (this value * 0.1) Celsius. -->
        <integer name="config_shutdownBatteryTemperature">680</integer>
        <!-- add NTC Test Case-->
    	<integer name="config_warningHightBatteryTemperature">550</integer>
    	<integer name="config_warningLowBatteryTemperature">0</integer>
    	<!-- add NTC Test Case-->
        ... ...
    </resources>
    
    3. 在symbols 文件中添加对应java-symbol方便Framework代码引用

    symbols文件中为所有stringint值注册,方便Framework层代码的引用。
    alps/frameworks/base/core/res/res/values/symbols.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <!-- Private symbols that we need to reference from framework code.  See
           frameworks/base/core/res/MakeJavaSymbols.sed for how to easily generate
           this.
    
           Can be referenced in java code as: com.android.internal.R.<type>.<name>
           and in layout xml as: "@*android:<type>/<name>"
      -->
    ... ... 
      <java-symbol type="integer" name="config_shutdownBatteryTemperature" />
      <!-- add NTC Test Case-->
      <java-symbol type="integer" name="config_warningHightBatteryTemperature" />
      <java-symbol type="integer" name="config_warningLowBatteryTemperature" />
      <java-symbol type="string" name="warningLowBatteryTemperature" />
      <java-symbol type="string" name="warningHightBatteryTemperature" />
      <java-symbol type="string" name="shutdownBatteryTemperatureMsg" />
      <java-symbol type="string" name="batteryTemperatureWarning" />
      <java-symbol type="string" name="batteryTemperatureOk" />
      <!-- add NTC Test Case--> 
    

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

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

    实现方法如下:

    //add NTC Test Case
    import android.app.AlertDialog;
    import android.view.WindowManager;
    import android.content.DialogInterface;
    //add NTC Test Case
    
    ... ... 
    /**
     * <p>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.</p>
     * <p>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:</p>
     * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
     * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
     * <p>&quot;status&quot; - String, the current charging status.<br />
     * <p>&quot;health&quot; - String, the current battery health.<br />
     * <p>&quot;present&quot; - boolean, true if the battery is present<br />
     * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
     * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
     * into an AC power adapter; 2 if plugged in via USB.</p>
     * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
     * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
     * a degree Centigrade</p>
     * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
     *
     * <p>
     * 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.
     * </p>
     */
    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
           ... ...
    }
    

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

    微信关注公众号:  程序员Android,领福利

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-29 16:33 , Processed in 0.072214 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表