AutoRebootUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.wdkl.callingbed.util;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlarmManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.SystemProperties;
  9. import com.wdkl.callingbed.MyApplication;
  10. import com.wdkl.callingbed.common.Constants;
  11. import java.util.Calendar;
  12. import static com.wdkl.callingbed.common.Constants.NET_ERROR_FIVE_AFTER_TOAST;
  13. import static com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand.closeHeart;
  14. /**
  15. * Created by dengzhe on 2018/4/18.
  16. * //========自动重启工具类========//
  17. */
  18. public class AutoRebootUtil {
  19. private static Context context;
  20. private static Calendar calendar;
  21. private static int countDownMinute;
  22. private static String currentTime = "0";
  23. private static int timeFirst = 11;
  24. private static int timeSecond = 17;
  25. private static int mt = 50;
  26. private static int lt = 55;
  27. private static int hour;
  28. private static int minute;
  29. public static boolean sys_Reset = false;
  30. public static boolean sys_Shutdown = false;
  31. public static void reboot(Context context) {
  32. if (!sys_Reset) {
  33. sys_Reset = true;
  34. closeHeart();//关闭心跳
  35. Intent intent = new Intent(Intent.ACTION_REBOOT);
  36. intent.putExtra("nowait", 1);
  37. intent.putExtra("interval", 1);
  38. intent.putExtra("window", 0);
  39. context.sendBroadcast(intent);
  40. }
  41. }
  42. public static void shutdown(Context context) {
  43. if (!sys_Shutdown) {
  44. sys_Shutdown = true;
  45. closeHeart();//关闭心跳
  46. Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
  47. intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
  48. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  49. context.startActivity(intent);
  50. }
  51. }
  52. public static void reboot() {
  53. if (!sys_Reset) {
  54. sys_Reset = true;
  55. closeHeart();
  56. try {
  57. Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot "});
  58. proc.waitFor();
  59. } catch (Exception ex) {
  60. ex.printStackTrace();
  61. }
  62. }
  63. }
  64. public static void shutdown() {
  65. if (!sys_Shutdown) {
  66. sys_Shutdown = true;
  67. closeHeart();
  68. try {
  69. Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot -p"});
  70. proc.waitFor();
  71. } catch (Exception ex) {
  72. ex.printStackTrace();
  73. }
  74. }
  75. }
  76. //wuyq add for ethernet reboot begin
  77. public static void ethreboot() {
  78. try {
  79. SystemProperties.set("persist.sys.ethreboot","1");
  80. Thread.sleep(1000);
  81. } catch (Exception ex) {
  82. ex.printStackTrace();
  83. }
  84. }
  85. //wuyq add for ethernet reboot end
  86. /**
  87. * =======================================(网络信息初始化错误15次以上)并且连续重启次数3次,不再重启系统,需更换机子=======================================
  88. */
  89. public static void rebootContinueCounts(int counts) {
  90. SharedPreferencesUtil.putStringSp(context, NET_ERROR_FIVE_AFTER_TOAST, NET_ERROR_FIVE_AFTER_TOAST, String.valueOf(counts));
  91. }
  92. public static int getRepeatRebootCounts() {
  93. String rebootCounts = SharedPreferencesUtil
  94. .getStringSp(context, NET_ERROR_FIVE_AFTER_TOAST, NET_ERROR_FIVE_AFTER_TOAST);
  95. return rebootCounts.equals("") ? 0 : Integer.valueOf(rebootCounts);
  96. }
  97. public static void calculate(Context contexts) {
  98. context = contexts;
  99. calendar = Calendar.getInstance();
  100. hour = calendar.get(Calendar.HOUR_OF_DAY);
  101. minute = calendar.get(Calendar.MINUTE);
  102. // LogUtil.d("hour+minute", hour + "\t" + minute + "");
  103. countDownMinute = mt - minute;
  104. if ((hour == timeFirst || hour == timeSecond) && ((minute > mt) && (minute < lt))) {//12点和18点开启重启,检测网卡(每天)
  105. rebootContinueCounts(0);
  106. }
  107. // LogUtil.d("getRepeatRebootCounts()", getRepeatRebootCounts() + "");
  108. if (getRepeatRebootCounts() < 3) {
  109. LogUtil.d("getRepeatRebootCounts()", "is coming");
  110. rebootContinueCounts(getRepeatRebootCounts() + 1);
  111. reboot(contexts);
  112. } else {
  113. }
  114. }
  115. public static int getCountDownMinute() {
  116. return countDownMinute;
  117. }
  118. /**
  119. * 重启倒计时信息
  120. *
  121. * @return
  122. */
  123. public static String getTextTip() {
  124. if (countDownMinute <= 0) {
  125. return "";
  126. } else if (countDownMinute <= 5 && countDownMinute > 0 && (hour == timeFirst || hour == timeSecond)) {
  127. return "网络自检启动:系统将在" + countDownMinute + "分钟后重新启动";
  128. } else return "";
  129. }
  130. private static boolean restart = false;
  131. public static void restartApp() {
  132. if (!restart) {
  133. try {
  134. Intent intent = new Intent(MyApplication.getAppContext(), Constants.ANR_FC);
  135. @SuppressLint("WrongConstant") PendingIntent mPendingIntent = PendingIntent.getActivity(
  136. MyApplication.getAppContext(), 0, intent,
  137. Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  138. AlarmManager mgr = (AlarmManager) MyApplication.getAppContext().getSystemService(Context.ALARM_SERVICE);
  139. mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, mPendingIntent);
  140. for (Activity activity : MyApplication.activities) {
  141. if (activity != null) {
  142. activity.finish();
  143. }
  144. }
  145. MyApplication.activities.clear();
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. restart = true;
  150. android.os.Process.killProcess(android.os.Process.myPid());
  151. System.exit(0);
  152. }
  153. }
  154. }