Compare commits
11 Commits
5646b589e0
...
positions
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d3cee1121 | |||
|
|
2be6d5e122 | ||
| 6376ff4ccf | |||
|
|
dd20060754 | ||
|
|
298b337392 | ||
| 377ec4de09 | |||
|
|
033da4d544 | ||
|
|
1398ffc064 | ||
| 247f3f9b49 | |||
|
|
cec2e82550 | ||
|
|
ac5f425624 |
@@ -62,7 +62,7 @@ dependencies {
|
||||
// 应用介绍页类库
|
||||
api 'io.github.medyo:android-about-page:2.0.0'
|
||||
// 吐司类库
|
||||
api 'com.github.getActivity:ToastUtils:10.5'
|
||||
//api 'com.github.getActivity:ToastUtils:10.5'
|
||||
// 网络连接类库
|
||||
api 'com.squareup.okhttp3:okhttp:4.4.1'
|
||||
// AndroidX 类库
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sat Oct 25 18:29:48 HKT 2025
|
||||
stageCount=14
|
||||
#Tue Oct 28 20:03:59 HKT 2025
|
||||
stageCount=18
|
||||
libraryProject=
|
||||
baseVersion=15.0
|
||||
publishVersion=15.0.13
|
||||
publishVersion=15.0.17
|
||||
buildCount=0
|
||||
baseBetaVersion=15.0.14
|
||||
baseBetaVersion=15.0.18
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cc.winboll.studio.positions">
|
||||
|
||||
<!-- 1. 声明GPS权限 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
|
||||
<!-- 前台服务权限(可选,提升后台定位稳定性,避免服务被回收) -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
@@ -61,6 +64,17 @@
|
||||
<service android:name=".services.AssistantService"/>
|
||||
|
||||
<service android:name=".services.DistanceRefreshService"/>
|
||||
|
||||
|
||||
<!-- 2. 注册运动状态Receiver -->
|
||||
<receiver
|
||||
android:name="cc.winboll.studio.positions.receivers.MotionStatusReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="cc.winboll.studio.positions.ACTION_MOTION_STATUS" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -22,9 +22,9 @@ import android.widget.HorizontalScrollView;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
import com.hjq.toast.style.WhiteToastStyle;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
@@ -41,7 +41,6 @@ import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
|
||||
public class App extends GlobalApplication {
|
||||
|
||||
@@ -58,8 +57,8 @@ public class App extends GlobalApplication {
|
||||
ToastUtils.init(this);
|
||||
// 设置 Toast 布局样式
|
||||
//ToastUtils.setView(R.layout.view_toast);
|
||||
ToastUtils.setStyle(new WhiteToastStyle());
|
||||
ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
//ToastUtils.setStyle(new WhiteToastStyle());
|
||||
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
|
||||
//CrashHandler.getInstance().registerGlobal(this);
|
||||
//CrashHandler.getInstance().registerPart(this);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package cc.winboll.studio.positions.receivers;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/10/28 19:07
|
||||
* @Describe MotionStatusReceiver
|
||||
*/
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
|
||||
/**
|
||||
* 运动状态监听Receiver
|
||||
* 功能:接收运动状态广播,控制GPS权限申请与GPS监听开关
|
||||
*/
|
||||
public class MotionStatusReceiver extends BroadcastReceiver {
|
||||
public static final String TAG = "MotionStatusReceiver";
|
||||
|
||||
// 运动状态广播Action(需与运动状态发送方保持一致,如传感器服务)
|
||||
public static final String ACTION_MOTION_STATUS = "cc.winboll.studio.positions.ACTION_MOTION_STATUS";
|
||||
// 运动状态Extra键:0=静止/低运动,1=行走/高运动
|
||||
public static final String EXTRA_MOTION_STATUS = "EXTRA_MOTION_STATUS";
|
||||
// 静止时GPS定时获取间隔(单位:分钟,可配置)
|
||||
public static final long GPS_STATIC_INTERVAL = 1;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (context == null || intent == null || !TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS)) {
|
||||
LogUtils.w(TAG, "无效广播:Action不匹配或上下文为空");
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 获取运动状态(0=静止/低运动,1=行走/高运动)
|
||||
int motionStatus = intent.getIntExtra(EXTRA_MOTION_STATUS, 0);
|
||||
LogUtils.d(TAG, "接收运动状态:" + (motionStatus == 1 ? "行走中" : "静止/低运动"));
|
||||
|
||||
// 2. 绑定并获取MainService实例(确保服务已启动)
|
||||
MainService mainService = getMainService(context);
|
||||
if (mainService == null) {
|
||||
LogUtils.e(TAG, "MainService未启动,无法控制GPS状态");
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 根据运动状态处理GPS逻辑
|
||||
if (motionStatus == 1) {
|
||||
// 3.1 行走中:申请GPS权限(若未授予)+ 开启持续GPS监听
|
||||
handleWalkingStatus(mainService, context);
|
||||
} else {
|
||||
// 3.2 静止/低运动:关闭持续GPS监听 + 启动定时GPS获取
|
||||
handleStaticStatus(mainService);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理行走状态:申请GPS权限+开启持续GPS监听
|
||||
*/
|
||||
private void handleWalkingStatus(MainService mainService, Context context) {
|
||||
// 检查GPS权限(Android 6.0+动态权限)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||
context.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
// 发送权限申请广播(由Activity接收并发起申请,Receiver无法直接申请权限)
|
||||
Intent permissionIntent = new Intent("cc.winboll.studio.positions.ACTION_REQUEST_GPS_PERMISSION");
|
||||
permissionIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
context.sendBroadcast(permissionIntent);
|
||||
LogUtils.d(TAG, "行走中:GPS权限未授予,已发送权限申请广播");
|
||||
return;
|
||||
}
|
||||
|
||||
// 权限已授予:开启持续GPS监听(调用MainService原有方法)
|
||||
if (!mainService.isGpsListening()) { // 需在MainService中新增isGpsListening()方法
|
||||
mainService.startGpsLocation();
|
||||
LogUtils.d(TAG, "行走中:已开启持续GPS监听");
|
||||
}
|
||||
|
||||
// 停止静止时的GPS定时任务(避免重复获取)
|
||||
mainService.stopGpsStaticTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理静止状态:关闭持续GPS监听+启动定时GPS获取
|
||||
*/
|
||||
private void handleStaticStatus(MainService mainService) {
|
||||
// 关闭持续GPS监听(避免耗电)
|
||||
if (mainService.isGpsListening()) {
|
||||
mainService.stopGpsLocation();
|
||||
LogUtils.d(TAG, "静止中:已关闭持续GPS监听");
|
||||
}
|
||||
|
||||
// 启动定时GPS获取(获取一次后关闭,间隔GPS_STATIC_INTERVAL分钟)
|
||||
mainService.startGpsStaticTimer(GPS_STATIC_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MainService实例(通过绑定服务或单例,确保线程安全)
|
||||
*/
|
||||
private MainService getMainService(Context context) {
|
||||
// 方式1:若MainService单例有效,直接获取(推荐)
|
||||
MainService singleton = MainService.getInstance(context);
|
||||
if (singleton != null && singleton.isServiceRunning()) {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
// 方式2:若单例无效,尝试绑定服务(备用,需处理绑定回调)
|
||||
if (!ServiceUtil.isServiceAlive(context, MainService.class.getName())) {
|
||||
// 启动服务(若未运行)
|
||||
context.startService(new Intent(context, MainService.class));
|
||||
// 等待服务启动(短延时,实际项目建议用ServiceConnection异步绑定)
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return MainService.getInstance(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ 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;
|
||||
AppConfigsUtil mAppConfigsUtil;
|
||||
@@ -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
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.models.PositionModel;
|
||||
import cc.winboll.studio.positions.models.PositionTaskModel;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/10/27 18:40
|
||||
* @Describe 距离计算工具集(单例模式)
|
||||
*/
|
||||
public class DistanceCalculatorUtil {
|
||||
|
||||
public static final String TAG = "DistanceCalculatorUtil";
|
||||
|
||||
// 1. 私有静态 volatile 实例:保证多线程下实例可见性,避免指令重排序导致的空指针
|
||||
private static volatile DistanceCalculatorUtil sInstance;
|
||||
Context mContext;
|
||||
ArrayList<PositionModel> mPositionList; // 位置数据列表
|
||||
ArrayList<PositionTaskModel> mAllTasks;// 任务数据列表
|
||||
PositionModel mGpsPositionCalculated;
|
||||
long mLastCalculatedTime = 0;
|
||||
long mMinCalculatedTimeBettween = 30000; // GPS数据更新时,两次计算之间的最小时间间隔
|
||||
double mMinjumpDistance = 10.0f; // GPS数据更新时,能跳跃距离最小有效值,达到有效值时,两次计算之间的最小时间间隔阀值将被忽略。
|
||||
|
||||
// 2. 私有构造器:禁止外部通过 new 关键字创建实例,确保单例唯一性
|
||||
private DistanceCalculatorUtil(Context context) {
|
||||
// 可选:初始化工具类依赖的资源(如配置参数、缓存等)
|
||||
mContext = context;
|
||||
|
||||
LogUtils.d(TAG, "DistanceCalculatorUtil 单例实例初始化");
|
||||
}
|
||||
|
||||
// 3. 公开静态方法:双重校验锁获取单例,兼顾线程安全与性能
|
||||
public static DistanceCalculatorUtil getInstance(Context context) {
|
||||
// 第一重校验:避免已创建实例时的频繁加锁,提升性能
|
||||
if (sInstance == null) {
|
||||
// 加锁:确保多线程下仅一个线程进入实例创建逻辑
|
||||
synchronized (DistanceCalculatorUtil.class) {
|
||||
// 第二重校验:防止多线程并发时重复创建实例
|
||||
if (sInstance == null) {
|
||||
sInstance = new DistanceCalculatorUtil(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
// ---------------------- 以下可补充距离计算相关工具方法 ----------------------
|
||||
/**
|
||||
* 示例:Haversine 公式计算两点间距离(单位:米)
|
||||
* @param lat1 第一点纬度
|
||||
* @param lon1 第一点经度
|
||||
* @param lat2 第二点纬度
|
||||
* @param lon2 第二点经度
|
||||
* @return 两点间直线距离(米),计算失败返回 -1
|
||||
*/
|
||||
public static double calculateHaversineDistance(double lat1, double lon1, double lat2, double lon2) {
|
||||
try {
|
||||
final double EARTH_RADIUS = 6371000; // 地球半径(米)
|
||||
// 角度转弧度
|
||||
double latDiff = Math.toRadians(lat2 - lat1);
|
||||
double lonDiff = Math.toRadians(lon2 - lon1);
|
||||
|
||||
// Haversine 核心公式
|
||||
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2)
|
||||
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
|
||||
* Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);
|
||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
return EARTH_RADIUS * c; // 返回距离(米)
|
||||
} catch (Exception e) {
|
||||
LogUtils.d(TAG, "Haversine 距离计算失败:" + e.getMessage());
|
||||
return -1; // 标记计算失败
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算两点间距离(Haversine公式,纯Java 7 基础API,无数学工具类依赖)
|
||||
* @param gpsLat GPS纬度
|
||||
* @param gpsLon GPS经度
|
||||
* @param posLat 目标位置纬度
|
||||
* @param posLon 目标位置经度
|
||||
* @return 两点间距离(单位:米)
|
||||
*/
|
||||
/*private double calculateHaversineDistance(double gpsLat, double gpsLon, double posLat, double posLon) {
|
||||
final double EARTH_RADIUS = 6371000; // 地球半径(米)
|
||||
double latDiff = Math.toRadians(posLat - gpsLat);
|
||||
double lonDiff = Math.toRadians(posLon - gpsLon);
|
||||
// Haversine公式核心计算(Java 7 基础数学方法)
|
||||
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2)
|
||||
+ Math.cos(Math.toRadians(gpsLat)) * Math.cos(Math.toRadians(posLat))
|
||||
* Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);
|
||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return EARTH_RADIUS * c;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 校验所有任务触发条件(距离达标则触发任务通知)
|
||||
*/
|
||||
public void checkAllTaskTriggerCondition(PositionModel currentGpsPosition) {
|
||||
if (currentGpsPosition == null) {
|
||||
LogUtils.d(TAG, "传入坐标参数为空,退出函数。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算频率控制模块
|
||||
//
|
||||
// 计算与最近一次GPS计算的时间间隔
|
||||
long nCalculatedTimeBettween = System.currentTimeMillis() - mLastCalculatedTime;
|
||||
// 计算跳跃距离
|
||||
double gpsPositionCalculatedLatitude = mGpsPositionCalculated == null ?0.0f: mGpsPositionCalculated.getLatitude();
|
||||
double gpsPositionCalculatedLongitude = mGpsPositionCalculated == null ?0.0f: mGpsPositionCalculated.getLongitude();
|
||||
double jumpDistance = calculateHaversineDistance(gpsPositionCalculatedLatitude, gpsPositionCalculatedLongitude, currentGpsPosition.getLatitude(), currentGpsPosition.getLongitude());
|
||||
if (jumpDistance < mMinjumpDistance) {
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,小于50米。", jumpDistance));
|
||||
// 跳跃距离小于最小有效跳跃值
|
||||
if (nCalculatedTimeBettween < mMinCalculatedTimeBettween) {
|
||||
//间隔小于最小时间间隔设定
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:与最近一次计算间隔时间%d,坐标变化忽略。", nCalculatedTimeBettween));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mGpsPositionCalculated == null) {
|
||||
mGpsPositionCalculated = currentGpsPosition;
|
||||
LogUtils.d(TAG, "最后计算位置记录为空,现在使用新坐标为初始化。");
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,与上次计算间隔%d,现在启动任务数据计算。", jumpDistance, nCalculatedTimeBettween));
|
||||
|
||||
// 获取位置任务基础数据
|
||||
MainService mainService = MainService.getInstance(mContext);
|
||||
mPositionList = mainService.getPositionList();
|
||||
mAllTasks = mainService.getAllTasks();
|
||||
|
||||
// 位置数据为空,跳过校验。
|
||||
if (mPositionList.isEmpty()) {
|
||||
LogUtils.d(TAG, "checkAllTaskTriggerCondition:位置数据为空,跳过距离计算。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新所有位置点的位置距离数据
|
||||
refreshRealPositionDistance(currentGpsPosition);
|
||||
|
||||
// 任务数据为空,跳过校验。
|
||||
if (mAllTasks.isEmpty()) {
|
||||
LogUtils.d(TAG, "checkAllTaskTriggerCondition:任务数据为空,跳过任务提醒检查计算。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 迭代器遍历任务(Java 7 安全遍历,避免并发修改异常)
|
||||
Iterator<PositionTaskModel> taskIter = mAllTasks.iterator();
|
||||
while (taskIter.hasNext()) {
|
||||
PositionTaskModel task = taskIter.next();
|
||||
// 仅校验“已启用”且“绑定有效位置”的任务
|
||||
if (!task.isEnable() || TextUtils.isEmpty(task.getPositionId())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查找任务绑定的位置(Java 7 迭代器遍历位置列表)
|
||||
PositionModel bindPos = null;
|
||||
Iterator<PositionModel> posIter = mPositionList.iterator();
|
||||
while (posIter.hasNext()) {
|
||||
PositionModel pos = posIter.next();
|
||||
if (task.getPositionId().equals(pos.getPositionId())) {
|
||||
bindPos = pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bindPos == null) {
|
||||
LogUtils.w(TAG, "任务ID=" + task.getTaskId() + ":绑定位置不存在,跳过");
|
||||
task.setIsBingo(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 校验任务开始时间
|
||||
if (task.getStartTime() > System.currentTimeMillis()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 校验距离条件(判断是否满足任务触发阈值)
|
||||
double currentDistance = bindPos.getRealPositionDistance();
|
||||
if (currentDistance < 0) {
|
||||
LogUtils.w(TAG, "任务ID=" + task.getTaskId() + ":距离计算失败,跳过");
|
||||
task.setIsBingo(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isTriggered = false;
|
||||
int taskDistance = task.getDiscussDistance();
|
||||
// 任务触发条件:大于/小于指定距离(Java 7 基础判断,无三元运算符嵌套)
|
||||
if (task.isGreaterThan()) {
|
||||
isTriggered = currentDistance > taskDistance;
|
||||
} else if (task.isLessThan()) {
|
||||
isTriggered = currentDistance < taskDistance;
|
||||
}
|
||||
|
||||
// 更新任务触发状态+发送通知(状态变化时才处理)
|
||||
if (task.isBingo() != isTriggered) {
|
||||
task.setIsBingo(isTriggered);
|
||||
if (isTriggered) {
|
||||
MainService.getInstance(mContext).sendTaskTriggerNotification(task, bindPos, currentDistance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MainService.getInstance(mContext).saveAllTasks(); // 持久化更新后的任务状态
|
||||
// 记录最后坐标更新点
|
||||
mGpsPositionCalculated = currentGpsPosition;
|
||||
// 记录数据计算时间
|
||||
mLastCalculatedTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 强制刷新所有位置距离(GPS更新后调用,计算距离+校验任务触发条件)
|
||||
*/
|
||||
public void refreshRealPositionDistance(PositionModel currentGpsPosition) {
|
||||
// 遍历所有位置计算距离(Java 7 增强for循环,无Stream)
|
||||
for (PositionModel pos : mPositionList) {
|
||||
if (pos.isEnableRealPositionDistance()) {
|
||||
double distance = DistanceCalculatorUtil.calculateHaversineDistance(
|
||||
currentGpsPosition.getLatitude(),
|
||||
currentGpsPosition.getLongitude(),
|
||||
pos.getLatitude(),
|
||||
pos.getLongitude()
|
||||
);
|
||||
pos.setRealPositionDistance(distance);
|
||||
} else {
|
||||
pos.setRealPositionDistance(-1); // 未启用距离计算,标记为无效
|
||||
}
|
||||
}
|
||||
|
||||
// 距离刷新后通知GPS监听者
|
||||
MainService.getInstance(mContext).notifyAllGpsListeners(currentGpsPosition);
|
||||
}
|
||||
}
|
||||
@@ -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