Browse Source

更新桌面程序

lan 1 year ago
parent
commit
9a2982759e

+ 3 - 1
app/build.gradle

@@ -7,7 +7,7 @@ versionberg {
     major 1
     minor 1
     patch 0
-    build 46 // ${commitCount}
+    build 47 // ${commitCount}
     nameTemplate '${major}.${minor}.${patch}.${build}'// 1.1.0.01
     codeTemplate '${major}0${minor}0${patch}${build}'// 101001
 }
@@ -100,6 +100,8 @@ dependencies {
     implementation 'com.github.bumptech.glide:glide:4.14.2'
     //git AndroidAutoSize
     implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
+    // scheduler
+    api 'com.xdandroid:hellodaemon:1.2.2'
 }
 repositories {
     mavenCentral()

+ 2 - 3
app/src/main/java/com/zedu/launcher/MyApplication.java

@@ -34,7 +34,7 @@ import com.zedu.launcher.update.OKHttpUpdateHttpService;
 import com.zedu.launcher.http.OkgoDns;
 import com.zedu.launcher.socket.MyWebSocketClient;
 import com.zedu.launcher.socket.MyWebSocketServer;
-
+import com.zedu.launcher.daemon.DaemonCreator;
 import java.io.File;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
@@ -58,9 +58,8 @@ public class MyApplication extends Application {
         initXupdate();
         sendReceiver();
         GlobalData.isTeacher = SPStaticUtils.getBoolean(SPConfig.TEACHER_ROLE, false);
+        DaemonCreator.init(this);
     }
-
-
     private void initStorageFile() {
         // 如果存储卡可用
         String storagePath = PathUtils.getExternalStoragePath();

+ 14 - 0
app/src/main/java/com/zedu/launcher/daemon/DaemonCreator.java

@@ -0,0 +1,14 @@
+package com.zedu.launcher.daemon;
+
+import com.xdandroid.hellodaemon.DaemonEnv;
+import com.zedu.launcher.MyApplication;
+
+public class DaemonCreator {
+    public static void init(MyApplication app) {
+        // 初始化
+        DaemonEnv.initialize(app, DaemonService.class, 3 * 60 * 1000);
+        // 启动
+        DaemonService.sShouldStopService = false;
+        DaemonEnv.startServiceMayBind(DaemonService.class);
+    }
+}

+ 76 - 0
app/src/main/java/com/zedu/launcher/daemon/DaemonService.java

@@ -0,0 +1,76 @@
+package com.zedu.launcher.daemon;
+
+import android.content.Intent;
+import android.os.IBinder;
+
+import com.blankj.utilcode.util.LogUtils;
+import com.xdandroid.hellodaemon.AbsWorkService;
+import com.zedu.launcher.socket.MyWebSocketServer;
+
+import java.util.concurrent.TimeUnit;
+
+import io.reactivex.Observable;
+import io.reactivex.disposables.Disposable;
+
+public class DaemonService extends AbsWorkService {
+
+    //是否 任务完成, 不再需要服务运行?
+    public static boolean sShouldStopService;
+
+    public static Disposable sDisposable;
+
+    public static void stopService() {
+        LogUtils.d("服务停止");
+        //我们现在不再需要服务运行了, 将标志位置为 true
+        sShouldStopService = true;
+        //取消对任务的订阅
+        if (sDisposable != null) {
+            sDisposable.dispose();
+        }
+        //取消 Job / Alarm / Subscription
+        cancelJobAlarmSub();
+    }
+
+    @Override
+    public Boolean isWorkRunning(Intent intent, int flags, int startId) {
+        //若还没有取消订阅, 就说明任务仍在运行.
+        return sDisposable != null && !sDisposable.isDisposed();
+    }
+
+    @Override
+    public IBinder onBind(Intent intent, Void v) {
+        return null;
+    }
+
+    @Override
+    public void startWork(Intent intent, int flags, int startId) {
+        LogUtils.d("服务开始:检查磁盘中是否有上次销毁时保存的数据");
+        sDisposable = Observable.interval(5, TimeUnit.SECONDS)
+                //取消任务时取消定时唤醒
+                .doOnDispose(() -> {
+                    LogUtils.d("取消任务");
+                    cancelJobAlarmSub();
+                }).subscribe(count -> {
+                    // 2、每隔60秒,更新设备信息
+                    if (count > 0 && count % 10 == 0) {
+                        MyWebSocketServer.tryBroadcast("keepSelfLive");
+                    }
+                }, throwable -> LogUtils.d(throwable.getMessage()));
+    }
+
+    @Override
+    public void stopWork(Intent intent, int flags, int startId) {
+        LogUtils.d("服务结束");
+        stopService();
+    }
+
+    @Override
+    public Boolean shouldStopService(Intent intent, int flags, int startId) {
+        return sShouldStopService;
+    }
+
+    @Override
+    public void onServiceKilled(Intent rootIntent) {
+        LogUtils.d("服务关闭");
+    }
+}