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