Compare commits
	
		
			2 Commits
		
	
	
		
			5646b589e0
			...
			cec2e82550
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | cec2e82550 | ||
|   | ac5f425624 | 
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Sat Oct 25 18:29:48 HKT 2025 | ||||
| #Mon Oct 27 02:06:36 GMT 2025 | ||||
| stageCount=14 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.13 | ||||
| buildCount=0 | ||||
| buildCount=13 | ||||
| baseBetaVersion=15.0.14 | ||||
|   | ||||
| @@ -21,6 +21,7 @@ import cc.winboll.studio.positions.activities.LocationActivity; | ||||
| import cc.winboll.studio.positions.activities.WinBoLLActivity; | ||||
| import cc.winboll.studio.positions.services.MainService; | ||||
| import cc.winboll.studio.positions.utils.AppConfigsUtil; | ||||
| import cc.winboll.studio.positions.utils.ServiceUtil; | ||||
|  | ||||
| /** | ||||
|  * 主页面:仅负责 | ||||
| @@ -141,18 +142,11 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity { | ||||
| 					// 权限就绪:执行服务启停逻辑 | ||||
| 					if (isChecked) { | ||||
| 						LogUtils.d(TAG, "设置启动服务"); | ||||
| 						AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(true); | ||||
| 						// 启动服务(startService确保服务独立运行,不受Activity绑定影响) | ||||
| 						startService(new Intent(MainActivity.this, MainService.class)); | ||||
| 						ServiceUtil.startAutoService(MainActivity.this); | ||||
| 					} else { | ||||
| 						LogUtils.d(TAG, "设置关闭服务"); | ||||
| 						AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(false); | ||||
| 						// 停止服务前先解绑,避免服务被Activity持有 | ||||
| //						if (isServiceBound) { | ||||
| //							unbindService(mServiceConn); | ||||
| //							isServiceBound = false; | ||||
| //						} | ||||
| 						stopService(new Intent(MainActivity.this, MainService.class)); | ||||
|  | ||||
| 						ServiceUtil.stopAutoService(MainActivity.this); | ||||
| 					} | ||||
|  | ||||
| 					mManagePositionsButton.setEnabled(isChecked); | ||||
|   | ||||
| @@ -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,6 +43,8 @@ public class MainService extends Service { | ||||
|  | ||||
|     public static final String TAG = "MainService"; | ||||
|  | ||||
| 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE"; | ||||
|  | ||||
|     // ---------------------- 新增:定时器相关变量 ---------------------- | ||||
|     private ScheduledExecutorService taskCheckTimer; // 任务校验定时器 | ||||
|     private static final long TASK_CHECK_INTERVAL = 1; // 定时间隔(1分钟) | ||||
| @@ -323,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) { | ||||
| @@ -363,8 +367,10 @@ public class MainService extends Service { | ||||
|             mMyServiceConnection = new MyServiceConnection(); | ||||
|         } | ||||
|  | ||||
| 		if (mAppConfigsUtil.isEnableMainService(true)) { | ||||
| 			run(); // 启动服务核心逻辑 | ||||
| 		} | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 服务核心逻辑(启动前台服务、初始化GPS、加载数据等) | ||||
| @@ -615,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纬度 | ||||
| @@ -717,7 +715,7 @@ public class MainService extends Service { | ||||
|             } | ||||
|  | ||||
| 			// 校验任务开始时间 | ||||
| 			if(task.getStartTime() > System.currentTimeMillis()) { | ||||
| 			if (task.getStartTime() > System.currentTimeMillis()) { | ||||
| 				continue; | ||||
| 			} | ||||
|  | ||||
| @@ -798,9 +796,14 @@ 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) { | ||||
| 			run(); // 重启服务核心逻辑(保证服务启动后进入运行状态) | ||||
|         // 返回START_STICKY:服务被异常杀死后,系统会尝试重启(原逻辑保留) | ||||
|         return mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||
| 		} | ||||
|  | ||||
|         // 如果被设置为自启动就返回START_STICKY:服务被异常杀死后,系统会尝试重启(原逻辑保留) | ||||
| 		// 否则就启动默认参数 | ||||
|         return isSettingToEnable ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -41,11 +41,11 @@ public class AppConfigsUtil { | ||||
|         return sInstance; | ||||
|     } | ||||
|  | ||||
| 	void loadConfigs() { | ||||
| 	public void loadConfigs() { | ||||
| 		mAppConfigsModel = AppConfigsModel.loadBean(mContext, AppConfigsModel.class); | ||||
| 	} | ||||
|  | ||||
| 	void saveConfigs() { | ||||
| 	public void saveConfigs() { | ||||
| 		AppConfigsModel.saveBean(mContext, mAppConfigsModel); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -7,6 +7,10 @@ package cc.winboll.studio.positions.utils; | ||||
|  */ | ||||
| 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; | ||||
|  | ||||
| public class ServiceUtil { | ||||
| @@ -31,4 +35,35 @@ public class ServiceUtil { | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
| 	public static void stopAutoService(Context context) { | ||||
| 		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.startService(intent); | ||||
| 		LogUtils.d(TAG, "startAutoService"); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user