Android中apk中息屏、亮屏新思路_root权限熄屏亮屏-CSDN博客
文章浏览阅读9.6k次,点赞12次,收藏17次。备注:1.【项目的apk是跑在自己Android6.0主板设备上,上层是拥有Root权限的】 2.【本文中提到息屏是指在BroadcastReceiver中接收到ACTION_SCREEN_ON的操作; 亮屏是接收到Intent.ACTION_SCREEN_OFF操作】;业务需求 公司的设备是一个带显示屏Android(6.0 Root)板的智能终端,有人体感应头,现..._root权限熄屏亮屏
blog.csdn.net
C#:
//Bright screen logic code
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
public void openScreenOn() {
if (powerManager == null) {
powerManager = (PowerManager) AppApplication.getApplicationContexts().getSystemService(Context.POWER_SERVICE);
}
if (wakeLock == null) {
wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");
}
boolean ifOpen = powerManager.isScreenOn();
if (!ifOpen) {
//The screen lights up continuously
wakeLock.acquire();
//Release the lock so that the screen turns off after 2 minutes
wakeLock.release();
}
}
//The logic that needs to be called to the screen is called by the code
boolean root = AndroidRootUtils.checkDeviceRoot();
if(root){
AndroidRootUtils.execRootCmd("input keyevent 224");
}
//AndroidRootUtils Root permission tool class
public class AndroidRootUtils {
/**
*
* Determine whether the machine Android is rooted, that is, whether to obtain root permissions
*/
public static boolean checkDeviceRoot() {
boolean resualt = false;
int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
if (ret != -1) {
Log.i("checkDeviceRoot", "this Device have root!");
resualt = true;
} else {
resualt = false;
Log.i("checkDeviceRoot", "this Device not root!");
}
return resualt;
}
/**
* The command is executed and the result is output
*/
public static String execRootCmd(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process p = Runtime.getRuntime().exec("su");//The android system that has been rooted has the su command
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null) {
result += line;
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* Execute commands without paying attention to the resulting output
*/
public static int execRootCmdSilent(String cmd) {
int result = -1;
DataOutputStream dos = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}