后台服务启动设置优化完成
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Mon Oct 27 01:09:02 GMT 2025 | ||||
| #Mon Oct 27 02:06:36 GMT 2025 | ||||
| stageCount=14 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.13 | ||||
| buildCount=4 | ||||
| buildCount=13 | ||||
| baseBetaVersion=15.0.14 | ||||
|   | ||||
| @@ -18,6 +18,7 @@ import cc.winboll.studio.positions.utils.ServiceUtil; | ||||
| public class AssistantService extends Service { | ||||
|  | ||||
|     public final static String TAG = "AssistantService"; | ||||
| 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE"; | ||||
| 	 | ||||
|     MyServiceConnection mMyServiceConnection; | ||||
|     volatile boolean mIsServiceRunning; | ||||
| @@ -37,13 +38,18 @@ public class AssistantService extends Service { | ||||
|         } | ||||
|         // 设置运行参数 | ||||
|         mIsServiceRunning = false; | ||||
|         if (mAppConfigsUtil.isEnableMainService(true)) { | ||||
| 			run(); | ||||
| 		} | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int onStartCommand(Intent intent, int flags, int startId) { | ||||
| 		if (mAppConfigsUtil.isEnableMainService(true)) { | ||||
| 			run(); | ||||
|         return START_STICKY; | ||||
| 		} | ||||
|  | ||||
|         return  mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -43,7 +43,7 @@ public class MainService extends Service { | ||||
|  | ||||
|     public static final String TAG = "MainService"; | ||||
|  | ||||
| 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_ENABLE"; | ||||
| 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE"; | ||||
|  | ||||
|     // ---------------------- 新增:定时器相关变量 ---------------------- | ||||
|     private ScheduledExecutorService taskCheckTimer; // 任务校验定时器 | ||||
| @@ -325,8 +325,10 @@ public class MainService extends Service { | ||||
|      */ | ||||
|     public static synchronized MainService getInstance(Context context) { | ||||
|         if (sInstance == null) { | ||||
| 			if (AppConfigsUtil.getInstance(context).isEnableMainService(true)) { | ||||
| 				Intent intent = new Intent(context.getApplicationContext(), MainService.class); | ||||
| 				context.getApplicationContext().startService(intent); | ||||
| 			} | ||||
|             return null; | ||||
|         } | ||||
|         if (sAppContext == null) { | ||||
| @@ -365,8 +367,10 @@ public class MainService extends Service { | ||||
|             mMyServiceConnection = new MyServiceConnection(); | ||||
|         } | ||||
|  | ||||
| 		if (mAppConfigsUtil.isEnableMainService(true)) { | ||||
| 			run(); // 启动服务核心逻辑 | ||||
| 		} | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 服务核心逻辑(启动前台服务、初始化GPS、加载数据等) | ||||
| @@ -617,14 +621,6 @@ public class MainService extends Service { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 计算可见位置距离(原逻辑保留,Java 7 语法兼容,无Stream/并行流) | ||||
|      */ | ||||
|     private void calculateVisiblePositionDistance() { | ||||
|         // 原有逻辑(Java 7 语法适配:用迭代器/基础循环,无Lambda/forEach) | ||||
|         // 注:原代码标注“略”,此处保持空实现,实际使用时补充具体逻辑 | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 计算两点间距离(Haversine公式,纯Java 7 基础API,无数学工具类依赖) | ||||
|      * @param gpsLat GPS纬度 | ||||
| @@ -719,7 +715,7 @@ public class MainService extends Service { | ||||
|             } | ||||
|  | ||||
| 			// 校验任务开始时间 | ||||
| 			if(task.getStartTime() > System.currentTimeMillis()) { | ||||
| 			if (task.getStartTime() > System.currentTimeMillis()) { | ||||
| 				continue; | ||||
| 			} | ||||
|  | ||||
| @@ -801,15 +797,10 @@ public class MainService extends Service { | ||||
|     @Override | ||||
|     public int onStartCommand(Intent intent, int flags, int startId) { | ||||
| 		boolean isSettingToEnable = intent.getBooleanExtra(EXTRA_IS_SETTING_TO_ENABLE, false); | ||||
| 		if(isSettingToEnable) { | ||||
| 			mAppConfigsUtil.setIsEnableMainService(true); | ||||
| 			mAppConfigsUtil.saveConfigs(); | ||||
| 		} else { | ||||
| 			mAppConfigsUtil.setIsEnableMainService(false); | ||||
| 			mAppConfigsUtil.saveConfigs(); | ||||
| 		if (isSettingToEnable) { | ||||
| 			run(); // 重启服务核心逻辑(保证服务启动后进入运行状态) | ||||
| 		} | ||||
|  | ||||
|         run(); // 重启服务核心逻辑(保证服务启动后进入运行状态) | ||||
|         // 如果被设置为自启动就返回START_STICKY:服务被异常杀死后,系统会尝试重启(原逻辑保留) | ||||
| 		// 否则就启动默认参数 | ||||
|         return isSettingToEnable ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import android.app.ActivityManager; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import cc.winboll.studio.positions.services.AssistantService; | ||||
| import cc.winboll.studio.positions.services.MainService; | ||||
| import java.util.List; | ||||
|  | ||||
| @@ -36,19 +37,33 @@ public class ServiceUtil { | ||||
|     } | ||||
|  | ||||
| 	public static void stopAutoService(Context context) { | ||||
| 		Intent intent = new Intent(context, MainService.class); | ||||
| 		intent.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, false); | ||||
| 		context.stopService(intent); // 先停止旧服务 | ||||
| 		context.startService(intent); // 传入新的启动标志位,返回给系统 | ||||
| 		context.stopService(intent); // 再次关闭服务 | ||||
| 		AppConfigsUtil appConfigsUtil = AppConfigsUtil.getInstance(context); | ||||
| 		appConfigsUtil.setIsEnableMainService(false); | ||||
| 		appConfigsUtil.saveConfigs(); | ||||
| 		// 关闭并设置主服务 | ||||
| 		Intent intent1 = new Intent(context, MainService.class); | ||||
| 		intent1.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, false); | ||||
| 		context.stopService(intent1); // 先停止旧服务 | ||||
| 		context.startService(intent1); // 传入新的启动标志位,返回给系统 | ||||
| 		// 关闭并设置主服务守护进程 | ||||
| 		Intent intent2 = new Intent(context, AssistantService.class); | ||||
| 		intent2.putExtra(AssistantService.EXTRA_IS_SETTING_TO_ENABLE, false); | ||||
| 		context.stopService(intent2); // 先停止旧服务 | ||||
| 		context.startService(intent2); // 传入新的启动标志位,返回给系统 | ||||
| 		// 再次关闭所有服务 | ||||
| 		context.stopService(intent1); | ||||
| 		context.stopService(intent2); | ||||
|  | ||||
| 		LogUtils.d(TAG, "stopAutoService"); | ||||
| 	} | ||||
|  | ||||
| 	public static void startAutoService(Context context) { | ||||
| 		AppConfigsUtil appConfigsUtil = AppConfigsUtil.getInstance(context); | ||||
| 		appConfigsUtil.setIsEnableMainService(true); | ||||
| 		appConfigsUtil.saveConfigs(); | ||||
| 		Intent intent = new Intent(context, MainService.class); | ||||
| 		intent.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, true); | ||||
| 		context.stopService(intent); // 先停止旧服务 | ||||
| 		context.startService(intent); // 传入新的启动标志位,返回给系统 | ||||
| 		context.startService(intent); | ||||
| 		LogUtils.d(TAG, "startAutoService"); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen