Commit 6d25c5c9 by pye52

优化Monitor的代码结构

parent 75095b78
...@@ -325,8 +325,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -325,8 +325,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
commandViewModel.initialize(); commandViewModel.initialize();
handler.post(updateTimeRunnable); handler.post(updateTimeRunnable);
MonitorUtils.startLogFilesMonitor(this); MonitorUtils.startMonitors(this);
MonitorUtils.startDatabaseMonitor(this);
SCWebSocketClient.getInstance().tryConnect(); SCWebSocketClient.getInstance().tryConnect();
} }
...@@ -400,8 +399,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -400,8 +399,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
TTSHelper.release(); TTSHelper.release();
SCWebSocketClient.getInstance().realClose(); SCWebSocketClient.getInstance().realClose();
SCTaskExecutor.getInstance().quit(); SCTaskExecutor.getInstance().quit();
MonitorUtils.stopDatabaseMonitor(this); MonitorUtils.stopMonitors(this);
MonitorUtils.stopLogFilesMonitor(this);
} }
private void initViews() { private void initViews() {
......
...@@ -17,8 +17,18 @@ public class MonitorUtils { ...@@ -17,8 +17,18 @@ public class MonitorUtils {
private static final int LOG_REPEAT_INTERVAL = 4; private static final int LOG_REPEAT_INTERVAL = 4;
private static final int DATABASE_REPEAT_INTERVAL = 12; private static final int DATABASE_REPEAT_INTERVAL = 12;
public static void startMonitors(Context context) {
startLogFilesMonitor(context);
startDatabaseMonitor(context);
}
public static void stopMonitors(Context context) {
stopLogFilesMonitor(context);
stopDatabaseMonitor(context);
}
// 每隔一定时间检查日志文件夹并进行清理 // 每隔一定时间检查日志文件夹并进行清理
public static void startLogFilesMonitor(Context context) { private static void startLogFilesMonitor(Context context) {
stopLogFilesMonitor(context); stopLogFilesMonitor(context);
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(LogFileMonitor.class, LOG_REPEAT_INTERVAL, TimeUnit.HOURS) PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(LogFileMonitor.class, LOG_REPEAT_INTERVAL, TimeUnit.HOURS)
.addTag(LOG_MONITOR_WORKER) .addTag(LOG_MONITOR_WORKER)
...@@ -27,13 +37,13 @@ public class MonitorUtils { ...@@ -27,13 +37,13 @@ public class MonitorUtils {
.enqueue(request); .enqueue(request);
} }
public static void stopLogFilesMonitor(Context context) { private static void stopLogFilesMonitor(Context context) {
WorkManager.getInstance(context) WorkManager.getInstance(context)
.cancelAllWorkByTag(LOG_MONITOR_WORKER); .cancelAllWorkByTag(LOG_MONITOR_WORKER);
} }
// 每隔一定时间检查数据库并进行清理 // 每隔一定时间检查数据库并进行清理
public static void startDatabaseMonitor(Context context) { private static void startDatabaseMonitor(Context context) {
stopDatabaseMonitor(context); stopDatabaseMonitor(context);
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(DatabaseMonitor.class, DATABASE_REPEAT_INTERVAL, TimeUnit.HOURS) PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(DatabaseMonitor.class, DATABASE_REPEAT_INTERVAL, TimeUnit.HOURS)
.addTag(DATABASE_MONITOR_WORKER) .addTag(DATABASE_MONITOR_WORKER)
...@@ -42,7 +52,7 @@ public class MonitorUtils { ...@@ -42,7 +52,7 @@ public class MonitorUtils {
.enqueue(request); .enqueue(request);
} }
public static void stopDatabaseMonitor(Context context) { private static void stopDatabaseMonitor(Context context) {
WorkManager.getInstance(context) WorkManager.getInstance(context)
.cancelAllWorkByTag(DATABASE_MONITOR_WORKER); .cancelAllWorkByTag(DATABASE_MONITOR_WORKER);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment