Android 12.0 电池温度 < 0℃ 或 电池温度 > 60℃ 时弹出提示语,电池温度 < -25℃ 或 电池温度 > 65℃ 时弹出提示语并关机

Android 12.0 电池温度 < 0℃ 或 电池温度 > 60℃ 时弹出提示语,电池温度 < -25℃ 或 电池温度 > 65℃ 时弹出提示语并关机
最近做到项目需求需要给设备电池增加高低温提醒,具体修改点如下:

在/vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml中增加:

    <string name="title_battery_over_temperature_shutdown">"Over Battery Temperature"</string>
    <string name="title_charger_over_temperature">"Over Battery Temperature"</string>
    <string name="title_charger_low_temperature">"Low Battery Temperature"</string>
    <string name="title_battery_low_temperature_shutdown">"Low Battery Temperature"</string>
    <string name="title_battery_low_level">"Low Battery Level"</string>
    <string name="title_battery_over_temperature">"Over Battery Temperature"</string>
    <string name="title_battery_low_temperature">"Low Battery Temperature"</string>

    <string name="msg_battery_over_temperature_shutdown">"Your battery is over temperature.\nYour phone will shut down after 30 \nseconds."</string>
    <string name="msg_charger_over_temperature">"Your battery is over temperature.\nCharging has been suspended."</string>
    <string name="msg_charger_low_temperature">"Your battery temperature is too low.\nCharging has been suspended."</string>
    <string name="msg_battery_low_temperature_shutdown">"Your battery temperature is too low.\nYour phone will shut down after 30 \nseconds."</string>
    <string name="msg_battery_low_level">"Your battery is low in power and will shut down after 30 seconds."</string>
    <string name="msg_battery_over_temperature">"Your battery is over temperature."</string>
    <string name="msg_battery_low_temperature">"Your battery is low temperature"</string>

在/vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/下新建布局文件:
battery_warning.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="4dp"
        android:gravity="center_horizontal">

    <!-- Message to show to use. -->
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:paddingTop="4dp" android:paddingBottom="4dp" />
    <LinearLayout android:id="@+id/inner_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="4dp" android:paddingBottom="4dp">
    </LinearLayout>
    <!-- Alert dialog style buttons along the bottom. -->
    <LinearLayout style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:measureWithLargestChild="true"
        android:orientation="vertical">
        <Button style="?android:attr/buttonBarButtonStyle" android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"/>
    </LinearLayout>
</LinearLayout>

custom_title_1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>
</LinearLayout>

在/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/power下新建java文件:
BatteryTemperatureActivity.java:

package com.android.systemui.power;

import com.android.systemui.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.TextView;


public class BatteryTemperatureActivity extends Activity {
    private static final String TAG = "BatteryTemperatureActivity";
    private static final String SHARED_PREFERENCES_NAME = "battery_warning_settings";
    protected static final String KEY_TYPE = "type";
    private Handler mHandler;
    private Ringtone mRingtone;
    private int mType;
    private int mPlugType;
    private int mBatteryLevel;

    private static final int BATTERY_OVER_TEMPERATURE_SHUTDOWN_TYPE = 0;
    private static final int CHARGER_OVER_TEMPERATURE_TYPE = 1;
    private static final int CHARGER_LOW_TEMPERATURE_TYPE = 2;
    private static final int BATTERY_LOW_TEMPERATURE_SHUTDOWN_TYPE = 3;
    private static final int BATTERY_LOW_LEVEL = 4;
    private static final int BATTERY_OVER_TEMPERATURE_TYPE = 5;
    private static final int BATTERY_LOW_TEMPERATURE_TYPE = 6;

