From 53bc91c07a31ebadaf95b44864d482e21ad71a53 Mon Sep 17 00:00:00 2001 From: level <1364007522@qq.com> Date: Mon, 30 Jun 2025 15:34:33 +0800 Subject: [PATCH] =?UTF-8?q?bug=20fix=20:=E6=9B=B4=E6=96=B0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=BF=E7=94=A8=E6=97=B6=E9=95=BF=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=94=B110s=E6=9B=B4=E6=94=B9=E4=B8=BA60s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yingmao/clash/fragment/home/HomeFragment.kt | 17 ++++++++++++++++- .../clash/service/ConnectionService.java | 2 +- .../main/java/com/yingmao/clash/util/Utils.kt | 11 +++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/yingmao/clash/fragment/home/HomeFragment.kt b/app/src/main/java/com/yingmao/clash/fragment/home/HomeFragment.kt index e69062e..36498bc 100644 --- a/app/src/main/java/com/yingmao/clash/fragment/home/HomeFragment.kt +++ b/app/src/main/java/com/yingmao/clash/fragment/home/HomeFragment.kt @@ -66,6 +66,7 @@ import com.yingmao.clash.util.ActivityResultLifecycle import com.yingmao.clash.util.CommonUtils import com.yingmao.clash.util.MarqueeTextView import com.yingmao.clash.util.PayUtils +import com.yingmao.clash.util.Utils import com.yingmao.clash.util.startClashService import com.yingmao.clash.util.stopClashService import com.yingmao.clash.view.RxToast @@ -384,7 +385,12 @@ class HomeFragment : BaseFragment(), CoroutineScope by CoroutineScope(Dispatcher MMKV.defaultMMKV().encode(BZYConstants.CONNECTID, dataInfo.data.id) val serviceIntent = Intent(requireContext(), ConnectionService::class.java) serviceIntent.putExtra("receiver", resultReceiver) - requireContext().startService(serviceIntent) + val serviceRunning = + Utils.isServiceRunning(requireContext(), ConnectionService::class.java) + android.util.Log.i("AppStartupStatus","data::"+serviceRunning) + if(!serviceRunning){ + requireContext().startService(serviceIntent) + } } } } @@ -994,4 +1000,13 @@ class HomeFragment : BaseFragment(), CoroutineScope by CoroutineScope(Dispatcher }) dialog.show() } + override fun onDestroy() { + super.onDestroy() + val serviceRunning = + Utils.isServiceRunning(requireContext(), ConnectionService::class.java) + if(serviceRunning){ + val serviceIntent= Intent(requireContext(), ConnectionService::class.java) + requireContext().stopService(serviceIntent) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/yingmao/clash/service/ConnectionService.java b/app/src/main/java/com/yingmao/clash/service/ConnectionService.java index 432e09a..18b1cfc 100644 --- a/app/src/main/java/com/yingmao/clash/service/ConnectionService.java +++ b/app/src/main/java/com/yingmao/clash/service/ConnectionService.java @@ -13,7 +13,7 @@ import com.yingmao.clash.fragment.AccelerateViewModel; public class ConnectionService extends Service { private static final String TAG = "TimerService"; - private static final long INTERVAL_MILLIS = 10000; // 10 秒 + private static final long INTERVAL_MILLIS = 60000; // 60 秒 private Handler handler = new Handler(); private Intent intent; private Runnable runnable = new Runnable() { diff --git a/app/src/main/java/com/yingmao/clash/util/Utils.kt b/app/src/main/java/com/yingmao/clash/util/Utils.kt index bd68f1d..a55eb29 100644 --- a/app/src/main/java/com/yingmao/clash/util/Utils.kt +++ b/app/src/main/java/com/yingmao/clash/util/Utils.kt @@ -2,6 +2,7 @@ package com.yingmao.clash.util import android.Manifest import android.app.Activity +import android.app.ActivityManager import android.content.Context import android.content.pm.PackageManager import android.media.MediaDrm @@ -286,5 +287,15 @@ object Utils { v.requestLayout() } } + fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean { + val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val runningServices = manager.getRunningServices(Int.MAX_VALUE) + for (service in runningServices) { + if (serviceClass.name == service.service.className) { + return true + } + } + return false + } } \ No newline at end of file