Skip to content

Commit 10c82c1

Browse files
authored
perf(service): Optimize the keepalive service restart logic, increase the restart cooldown time, and avoid repeated restarts (#15)
1 parent c9d78d7 commit 10c82c1

File tree

11 files changed

+27
-1344
lines changed

11 files changed

+27
-1344
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
66
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
77

8-
<!-- 定时器和启动权限 -->
9-
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
10-
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
8+
<!-- 开机启动权限 -->
119
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
12-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
1310

1411
<!-- 查询包权限 -->
1512
<uses-permission
@@ -36,15 +33,9 @@
3633
<!-- 电池优化和唤醒锁权限 -->
3734
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
3835
<uses-permission android:name="android.permission.WAKE_LOCK" />
39-
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
4036

4137
<!-- 安装权限 -->
4238
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
43-
44-
<!-- 保活相关权限 -->
45-
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
46-
<uses-permission android:name="android.permission.GET_TASKS" />
47-
<uses-permission android:name="android.permission.REORDER_TASKS" />
4839

4940
<application
5041
android:name="com.openlist.mobile.App"
@@ -99,13 +90,6 @@
9990
android:foregroundServiceType="dataSync"
10091
tools:ignore="ExportedService" />
10192

102-
<!-- 保活服务 -->
103-
<service
104-
android:name=".KeepAliveService"
105-
android:exported="false"
106-
android:enabled="true"
107-
android:process=":keep_alive" />
108-
10993
<!-- 开机启动接收器 -->
11094
<receiver
11195
android:name=".BootReceiver"
@@ -121,20 +105,5 @@
121105
</intent-filter>
122106
</receiver>
123107

124-
<!-- 保活接收器 -->
125-
<receiver
126-
android:name=".KeepAliveReceiver"
127-
android:exported="true"
128-
android:enabled="true">
129-
<intent-filter android:priority="1000">
130-
<action android:name="android.intent.action.SCREEN_ON" />
131-
<action android:name="android.intent.action.SCREEN_OFF" />
132-
<action android:name="android.intent.action.USER_PRESENT" />
133-
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
134-
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
135-
<action android:name="com.openlist.mobile.KEEP_ALIVE" />
136-
</intent-filter>
137-
</receiver>
138-
139108
</application>
140109
</manifest>

android/app/src/main/kotlin/com/openlist/mobile/App.kt

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,55 +28,9 @@ class App : FlutterApplication() {
2828
android.util.Log.e("App", "Native/JNI related crash detected")
2929
}
3030

31-
// 尝试重启服务(保活机制)
32-
try {
33-
// 检查是否被用户手动停止
34-
if (com.openlist.mobile.config.AppConfig.isManuallyStoppedByUser) {
35-
android.util.Log.d("App", "Service was manually stopped by user, skipping restart after crash")
36-
} else if (com.openlist.mobile.config.AppConfig.isStartAtBootEnabled) {
37-
val intent = android.content.Intent(this, OpenListService::class.java)
38-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
39-
startForegroundService(intent)
40-
} else {
41-
startService(intent)
42-
}
43-
android.util.Log.d("App", "Service restart attempted after crash")
44-
}
45-
} catch (e: Exception) {
46-
android.util.Log.e("App", "Failed to restart service after crash", e)
47-
}
48-
4931
// 调用默认的异常处理器
5032
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
5133
defaultHandler?.uncaughtException(thread, throwable)
5234
}
53-
54-
// 初始化保活机制
55-
initKeepAlive()
56-
}
57-
58-
/**
59-
* 初始化保活机制
60-
*/
61-
private fun initKeepAlive() {
62-
try {
63-
// 初始化WorkManager任务
64-
com.openlist.mobile.utils.WorkManagerHelper.initialize(this)
65-
66-
// 检查是否被用户手动停止
67-
if (com.openlist.mobile.config.AppConfig.isManuallyStoppedByUser) {
68-
android.util.Log.d("App", "Service was manually stopped by user, skipping keep alive initialization")
69-
return
70-
}
71-
72-
// 如果启用了开机启动,则启动保活服务
73-
if (com.openlist.mobile.config.AppConfig.isStartAtBootEnabled) {
74-
val keepAliveIntent = android.content.Intent(this, KeepAliveService::class.java)
75-
startService(keepAliveIntent)
76-
android.util.Log.d("App", "Keep alive service started")
77-
}
78-
} catch (e: Exception) {
79-
android.util.Log.e("App", "Failed to initialize keep alive", e)
80-
}
8135
}
8236
}

android/app/src/main/kotlin/com/openlist/mobile/BootReceiver.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ class BootReceiver : BroadcastReceiver() {
8787
}
8888
Log.d(TAG, "Main service start command sent")
8989

90-
// 启动保活服务
91-
val keepAliveIntent = Intent(context, KeepAliveService::class.java)
92-
context.startService(keepAliveIntent)
93-
Log.d(TAG, "Keep alive service start command sent")
94-
9590
} catch (e: Exception) {
9691
Log.e(TAG, "Failed to start services", e)
9792
}

android/app/src/main/kotlin/com/openlist/mobile/KeepAliveReceiver.kt

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)