后台服务启动设置优化完成

This commit is contained in:
ZhanGSKen
2025-10-27 10:09:39 +08:00
parent ac5f425624
commit cec2e82550
4 changed files with 49 additions and 37 deletions

View File

@@ -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

View File

@@ -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;
run();
if (mAppConfigsUtil.isEnableMainService(true)) {
run();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
run();
return START_STICKY;
if (mAppConfigsUtil.isEnableMainService(true)) {
run();
}
return mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId);
}
@Override

View File

@@ -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) {
Intent intent = new Intent(context.getApplicationContext(), MainService.class);
context.getApplicationContext().startService(intent);
if (AppConfigsUtil.getInstance(context).isEnableMainService(true)) {
Intent intent = new Intent(context.getApplicationContext(), MainService.class);
context.getApplicationContext().startService(intent);
}
return null;
}
if (sAppContext == null) {
@@ -365,7 +367,9 @@ public class MainService extends Service {
mMyServiceConnection = new MyServiceConnection();
}
run(); // 启动服务核心逻辑
if (mAppConfigsUtil.isEnableMainService(true)) {
run(); // 启动服务核心逻辑
}
}
/**
@@ -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);

View File

@@ -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");
}
}