This commit is contained in:
lollipopkit
2023-08-20 21:34:32 +08:00
parent 9c8ed3dfa8
commit a0287a9f36
4 changed files with 40 additions and 6 deletions

View File

@@ -55,5 +55,7 @@
android:name="android.appwidget.provider"
android:resource="@xml/home_widget" />
</receiver>
<service android:name=".KeepAliveService"/>
</application>
</manifest>

View File

@@ -0,0 +1,18 @@
package tech.lolli.toolbox
import android.app.Service
import android.content.Intent
import android.os.IBinder
import org.jetbrains.annotations.Nullable
class KeepAliveService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return START_STICKY
}
@Nullable
override fun onBind(intent: Intent?): IBinder? {
return null
}
}

View File

@@ -1,5 +1,6 @@
package tech.lolli.toolbox
import android.content.Intent
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
@@ -11,13 +12,20 @@ class MainActivity : FlutterActivity() {
MethodChannel(binaryMessenger, "tech.lolli.toolbox/app_retain").apply {
setMethodCallHandler { method, result ->
if (method.method == "sendToBackground") {
when (method.method) {
"sendToBackground" -> {
moveTaskToBack(true)
result.success(null)
} else {
}
"startService" -> {
val intent = Intent(this@MainActivity, KeepAliveService::class.java)
startService(intent)
}
else -> {
result.notImplemented()
}
}
}
}
}
}

View File

@@ -6,6 +6,7 @@ import 'package:logging/logging.dart';
import 'package:macos_window_utils/window_manipulator.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toolbox/data/res/misc.dart';
import 'package:toolbox/view/widget/custom_appbar.dart';
import 'app.dart';
@@ -92,8 +93,13 @@ Future<void> initApp() async {
final settings = locator<SettingStore>();
loadFontFile(settings.fontPath.fetch());
// SharedPreferences is only used on Android for saving home widgets settings.
// Android only
if (!isAndroid) return;
// Only start service when [bgRun] is true.
if (locator<SettingStore>().bgRun.fetch() ?? false) {
bgRunChannel.invokeMethod('startService');
}
// SharedPreferences is only used on Android for saving home widgets settings.
SharedPreferences.setPrefix('');
}