优化服务启动设置方案
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sat Oct 25 18:29:48 HKT 2025
|
||||
#Mon Oct 27 01:09:02 GMT 2025
|
||||
stageCount=14
|
||||
libraryProject=
|
||||
baseVersion=15.0
|
||||
publishVersion=15.0.13
|
||||
buildCount=0
|
||||
buildCount=4
|
||||
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);
|
||||
|
||||
@@ -42,6 +42,8 @@ import java.util.concurrent.TimeUnit; // 新增:定时器时间单位依赖
|
||||
public class MainService extends Service {
|
||||
|
||||
public static final String TAG = "MainService";
|
||||
|
||||
public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_ENABLE";
|
||||
|
||||
// ---------------------- 新增:定时器相关变量 ----------------------
|
||||
private ScheduledExecutorService taskCheckTimer; // 任务校验定时器
|
||||
@@ -798,9 +800,19 @@ 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();
|
||||
}
|
||||
|
||||
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,9 @@ 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.MainService;
|
||||
import java.util.List;
|
||||
|
||||
public class ServiceUtil {
|
||||
@@ -31,4 +34,21 @@ public class ServiceUtil {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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); // 再次关闭服务
|
||||
LogUtils.d(TAG, "stopAutoService");
|
||||
}
|
||||
|
||||
public static void startAutoService(Context context) {
|
||||
Intent intent = new Intent(context, MainService.class);
|
||||
intent.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, true);
|
||||
context.stopService(intent); // 先停止旧服务
|
||||
context.startService(intent); // 传入新的启动标志位,返回给系统
|
||||
LogUtils.d(TAG, "startAutoService");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user