当前位置:   article > 正文

android 实现息屏亮屏 Runtime.getRuntime().exec不执行

android 实现息屏亮屏 Runtime.getRuntime().exec不执行

        公司想实现远程息屏亮屏。试了下PowerManager,对我这个广告屏来讲是没有效果的。

  1. import android.content.Context;
  2. import android.os.PowerManager;
  3. public class ScreenStateHelper {
  4. private PowerManager powerManager;
  5. private WakeLock wakeLock;
  6. public ScreenStateHelper(Context context) {
  7. powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  8. }
  9. public boolean isScreenOn() {
  10. return powerManager.isInteractive();
  11. }
  12. public void acquireWakeLock() {
  13. if (wakeLock != null) {
  14. wakeLock.release();
  15. }
  16. wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyWakeLock");
  17. wakeLock.acquire();
  18. }
  19. public void releaseWakeLock() {
  20. if (wakeLock != null) {
  21. wakeLock.release();
  22. wakeLock = null;
  23. }
  24. }
  25. }

然后想着是把亮度调成0,结果只是变得很暗,还是能看见界面:

  1. public static void setScreenDark(Context context) {
  2. if (!Settings.System.canWrite(context)) {
  3. Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
  4. intent.setData(Uri.parse("package:" + context.getPackageName()));
  5. context.startActivity(intent);
  6. } else {
  7. Settings.System.putInt(
  8. context.getContentResolver(),
  9. Settings.System.SCREEN_BRIGHTNESS,
  10. 0
  11. );
  12. }
  13. }

最后想着用adb命令试试:

        

adb shell input keyevent KEYCODE_POWER

发现是可以使用的,然后使用Runtime.getRuntime().exec("input keyevent KEYCODE_POWER"),发现死活没反应,设备也root了。后来发现需要su权限,然后就可以允许了,代码如下:

  1. public static void executeADBCommands(boolean isRooted, String... commands) {
  2. Process process = null;
  3. BufferedReader successResult = null;
  4. BufferedReader errorResult = null;
  5. DataOutputStream os = null;
  6. try {
  7. process = Runtime.getRuntime().exec(isRooted ? "su" : "sh", null, null);
  8. os = new DataOutputStream(process.getOutputStream());
  9. for (String command : commands) {
  10. if (command == null) continue;
  11. os.write(command.getBytes());
  12. os.writeBytes(System.getProperty("line.separator"));
  13. os.flush();
  14. }
  15. os.writeBytes("exit" + System.getProperty("line.separator"));
  16. os.flush();
  17. int result = process.waitFor();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. } finally {
  21. try {
  22. if (os != null) {
  23. os.close();
  24. }
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. try {
  29. if (successResult != null) {
  30. successResult.close();
  31. }
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. try {
  36. if (errorResult != null) {
  37. errorResult.close();
  38. }
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. if (process != null) {
  43. process.destroy();
  44. }
  45. }
  46. }

调用的时候:

CommonUtils.executeADBCommands(true, "input keyevent KEYCODE_POWER");

这样就能做到息屏亮屏了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/293273
推荐阅读
相关标签
  

闽ICP备14008679号