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"