    static final int[] shutdownTitle = new int[] {
            R.string.title_battery_over_temperature_shutdown,
            R.string.title_charger_over_temperature,
            R.string.title_charger_low_temperature,
            R.string.title_battery_low_temperature_shutdown,
            R.string.title_battery_low_level,
            R.string.title_battery_over_temperature,
            R.string.title_battery_low_temperature,};
    private static final int[] shutdownMsg = new int[] {
            R.string.msg_battery_over_temperature_shutdown,
            R.string.msg_charger_over_temperature,
            R.string.msg_charger_low_temperature,
            R.string.msg_battery_low_temperature_shutdown,
            R.string.msg_battery_low_level,
            R.string.msg_battery_over_temperature,
            R.string.msg_battery_low_temperature,};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setFinishOnTouchOutside(false);
        setContentView(R.layout.battery_warning);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.custom_title_1);

        mHandler = new Handler();

        Intent intent = getIntent();
        mType = intent.getIntExtra("TemperatueType", -1);
        mPlugType = intent.getIntExtra("PlugType", 0);
        mBatteryLevel = intent.getIntExtra("BatteryLevel",-1);
        Log.d("nowtemp", "mType is " + mType);
        Log.d("nowtemp", "PlugType is " + mPlugType);
        Log.d("nowtemp", "BatteryLevel is " + mBatteryLevel);
        if (mType == BATTERY_LOW_TEMPERATURE_SHUTDOWN_TYPE
                    || mType == BATTERY_OVER_TEMPERATURE_SHUTDOWN_TYPE) {
            showWarningDialog(mType);
            //shutdown
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
                    intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Log.d("nowtemp", "shutdown is ok");
                    BatteryTemperatureActivity.this.startActivityAsUser(intent, UserHandle.CURRENT);
                }
            },30000);
            ///
        } else if (mType == CHARGER_LOW_TEMPERATURE_TYPE || mType == CHARGER_OVER_TEMPERATURE_TYPE) {
            if(mPlugType != 0) {
                showWarningDialog(mType);
            } else {
                finish();
            }
        } else if (mType == BATTERY_OVER_TEMPERATURE_TYPE || mType == BATTERY_LOW_TEMPERATURE_TYPE) {
            if(mPlugType == 0) {
                showWarningDialog(mType);
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "receive ACTION_BATTERY_CHANGED broadcast, finish");
    }

    private void showWarningDialog(int type) {
        TextView textView = (TextView) findViewById(R.id.left_text);
        TextView textView2 = (TextView) findViewById(R.id.text);

        textView.setTextSize(20);
        textView2.setTextSize(16);
        textView.setText(getString(shutdownTitle[mType]));
        textView2.setText(getString(shutdownMsg[mType]));

        LinearLayout layout = (LinearLayout)findViewById(R.id.inner_content);

        Button button = (Button)findViewById(R.id.add);
        button.setText("OK");
        button.setOnClickListener(mDismissContentListener);


    }
    private OnClickListener mDismissContentListener = new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    };



}

在/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/power/PowerUI.java的

else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {

中增加

                int type = -1;
                int nowTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
                int mPlugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
                int tBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                Slog.d("nowtemp", "powerUI nowtemp is "+nowTemp);
                Slog.d("nowtemp", "powerUI BatteryLevel is "+tBatteryLevel);
                if((mPlugType ==0) && (nowTemp >= 600 || nowTemp < -100)){
                    if (nowTemp >= 650) {
                        type = 0;
                    } else if (nowTemp >= 600) {
                        type = 5;
                    } else if (-250 < nowTemp && nowTemp < -99) {
                        type = 6;
                    } else if (nowTemp <= -250) {
                        type = 3;
                    }
                    Intent tempIntent = new Intent();
                    tempIntent.setClass(mContext, BatteryTemperatureActivity.class);
                    tempIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    tempIntent.putExtra("TemperatueType", type);
                    tempIntent.putExtra("PlugType", mPlugType);
                    tempIntent.putExtra("BatteryLevel", tBatteryLevel);
                    mContext.startActivity(tempIntent);
                } else if ((mPlugType != 0) && (nowTemp >= 550 || nowTemp <= 0)) {
                    if (nowTemp >= 600) {
                        type = 1;
                    } else if (nowTemp <= 0) {
                        type = 2;
                    }
                    Intent tempIntent = new Intent();
                    tempIntent.setClass(mContext, BatteryTemperatureActivity.class);
                    tempIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    tempIntent.putExtra("TemperatueType", type);
                    tempIntent.putExtra("PlugType", mPlugType);
                    tempIntent.putExtra("BatteryLevel", tBatteryLevel);
                    mContext.startActivity(tempIntent);
                }

在/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml中增加:

        <activity android:name=".power.BatteryTemperatureActivity"
            android:process=":nowtemp"
            android:excludeFromRecents="true"
            android:launchMode="singleTop"
            android:theme="@*android:style/Theme.Material.Light.Dialog.Alert">
        </activity>

重新编译验证,修改生效,设备电池已增加高低温提醒

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jeffries_C

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

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

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

打赏作者

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

抵扣说明:

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

余额充值