mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
new: custom foreground service on Android (#556)
This commit is contained in:
@@ -53,7 +53,6 @@ android {
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
|
||||
applicationId "tech.lolli.toolbox"
|
||||
// You can update the following values to match your application needs.
|
||||
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<manifest xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
@@ -15,7 +16,8 @@
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:allowBackup="true"
|
||||
android:hasFragileUserData="true"
|
||||
android:restoreAnyVersion="true">
|
||||
android:restoreAnyVersion="true"
|
||||
tools:targetApi="q">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
@@ -43,11 +45,6 @@
|
||||
android:name="flutterEmbedding"
|
||||
android:value="2" />
|
||||
|
||||
<service
|
||||
android:name="id.flutter.flutter_background_service.BackgroundService"
|
||||
android:foregroundServiceType="dataSync"
|
||||
/>
|
||||
|
||||
<receiver
|
||||
android:name=".widget.HomeWidget"
|
||||
android:exported="false"
|
||||
@@ -68,8 +65,9 @@
|
||||
</receiver>
|
||||
|
||||
<service
|
||||
android:name=".MyForegroundService"
|
||||
android:name=".ForegroundService"
|
||||
android:enabled="true"
|
||||
android:foregroundServiceType="dataSync"
|
||||
android:exported="false" />
|
||||
|
||||
</application>
|
||||
|
||||
@@ -4,11 +4,9 @@ import android.app.*
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
class MyForegroundService : Service() {
|
||||
|
||||
private val CHANNEL_ID = "ForegroundServiceChannel"
|
||||
class ForegroundService : Service() {
|
||||
private val chanId = "ForegroundServiceChannel"
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@@ -31,8 +29,8 @@ class MyForegroundService : Service() {
|
||||
private fun createNotificationChannel() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val serviceChannel = NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
CHANNEL_ID,
|
||||
chanId,
|
||||
chanId,
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
)
|
||||
val manager = getSystemService(NotificationManager::class.java)
|
||||
@@ -49,11 +47,20 @@ class MyForegroundService : Service() {
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
Notification.Builder(this, chanId)
|
||||
.setContentTitle("App is running")
|
||||
.setContentText("Click to open the app")
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
} else {
|
||||
Notification.Builder(this)
|
||||
.setContentTitle("App is running")
|
||||
.setContentText("Click to open the app")
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package tech.lolli.toolbox
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.Manifest
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import io.flutter.embedding.android.FlutterFragmentActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
@@ -18,7 +23,8 @@ class MainActivity: FlutterFragmentActivity() {
|
||||
result.success(null)
|
||||
}
|
||||
"startService" -> {
|
||||
val serviceIntent = Intent(this, ForegroundService::class.java)
|
||||
reqPerm()
|
||||
val serviceIntent = Intent(this@MainActivity, ForegroundService::class.java)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(serviceIntent)
|
||||
} else {
|
||||
@@ -32,4 +38,16 @@ class MainActivity: FlutterFragmentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun reqPerm() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
|
||||
123,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:flutter_displaymode/flutter_displaymode.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:server_box/app.dart';
|
||||
import 'package:server_box/core/channel/bg_run.dart';
|
||||
import 'package:server_box/core/utils/sync/icloud.dart';
|
||||
import 'package:server_box/core/utils/sync/webdav.dart';
|
||||
import 'package:server_box/data/model/app/menu/server_func.dart';
|
||||
@@ -109,6 +110,10 @@ void _doPlatformRelated() async {
|
||||
if (isAndroid) {
|
||||
// try switch to highest refresh rate
|
||||
FlutterDisplayMode.setHighRefreshRate();
|
||||
if (Stores.setting.bgRun.fetch()) {
|
||||
Loggers.app.info('Start foreground service');
|
||||
BgRunMC.startService();
|
||||
}
|
||||
}
|
||||
|
||||
final serversCount = Stores.server.box.keys.length;
|
||||
|
||||
@@ -259,11 +259,11 @@ class _AddPage extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const viewPadding = 3.0;
|
||||
const viewPadding = 7.0;
|
||||
final viewWidth = context.media.size.width - 2 * viewPadding;
|
||||
|
||||
final itemCount = ServerProvider.servers.length;
|
||||
const itemPadding = 3.0;
|
||||
const itemPadding = 1.0;
|
||||
const itemWidth = 150.0;
|
||||
const itemHeight = 50.0;
|
||||
|
||||
@@ -272,9 +272,7 @@ class _AddPage extends StatelessWidget {
|
||||
max(viewWidth ~/ (visualCrossCount * itemPadding + itemWidth), 1);
|
||||
final mainCount = itemCount ~/ crossCount + 1;
|
||||
|
||||
return Center(
|
||||
key: const Key('sshTabAddServer'),
|
||||
child: ServerProvider.serverOrder.listenVal((order) {
|
||||
return ServerProvider.serverOrder.listenVal((order) {
|
||||
if (order.isEmpty) {
|
||||
return Center(
|
||||
child: Text(libL10n.empty, textAlign: TextAlign.center),
|
||||
@@ -290,15 +288,12 @@ class _AddPage extends StatelessWidget {
|
||||
children: List.generate(crossCount, (columnIndex) {
|
||||
final idx = rowIndex * crossCount + columnIndex;
|
||||
final id = order.elementAtOrNull(idx);
|
||||
if (id == null) return _placeholder;
|
||||
|
||||
final spi = ServerProvider.pick(id: order[idx])?.value.spi;
|
||||
final spi = ServerProvider.pick(id: id)?.value.spi;
|
||||
if (spi == null) return _placeholder;
|
||||
|
||||
return Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(itemPadding),
|
||||
child: CardX(
|
||||
child: InkWell(
|
||||
onTap: () => onTapInitCard(spi),
|
||||
child: Container(
|
||||
@@ -319,15 +314,13 @@ class _AddPage extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
).cardx,
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user