GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加关机及重启设备代码 · RlonLL11FE/AndroidUtilCode@966f4c8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • utilcode/src/main/java/com/blankj/utilcode/utils
    • DeviceUtils.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java
+42-1Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.