Compare commits
	
		
			2 Commits
		
	
	
		
			5646b589e0
			...
			cec2e82550
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | cec2e82550 | ||
|   | ac5f425624 | 
| @@ -1,8 +1,8 @@ | |||||||
| #Created by .winboll/winboll_app_build.gradle | #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 | stageCount=14 | ||||||
| libraryProject= | libraryProject= | ||||||
| baseVersion=15.0 | baseVersion=15.0 | ||||||
| publishVersion=15.0.13 | publishVersion=15.0.13 | ||||||
| buildCount=0 | buildCount=13 | ||||||
| baseBetaVersion=15.0.14 | 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.activities.WinBoLLActivity; | ||||||
| import cc.winboll.studio.positions.services.MainService; | import cc.winboll.studio.positions.services.MainService; | ||||||
| import cc.winboll.studio.positions.utils.AppConfigsUtil; | 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) { | 					if (isChecked) { | ||||||
| 						LogUtils.d(TAG, "设置启动服务"); | 						LogUtils.d(TAG, "设置启动服务"); | ||||||
| 						AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(true); | 						ServiceUtil.startAutoService(MainActivity.this); | ||||||
| 						// 启动服务(startService确保服务独立运行,不受Activity绑定影响) |  | ||||||
| 						startService(new Intent(MainActivity.this, MainService.class)); |  | ||||||
| 					} else { | 					} else { | ||||||
| 						LogUtils.d(TAG, "设置关闭服务"); | 						LogUtils.d(TAG, "设置关闭服务"); | ||||||
| 						AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(false); |  | ||||||
| 						// 停止服务前先解绑,避免服务被Activity持有 | 						ServiceUtil.stopAutoService(MainActivity.this); | ||||||
| //						if (isServiceBound) { |  | ||||||
| //							unbindService(mServiceConn); |  | ||||||
| //							isServiceBound = false; |  | ||||||
| //						} |  | ||||||
| 						stopService(new Intent(MainActivity.this, MainService.class)); |  | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					mManagePositionsButton.setEnabled(isChecked); | 					mManagePositionsButton.setEnabled(isChecked); | ||||||
|   | |||||||
| @@ -18,7 +18,8 @@ import cc.winboll.studio.positions.utils.ServiceUtil; | |||||||
| public class AssistantService extends Service { | public class AssistantService extends Service { | ||||||
|  |  | ||||||
|     public final static String TAG = "AssistantService"; |     public final static String TAG = "AssistantService"; | ||||||
|  | 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE"; | ||||||
|  | 	 | ||||||
|     MyServiceConnection mMyServiceConnection; |     MyServiceConnection mMyServiceConnection; | ||||||
|     volatile boolean mIsServiceRunning; |     volatile boolean mIsServiceRunning; | ||||||
|     AppConfigsUtil mAppConfigsUtil; |     AppConfigsUtil mAppConfigsUtil; | ||||||
| @@ -37,13 +38,18 @@ public class AssistantService extends Service { | |||||||
|         } |         } | ||||||
|         // 设置运行参数 |         // 设置运行参数 | ||||||
|         mIsServiceRunning = false; |         mIsServiceRunning = false; | ||||||
|         run(); |         if (mAppConfigsUtil.isEnableMainService(true)) { | ||||||
|  | 			run(); | ||||||
|  | 		} | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public int onStartCommand(Intent intent, int flags, int startId) { |     public int onStartCommand(Intent intent, int flags, int startId) { | ||||||
|         run(); | 		if (mAppConfigsUtil.isEnableMainService(true)) { | ||||||
|         return START_STICKY; | 			run(); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  |         return  mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|   | |||||||
| @@ -43,6 +43,8 @@ public class MainService extends Service { | |||||||
|  |  | ||||||
|     public static final String TAG = "MainService"; |     public static final String TAG = "MainService"; | ||||||
|  |  | ||||||
|  | 	public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE"; | ||||||
|  |  | ||||||
|     // ---------------------- 新增:定时器相关变量 ---------------------- |     // ---------------------- 新增:定时器相关变量 ---------------------- | ||||||
|     private ScheduledExecutorService taskCheckTimer; // 任务校验定时器 |     private ScheduledExecutorService taskCheckTimer; // 任务校验定时器 | ||||||
|     private static final long TASK_CHECK_INTERVAL = 1; // 定时间隔(1分钟) |     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) { |     public static synchronized MainService getInstance(Context context) { | ||||||
|         if (sInstance == null) { |         if (sInstance == null) { | ||||||
|             Intent intent = new Intent(context.getApplicationContext(), MainService.class); | 			if (AppConfigsUtil.getInstance(context).isEnableMainService(true)) { | ||||||
|             context.getApplicationContext().startService(intent); | 				Intent intent = new Intent(context.getApplicationContext(), MainService.class); | ||||||
|  | 				context.getApplicationContext().startService(intent); | ||||||
|  | 			} | ||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|         if (sAppContext == null) { |         if (sAppContext == null) { | ||||||
| @@ -363,7 +367,9 @@ public class MainService extends Service { | |||||||
|             mMyServiceConnection = new MyServiceConnection(); |             mMyServiceConnection = new MyServiceConnection(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         run(); // 启动服务核心逻辑 | 		if (mAppConfigsUtil.isEnableMainService(true)) { | ||||||
|  | 			run(); // 启动服务核心逻辑 | ||||||
|  | 		} | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -615,14 +621,6 @@ public class MainService extends Service { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 计算可见位置距离(原逻辑保留,Java 7 语法兼容,无Stream/并行流) |  | ||||||
|      */ |  | ||||||
|     private void calculateVisiblePositionDistance() { |  | ||||||
|         // 原有逻辑(Java 7 语法适配:用迭代器/基础循环,无Lambda/forEach) |  | ||||||
|         // 注:原代码标注“略”,此处保持空实现,实际使用时补充具体逻辑 |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 计算两点间距离(Haversine公式,纯Java 7 基础API,无数学工具类依赖) |      * 计算两点间距离(Haversine公式,纯Java 7 基础API,无数学工具类依赖) | ||||||
|      * @param gpsLat GPS纬度 |      * @param gpsLat GPS纬度 | ||||||
| @@ -717,7 +715,7 @@ public class MainService extends Service { | |||||||
|             } |             } | ||||||
|  |  | ||||||
| 			// 校验任务开始时间 | 			// 校验任务开始时间 | ||||||
| 			if(task.getStartTime() > System.currentTimeMillis()) { | 			if (task.getStartTime() > System.currentTimeMillis()) { | ||||||
| 				continue; | 				continue; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -798,9 +796,14 @@ public class MainService extends Service { | |||||||
|      */ |      */ | ||||||
|     @Override |     @Override | ||||||
|     public int onStartCommand(Intent intent, int flags, int startId) { |     public int onStartCommand(Intent intent, int flags, int startId) { | ||||||
|         run(); // 重启服务核心逻辑(保证服务启动后进入运行状态) | 		boolean isSettingToEnable = intent.getBooleanExtra(EXTRA_IS_SETTING_TO_ENABLE, false); | ||||||
|         // 返回START_STICKY:服务被异常杀死后,系统会尝试重启(原逻辑保留) | 		if (isSettingToEnable) { | ||||||
|         return mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | 			run(); // 重启服务核心逻辑(保证服务启动后进入运行状态) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  |         // 如果被设置为自启动就返回START_STICKY:服务被异常杀死后,系统会尝试重启(原逻辑保留) | ||||||
|  | 		// 否则就启动默认参数 | ||||||
|  |         return isSettingToEnable ? Service.START_STICKY : super.onStartCommand(intent, flags, startId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|   | |||||||
| @@ -41,11 +41,11 @@ public class AppConfigsUtil { | |||||||
|         return sInstance; |         return sInstance; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| 	void loadConfigs() { | 	public void loadConfigs() { | ||||||
| 		mAppConfigsModel = AppConfigsModel.loadBean(mContext, AppConfigsModel.class); | 		mAppConfigsModel = AppConfigsModel.loadBean(mContext, AppConfigsModel.class); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	void saveConfigs() { | 	public void saveConfigs() { | ||||||
| 		AppConfigsModel.saveBean(mContext, mAppConfigsModel); | 		AppConfigsModel.saveBean(mContext, mAppConfigsModel); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,6 +7,10 @@ package cc.winboll.studio.positions.utils; | |||||||
|  */ |  */ | ||||||
| import android.app.ActivityManager; | import android.app.ActivityManager; | ||||||
| import android.content.Context; | 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; | import java.util.List; | ||||||
|  |  | ||||||
| public class ServiceUtil { | public class ServiceUtil { | ||||||
| @@ -31,4 +35,35 @@ public class ServiceUtil { | |||||||
|  |  | ||||||
|         return false; |         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