From 372d7f1776bf77c6522b3060f86bae518d105aaa Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 1 Aug 2021 11:36:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/service/WebService.kt | 8 ++----- .../io/legado/app/service/WebTileService.kt | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index 2342a89c4..c11024ac8 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -41,9 +41,7 @@ class WebService : BaseService() { isRun = true notificationContent = getString(R.string.service_starting) upNotification() - startService { - action = IntentAction.start - } + WebTileService.setState(this, true) } override fun onDestroy() { @@ -56,9 +54,7 @@ class WebService : BaseService() { webSocketServer?.stop() } postEvent(EventBus.WEB_SERVICE, "") - startService { - action = IntentAction.stop - } + WebTileService.setState(this, false) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { diff --git a/app/src/main/java/io/legado/app/service/WebTileService.kt b/app/src/main/java/io/legado/app/service/WebTileService.kt index c1a1ccad5..a99fb062d 100644 --- a/app/src/main/java/io/legado/app/service/WebTileService.kt +++ b/app/src/main/java/io/legado/app/service/WebTileService.kt @@ -1,15 +1,36 @@ package io.legado.app.service +import android.content.Context import android.content.Intent import android.os.Build import android.service.quicksettings.Tile import android.service.quicksettings.TileService import androidx.annotation.RequiresApi import io.legado.app.constant.IntentAction +import io.legado.app.utils.startService +/** + * web服务快捷开关 + */ @RequiresApi(Build.VERSION_CODES.N) class WebTileService : TileService() { + companion object { + + fun setState(context: Context, active: Boolean) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + context.startService { + action = if (active) { + IntentAction.start + } else { + IntentAction.stop + } + } + } + } + + } + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.action) { IntentAction.start -> {