|
1 | 1 | package com.blankj.utilcode.utils; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.content.Intent; |
4 | 5 | import android.net.wifi.WifiInfo; |
5 | 6 | import android.net.wifi.WifiManager; |
| 7 | +import android.os.PowerManager; |
6 | 8 | import android.os.Build; |
7 | 9 | import android.provider.Settings; |
8 | 10 |
|
@@ -23,6 +25,45 @@ public class DeviceUtils { |
23 | 25 | private DeviceUtils() { |
24 | 26 | throw new UnsupportedOperationException("u can't instantiate me..."); |
25 | 27 | } |
| 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 | + } |
26 | 67 |
|
27 | 68 | /** |
28 | 69 | * 判断设备是否root |
@@ -119,4 +160,4 @@ public static String getModel() { |
119 | 160 | } |
120 | 161 | return model; |
121 | 162 | } |
122 | | -} |
| 163 | +} |