电池高温下自动关机保护

1. 需求

  • 客制化项目:电池温度过高,主机将自动关机

  • 具体条件:电池温度 59 摄氏度以上,主机弹出“关机提醒”后自动关机

2. 开发说明

2.1 电池温度信息
  • Linux 的节点:/sys/devices/platform/battery/Battery_Temperature
  • 应用层:注册电池广播即可
2.1 具体思路

后台运行常驻服务,监听广播,当温度达到阈值情况下,弹出对话框提示,对话框消失后执行关机指令。

2.1.1 电池广播
    private class PowerUIReceiver extends BroadcastReceiver {

        public void init() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
            mContext.registerReceiver(this, filter);
        }
2.1.2 判断条件
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }

            String action = intent.getAction();

            int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            if (temperature >= PowerSaveManagerContant.HIGHT_BATTERY_TEMP_SHUTDOWN) {
                if (mISystemStatusCallBack != null && !isShutdownFlag) {
                    isShutdownFlag = true;
                    mISystemStatusCallBack.highTemperature(temperature);
                }
            }
2.1.3 对话框

android:theme=”@*android:style/Theme.Material.Light.Dialog.Alert” >

        <activity
            android:name="com.android.lava.powersave.ui.ShutdownActivity"
            android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc"
            android:exported="false"
            android:label="@string/high_temp_shutdown_title"
            android:theme="@*android:style/Theme.Material.Light.Dialog.Alert" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

UI

public class ShutdownActivity extends Activity {

    public static final int MSG_SHUTDOWN = 0;
    public static final int DELAY_TIME = 5000;

    private Ringtone mRingtone;
    private static final Uri WARNING_SOUND_URI = Uri.parse("file:///system/media/audio/ui/VideoRecord.ogg");

    private final Handler mHandler = new ShutdownHandler(this);

    private static class ShutdownHandler extends WeakHandler<ShutdownActivity> {
        public ShutdownHandler(ShutdownActivity owner) {
            super(owner);
        }

        @Override
        public void handleMessage(Message msg) {
            ShutdownActivity activity = getOwner();
            if (activity == null)
                return;

            if (msg.what == MSG_SHUTDOWN) {
                PowerSaveController.shutdown(activity);
                activity.finish();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_shutdown_confirm);

        mHandler.sendEmptyMessageDelayed(MSG_SHUTDOWN, DELAY_TIME);
        playAlertSound(WARNING_SOUND_URI);
    }
2.1.4 关机指令

注意需要权限和系统进程id

    /**
     * Shutdown 
     * 
     * 1. <uses-permission android:name="android.permission.SHUTDOWN" />
     * 2. android:sharedUserId="android.uid.system"
     */
    public static void shutdown(Context mContext) {
        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
2.1.5 温度模拟数据
adb shell "echo 50 > /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 53> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 55> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 57> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 58> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 59 > /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 60> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 62> /sys/devices/platform/battery/Battery_Temperature"
adb shell "echo 63> /sys/devices/platform/battery/Battery_Temperature"

3. 界面效果

界面效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

法迪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值