Skip to content

Commit 2d36cd9

Browse files
author
Suyunjing
authored
fix(android): Fix auto-start service not launching on boot (#36)
1 parent b5271f9 commit 2d36cd9

11 files changed

Lines changed: 206 additions & 173 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
.history
1010
.svn/
1111
migrate_working_dir/
12+
docs/
1213

1314
# IntelliJ related
1415
*.iml

android/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,21 @@
8282
android:name="flutterEmbedding"
8383
android:value="2" />
8484

85-
<!-- OpenList后台服务 -->
85+
<!-- OpenList background service -->
8686
<service
8787
android:name=".OpenListService"
88-
android:exported="true"
88+
android:exported="false"
8989
android:enabled="true"
90-
android:foregroundServiceType="dataSync"
91-
tools:ignore="ExportedService" />
90+
android:foregroundServiceType="dataSync" />
9291

93-
<!-- 开机启动接收器 -->
92+
<!-- Boot receiver -->
9493
<receiver
9594
android:name=".BootReceiver"
9695
android:exported="true"
9796
android:enabled="true">
98-
<intent-filter android:priority="1000">
97+
<intent-filter>
9998
<action android:name="android.intent.action.BOOT_COMPLETED" />
100-
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
101-
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
10299
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
103-
<action android:name="android.intent.action.PACKAGE_REPLACED" />
104-
<data android:scheme="package" />
105100
</intent-filter>
106101
</receiver>
107102

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.openlist.mobile
22

33
import android.app.Application
4+
import android.util.Log
5+
import com.openlist.mobile.model.openlist.OpenList
46
import com.openlist.mobile.utils.ToastUtils.longToast
57
import io.flutter.app.FlutterApplication
68

79
val app by lazy { App.app }
810

911
class App : FlutterApplication() {
1012
companion object {
13+
private const val TAG = "App"
1114
lateinit var app: Application
1215
}
1316

@@ -17,18 +20,27 @@ class App : FlutterApplication() {
1720

1821
app = this
1922

20-
// 设置全局异常处理器来捕获未处理的异常
23+
// Early initialization of OpenList to prepare for boot startup
24+
try {
25+
Log.d(TAG, "Performing early OpenList initialization")
26+
OpenList.init()
27+
Log.d(TAG, "OpenList early initialization completed")
28+
} catch (e: Exception) {
29+
Log.e(TAG, "Failed to initialize OpenList early", e)
30+
}
31+
32+
// Set global exception handler to catch uncaught exceptions
2133
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
22-
android.util.Log.e("App", "Uncaught exception in thread ${thread.name}", throwable)
34+
Log.e(TAG, "Uncaught exception in thread ${thread.name}", throwable)
2335

24-
// 如果是 JNI 相关的错误,记录详细信息
36+
// Log detailed info for JNI related errors
2537
if (throwable.message?.contains("JNI") == true ||
2638
throwable.message?.contains("native") == true ||
2739
throwable is UnsatisfiedLinkError) {
28-
android.util.Log.e("App", "Native/JNI related crash detected")
40+
Log.e(TAG, "Native/JNI related crash detected")
2941
}
3042

31-
// 调用默认的异常处理器
43+
// Call default exception handler
3244
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
3345
defaultHandler?.uncaughtException(thread, throwable)
3446
}

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

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,62 @@ import android.util.Log
88
import com.openlist.mobile.config.AppConfig
99

1010
/**
11-
* 开机启动接收器 - 处理开机启动和包更新事件
11+
* Boot receiver - handles device boot and package update events
1212
*/
1313
class BootReceiver : BroadcastReceiver() {
1414
companion object {
1515
private const val TAG = "BootReceiver"
1616
}
1717

18-
override fun onReceive(context: Context, intent: Intent) {
19-
val action = intent.action
20-
Log.d(TAG, "Received broadcast: $action")
18+
override fun onReceive(context: Context?, intent: Intent?) {
19+
if (context == null || intent?.action == null) return
20+
21+
Log.d(TAG, "Received broadcast: ${intent.action}")
2122

22-
try {
23-
when (action) {
24-
Intent.ACTION_BOOT_COMPLETED,
25-
"android.intent.action.QUICKBOOT_POWERON",
26-
"com.htc.intent.action.QUICKBOOT_POWERON" -> {
27-
Log.d(TAG, "Boot completed")
28-
handleBootCompleted(context)
29-
}
30-
31-
Intent.ACTION_MY_PACKAGE_REPLACED,
32-
Intent.ACTION_PACKAGE_REPLACED -> {
33-
Log.d(TAG, "Package replaced")
34-
handlePackageReplaced(context, intent)
35-
}
36-
37-
else -> {
38-
Log.d(TAG, "Unknown action: $action")
39-
}
23+
when (intent.action) {
24+
Intent.ACTION_BOOT_COMPLETED -> {
25+
handleBootCompleted(context)
26+
}
27+
Intent.ACTION_MY_PACKAGE_REPLACED -> {
28+
handlePackageReplaced(context)
4029
}
41-
} catch (e: Exception) {
42-
Log.e(TAG, "Error handling boot broadcast", e)
4330
}
4431
}
4532

46-
/**
47-
* 处理开机完成事件
48-
*/
4933
private fun handleBootCompleted(context: Context) {
5034
if (!AppConfig.isStartAtBootEnabled) {
51-
Log.d(TAG, "Auto start is disabled")
35+
Log.d(TAG, "Auto-start disabled, skipping")
5236
return
5337
}
5438

55-
// 开机时清除手动停止标志,因为设备重启了
39+
// Clear manual stop flag on boot
5640
AppConfig.isManuallyStoppedByUser = false
57-
Log.d(TAG, "Manual stop flag cleared on boot")
58-
59-
Log.d(TAG, "Starting services after boot")
60-
startServices(context)
41+
42+
Log.d(TAG, "Starting OpenList service")
43+
startService(context)
6144
}
6245

63-
/**
64-
* 处理包更新事件
65-
*/
66-
private fun handlePackageReplaced(context: Context, intent: Intent) {
67-
val packageName = intent.dataString
68-
if (packageName?.contains(context.packageName) == true) {
69-
Log.d(TAG, "Our package was replaced, restarting services")
70-
if (AppConfig.isStartAtBootEnabled) {
71-
startServices(context)
72-
}
46+
private fun handlePackageReplaced(context: Context) {
47+
if (!AppConfig.isStartAtBootEnabled) {
48+
Log.d(TAG, "Auto-start disabled, skipping package update restart")
49+
return
7350
}
51+
52+
Log.d(TAG, "Starting OpenList service after package update")
53+
startService(context)
7454
}
7555

76-
/**
77-
* 启动所有必要的服务
78-
*/
79-
private fun startServices(context: Context) {
56+
private fun startService(context: Context) {
8057
try {
81-
// 启动主服务
82-
val mainServiceIntent = Intent(context, OpenListService::class.java)
58+
val serviceIntent = Intent(context, OpenListService::class.java)
8359
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
84-
context.startForegroundService(mainServiceIntent)
60+
context.startForegroundService(serviceIntent)
8561
} else {
86-
context.startService(mainServiceIntent)
62+
context.startService(serviceIntent)
8763
}
88-
Log.d(TAG, "Main service start command sent")
89-
64+
Log.d(TAG, "Service start command sent")
9065
} catch (e: Exception) {
91-
Log.e(TAG, "Failed to start services", e)
66+
Log.e(TAG, "Failed to start service", e)
9267
}
9368
}
9469
}

0 commit comments

Comments
 (0)