Skip to content

Commit 966f4c8

Browse files
authored
添加关机及重启设备代码
## 需要系统权限
1 parent be1b2eb commit 966f4c8

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.blankj.utilcode.utils;
22

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.net.wifi.WifiInfo;
56
import android.net.wifi.WifiManager;
7+
import android.os.PowerManager;
68
import android.os.Build;
79
import android.provider.Settings;
810

@@ -23,6 +25,45 @@ public class DeviceUtils {
2325
private DeviceUtils() {
2426
throw new UnsupportedOperationException("u can't instantiate me...");
2527
}
28+
29+
/**
30+
* 关闭设备
31+
* 需要系统权限 <android:sharedUserId="android.uid.system"/>
32+
* @param context
33+
*/
34+
public static void shutDownDevice(Context context) {
35+
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
36+
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
37+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
38+
context.startActivity(intent);
39+
}
40+
41+
/**
42+
* 重启设备,广播方式 需要系统权限
43+
* 需要系统权限 <android:sharedUserId="android.uid.system"/>
44+
* @param context
45+
*/
46+
public static void rebootDevice(Context context, String reason) {
47+
Intent intent = new Intent(Intent.ACTION_REBOOT);
48+
intent.putExtra("nowait", 1);
49+
intent.putExtra("interval", 1);
50+
intent.putExtra("window", 0);
51+
context.sendBroadcast(intent);
52+
}
53+
54+
/**
55+
* 重启设备, 电源管理的方式 需要系统权限
56+
* 需要系统权限 <android:sharedUserId="android.uid.system"/>
57+
* @param context
58+
*/
59+
public static void rebootDevicePM(Context context, String reason) {
60+
PowerManager mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
61+
try {
62+
mPowerManager.reboot(reason);
63+
} catch (Exception e) {
64+
KLog.w("重启设备失败: —— " + e.getMessage());
65+
}
66+
}
2667

2768
/**
2869
* 判断设备是否root
@@ -119,4 +160,4 @@ public static String getModel() {
119160
}
120161
return model;
121162
}
122-
}
163+
}

0 commit comments

Comments
 (0)