Compare commits
4 Commits
positions-
...
positions
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf52f86b3d | ||
|
|
566e84f53f | ||
| 2d3cee1121 | |||
|
|
2be6d5e122 |
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Tue Oct 28 14:17:09 HKT 2025
|
#Wed Nov 05 08:47:36 GMT 2025
|
||||||
stageCount=17
|
stageCount=18
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.16
|
publishVersion=15.0.17
|
||||||
buildCount=0
|
buildCount=29
|
||||||
baseBetaVersion=15.0.17
|
baseBetaVersion=15.0.18
|
||||||
|
|||||||
@@ -3,21 +3,24 @@
|
|||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cc.winboll.studio.positions">
|
package="cc.winboll.studio.positions">
|
||||||
|
|
||||||
<!-- 前台服务权限(可选,提升后台定位稳定性,避免服务被回收) -->
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
||||||
|
|
||||||
<!-- 只能在前台获取精确的位置信息 -->
|
<!-- 只能在前台获取精确的位置信息 -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||||
|
|
||||||
<!-- 只有在前台运行时才能获取大致位置信息 -->
|
<!-- 只有在前台运行时才能获取大致位置信息 -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||||
|
|
||||||
<!-- 拥有完全的网络访问权限 -->
|
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
|
||||||
|
|
||||||
<!-- 在后台使用位置信息 -->
|
<!-- 在后台使用位置信息 -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||||
|
|
||||||
|
<!-- 运行前台服务 -->
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||||
|
|
||||||
|
<!-- 运行“location”类型的前台服务 -->
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
|
||||||
|
|
||||||
|
<!-- 拥有完全的网络访问权限 -->
|
||||||
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.hardware.location.gps"
|
android:name="android.hardware.location.gps"
|
||||||
android:required="false"/>
|
android:required="false"/>
|
||||||
@@ -56,11 +59,28 @@
|
|||||||
android:name="com.google.android.gms.version"
|
android:name="com.google.android.gms.version"
|
||||||
android:value="@integer/google_play_services_version"/>
|
android:value="@integer/google_play_services_version"/>
|
||||||
|
|
||||||
<service android:name=".services.MainService"/>
|
<service
|
||||||
|
android:name=".services.MainService"
|
||||||
|
android:exported="false"/>
|
||||||
|
|
||||||
<service android:name=".services.AssistantService"/>
|
<service
|
||||||
|
android:name=".services.AssistantService"
|
||||||
|
android:exported="false"/>
|
||||||
|
|
||||||
|
<service
|
||||||
|
android:name=".services.DistanceRefreshService"
|
||||||
|
android:exported="false"/>
|
||||||
|
|
||||||
|
<receiver android:name="cc.winboll.studio.positions.receivers.MotionStatusReceiver">
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
|
||||||
|
<action android:name="cc.winboll.studio.positions.receivers.MotionStatusReceiver"/>
|
||||||
|
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<service android:name=".services.DistanceRefreshService"/>
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -0,0 +1,361 @@
|
|||||||
|
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.hardware.Sensor;
|
||||||
|
import android.hardware.SensorEvent;
|
||||||
|
import android.hardware.SensorEventListener;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
import android.os.Build;
|
||||||
|
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.services.MainService;
|
||||||
|
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运动状态监听Receiver
|
||||||
|
* 功能:1.持续监听传感器(不关闭) 2.每5秒计算运动状态 3.按状态切换GPS模式(实时/30秒定时)
|
||||||
|
*/
|
||||||
|
public class MotionStatusReceiver extends BroadcastReceiver implements SensorEventListener {
|
||||||
|
public static final String TAG = "MotionStatusReceiver";
|
||||||
|
|
||||||
|
// 广播Action
|
||||||
|
public static final String ACTION_MOTION_STATUS_RECEIVER = "cc.winboll.studio.positions.receivers.MotionStatusReceiver";
|
||||||
|
public static final String EXTRA_SENSORS_ENABLE = "EXTRA_SENSORS_ENABLE";
|
||||||
|
// 传感器启动状态标志位
|
||||||
|
boolean mIsSensorsEnable = false;
|
||||||
|
// 运动状态常量
|
||||||
|
private static final int MOTION_STATUS_STATIC = 0; // 静止/低运动
|
||||||
|
private static final int MOTION_STATUS_WALKING = 1; // 行走/高速运动
|
||||||
|
// 配置参数(按需求调整)
|
||||||
|
private static final float ACCELEROMETER_THRESHOLD = 0.8f; // 加速度阈值
|
||||||
|
private static final float GYROSCOPE_THRESHOLD = 0.5f; // 陀螺仪阈值
|
||||||
|
private static final long STATUS_CALC_INTERVAL = 5000; // 运动状态计算间隔(5秒)
|
||||||
|
private static final long GPS_STATIC_INTERVAL = 30; // 静止时GPS间隔(30秒)
|
||||||
|
// 核心对象
|
||||||
|
private volatile SensorManager mSensorManager;
|
||||||
|
private Sensor mAccelerometer;
|
||||||
|
private Sensor mGyroscope;
|
||||||
|
private volatile boolean mIsSensorListening = false; // 传感器是否持续监听
|
||||||
|
private int mCurrentMotionStatus = MOTION_STATUS_STATIC; // 当前运动状态
|
||||||
|
private Handler mMainHandler; // 主线程Handler(用于定时计算)
|
||||||
|
private Context mBroadcastContext; // 广播上下文
|
||||||
|
// 传感器数据缓存(用于5秒内数据汇总,避免单次波动误判)
|
||||||
|
private float mAccelMax = 0f; // 5秒内加速度最大值
|
||||||
|
private float mGyroMax = 0f; // 5秒内陀螺仪最大值
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "===== 接收器启动:onReceive() 开始执行 =====");
|
||||||
|
this.mBroadcastContext = context;
|
||||||
|
mMainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
|
if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||||
|
boolean isSettingEnable = intent.getBooleanExtra(EXTRA_SENSORS_ENABLE, false);
|
||||||
|
if (mIsSensorsEnable == false && isSettingEnable == true) {
|
||||||
|
mIsSensorsEnable = true;
|
||||||
|
// 1. 初始化传感器(必执行)
|
||||||
|
initSensors();
|
||||||
|
|
||||||
|
|
||||||
|
if (mAccelerometer == null || mGyroscope == null) {
|
||||||
|
LogUtils.e(TAG, "设备缺少加速度/陀螺仪,无法持续监听");
|
||||||
|
cleanResources(false); // 传感器不可用才清理
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 校验参数
|
||||||
|
if (context == null || intent == null) {
|
||||||
|
LogUtils.d(TAG, "onReceive():无效参数,终止处理");
|
||||||
|
cleanResources(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogUtils.d(TAG, "onReceive():接收到广播Action=" + intent.getAction());
|
||||||
|
|
||||||
|
// 3. 启动持续传感器监听(核心:不关闭,重复调用无影响)
|
||||||
|
startSensorListening();
|
||||||
|
|
||||||
|
// 4. 启动5秒定时计算运动状态(核心:持续触发状态判断)
|
||||||
|
startStatusCalcTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 5. 处理外部广播触发(可选,保留外部控制能力)
|
||||||
|
// if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||||
|
// int motionStatus = intent.getIntExtra(EXTRA_MOTION_STATUS, MOTION_STATUS_STATIC);
|
||||||
|
// String statusDesc = motionStatus == MOTION_STATUS_WALKING ? "高速运动" : "静止/低运动";
|
||||||
|
// LogUtils.d(TAG, "外部广播触发,强制设置运动状态:" + statusDesc);
|
||||||
|
// mCurrentMotionStatus = motionStatus;
|
||||||
|
// handleMotionStatus(mCurrentMotionStatus); // 立即执行GPS切换
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化传感器(持续监听,复用实例)
|
||||||
|
*/
|
||||||
|
private void initSensors() {
|
||||||
|
LogUtils.d(TAG, "initSensors():初始化传感器");
|
||||||
|
if (mSensorManager != null || mBroadcastContext == null) return;
|
||||||
|
|
||||||
|
mSensorManager = (SensorManager) mBroadcastContext.getSystemService(Context.SENSOR_SERVICE);
|
||||||
|
if (mSensorManager == null) {
|
||||||
|
LogUtils.e(TAG, "设备不支持传感器服务");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 获取传感器实例(持续复用,不销毁)
|
||||||
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||||
|
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "传感器初始化结果:加速度=" + (mAccelerometer != null) + ",陀螺仪=" + (mGyroscope != null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动传感器持续监听(核心:不关闭,注册一次一直生效)
|
||||||
|
*/
|
||||||
|
private void startSensorListening() {
|
||||||
|
if (mSensorManager == null || mAccelerometer == null || mGyroscope == null) return;
|
||||||
|
|
||||||
|
if (!mIsSensorListening) {
|
||||||
|
// 注册传感器监听(持续生效,直到服务销毁才注销)
|
||||||
|
mSensorManager.registerListener(
|
||||||
|
this,
|
||||||
|
mAccelerometer,
|
||||||
|
SensorManager.SENSOR_DELAY_NORMAL, // 正常延迟,平衡性能与精度
|
||||||
|
mMainHandler
|
||||||
|
);
|
||||||
|
mSensorManager.registerListener(
|
||||||
|
this,
|
||||||
|
mGyroscope,
|
||||||
|
SensorManager.SENSOR_DELAY_NORMAL,
|
||||||
|
mMainHandler
|
||||||
|
);
|
||||||
|
mIsSensorListening = true;
|
||||||
|
LogUtils.d(TAG, "startSensorListening():传感器持续监听已启动(不关闭)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动5秒定时计算运动状态(核心:周期性汇总传感器数据)
|
||||||
|
*/
|
||||||
|
private void startStatusCalcTimer() {
|
||||||
|
if (mMainHandler == null) return;
|
||||||
|
|
||||||
|
// 移除旧任务(避免重复注册)
|
||||||
|
mMainHandler.removeCallbacks(mStatusCalcRunnable);
|
||||||
|
// 启动定时任务(每5秒执行一次)
|
||||||
|
mMainHandler.postDelayed(mStatusCalcRunnable, STATUS_CALC_INTERVAL);
|
||||||
|
LogUtils.d(TAG, "startStatusCalcTimer():5秒运动状态计算定时器已启动");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运动状态计算任务(5秒执行一次)
|
||||||
|
*/
|
||||||
|
private final Runnable mStatusCalcRunnable = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// 1. 基于5秒内缓存的最大传感器数据判断状态
|
||||||
|
boolean isHighMotion = (mAccelMax > ACCELEROMETER_THRESHOLD) && (mGyroMax > GYROSCOPE_THRESHOLD);
|
||||||
|
int newMotionStatus = isHighMotion ? MOTION_STATUS_WALKING : MOTION_STATUS_STATIC;
|
||||||
|
|
||||||
|
// 2. 状态变化时才处理(避免频繁切换GPS)
|
||||||
|
if (newMotionStatus != mCurrentMotionStatus) {
|
||||||
|
mCurrentMotionStatus = newMotionStatus;
|
||||||
|
String statusDesc = isHighMotion ? "高速运动" : "静止/低运动";
|
||||||
|
LogUtils.d(TAG, "运动状态更新(5秒计算):" + statusDesc
|
||||||
|
+ "(加速度最大值=" + mAccelMax + ",陀螺仪最大值=" + mGyroMax + ")");
|
||||||
|
handleMotionStatus(newMotionStatus); // 切换GPS模式
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "运动状态无变化(5秒计算):" + (isHighMotion ? "高速运动" : "静止/低运动"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 重置传感器数据缓存,准备下一个5秒周期
|
||||||
|
mAccelMax = 0f;
|
||||||
|
mGyroMax = 0f;
|
||||||
|
|
||||||
|
// 4. 循环执行定时任务(核心:持续计算)
|
||||||
|
mMainHandler.postDelayed(this, STATUS_CALC_INTERVAL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传感器数据变化回调(核心:实时缓存最大数据)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onSensorChanged(SensorEvent event) {
|
||||||
|
if (event == null) return;
|
||||||
|
|
||||||
|
// 实时缓存5秒内的最大传感器数据(避免单次波动误判)
|
||||||
|
switch (event.sensor.getType()) {
|
||||||
|
case Sensor.TYPE_ACCELEROMETER:
|
||||||
|
float accelTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||||
|
if (accelTotal > mAccelMax) mAccelMax = accelTotal; // 缓存最大值
|
||||||
|
LogUtils.d(TAG, "加速度传感器实时数据:合值=" + accelTotal + "(当前5秒最大值=" + mAccelMax + ")");
|
||||||
|
break;
|
||||||
|
case Sensor.TYPE_GYROSCOPE:
|
||||||
|
float gyroTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||||
|
if (gyroTotal > mGyroMax) mGyroMax = gyroTotal; // 缓存最大值
|
||||||
|
LogUtils.d(TAG, "陀螺仪实时数据:合值=" + gyroTotal + "(当前5秒最大值=" + mGyroMax + ")");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理运动状态(核心:按状态切换GPS模式)
|
||||||
|
*/
|
||||||
|
private void handleMotionStatus(int motionStatus) {
|
||||||
|
LogUtils.d(TAG, "handleMotionStatus():开始处理运动状态,切换GPS模式");
|
||||||
|
if (mBroadcastContext == null) {
|
||||||
|
LogUtils.w(TAG, "上下文为空,无法处理GPS");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MainService mainService = getMainService();
|
||||||
|
if (mainService == null) {
|
||||||
|
LogUtils.e(TAG, "MainService未启动,GPS控制失败");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (motionStatus == MOTION_STATUS_WALKING) {
|
||||||
|
// 高速运动:启动GPS实时更新(2秒/1米)
|
||||||
|
handleHighMotionGPS(mainService);
|
||||||
|
} else {
|
||||||
|
// 静止/低运动:启动GPS30秒定时更新
|
||||||
|
handleStaticGPS(mainService);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 高速运动GPS处理:实时更新
|
||||||
|
*/
|
||||||
|
private void handleHighMotionGPS(MainService mainService) {
|
||||||
|
// 动态权限判断(Android 6.0+)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||||
|
mBroadcastContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
sendPermissionRequestBroadcast();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动实时GPS(已启动则不重复操作)
|
||||||
|
if (!mainService.isGpsListening()) {
|
||||||
|
mainService.startGpsLocation(); // 实时更新(2秒/1米)
|
||||||
|
mainService.stopGpsStaticTimer(); // 停止定时GPS
|
||||||
|
LogUtils.d(TAG, "高速运动:已启动GPS实时更新");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静止/低运动GPS处理:30秒定时更新
|
||||||
|
*/
|
||||||
|
private void handleStaticGPS(MainService mainService) {
|
||||||
|
// 停止实时GPS(已停止则不重复操作)
|
||||||
|
if (mainService.isGpsListening()) {
|
||||||
|
mainService.stopGpsLocation(); // 停止实时更新
|
||||||
|
LogUtils.d(TAG, "静止/低运动:已停止GPS实时更新");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动30秒定时GPS(已启动则不重复操作)
|
||||||
|
mainService.startGpsStaticTimer(GPS_STATIC_INTERVAL); // 30秒一次
|
||||||
|
LogUtils.d(TAG, "静止/低运动:已启动GPS30秒定时更新");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取MainService实例(复用逻辑)
|
||||||
|
*/
|
||||||
|
private MainService getMainService() {
|
||||||
|
if (mBroadcastContext == null) return null;
|
||||||
|
|
||||||
|
// 优先获取单例
|
||||||
|
MainService singleton = MainService.getInstance(mBroadcastContext);
|
||||||
|
if (singleton != null && singleton.isServiceRunning()) {
|
||||||
|
return singleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动服务并重试
|
||||||
|
if (!ServiceUtil.isServiceAlive(mBroadcastContext, MainService.class.getName())) {
|
||||||
|
mBroadcastContext.startService(new Intent(mBroadcastContext, MainService.class));
|
||||||
|
try {
|
||||||
|
Thread.sleep(500); // 等待服务启动
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MainService.getInstance(mBroadcastContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送GPS权限申请广播(Receiver无法直接申请)
|
||||||
|
*/
|
||||||
|
private void sendPermissionRequestBroadcast() {
|
||||||
|
Intent permissionIntent = new Intent("cc.winboll.studio.positions.ACTION_REQUEST_GPS_PERMISSION");
|
||||||
|
permissionIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||||
|
mBroadcastContext.sendBroadcast(permissionIntent);
|
||||||
|
LogUtils.d(TAG, "GPS权限缺失,已发送申请广播");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资源清理(核心:传感器不关闭,仅清理Handler和上下文)
|
||||||
|
* @param isForceStopSensor 是否强制停止传感器(仅服务销毁时传true)
|
||||||
|
*/
|
||||||
|
private void cleanResources(boolean isForceStopSensor) {
|
||||||
|
// 1. 停止定时计算任务
|
||||||
|
if (mMainHandler != null) {
|
||||||
|
mMainHandler.removeCallbacksAndMessages(null);
|
||||||
|
mMainHandler = null;
|
||||||
|
LogUtils.d(TAG, "cleanResources():已停止运动状态计算定时器");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 强制停止传感器(仅当外部触发销毁时执行,正常情况不关闭)
|
||||||
|
if (isForceStopSensor && mSensorManager != null && mIsSensorListening) {
|
||||||
|
mSensorManager.unregisterListener(this);
|
||||||
|
mIsSensorListening = false;
|
||||||
|
LogUtils.d(TAG, "cleanResources():已强制停止传感器监听");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 置空上下文(避免内存泄漏)
|
||||||
|
mBroadcastContext = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传感器精度变化回调(日志监控)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||||
|
String sensorType = sensor.getType() == Sensor.TYPE_ACCELEROMETER ? "加速度" : "陀螺仪";
|
||||||
|
String accuracyDesc = getAccuracyDesc(accuracy);
|
||||||
|
LogUtils.d(TAG, sensorType + "传感器精度变化:" + accuracyDesc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传感器精度描述转换
|
||||||
|
*/
|
||||||
|
private String getAccuracyDesc(int accuracy) {
|
||||||
|
switch (accuracy) {
|
||||||
|
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: return "高";
|
||||||
|
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: return "中";
|
||||||
|
case SensorManager.SENSOR_STATUS_ACCURACY_LOW: return "低";
|
||||||
|
case SensorManager.SENSOR_STATUS_UNRELIABLE: return "不可靠";
|
||||||
|
default: return "未知";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 补充:Receiver销毁时强制清理(需在MainService注销时调用)
|
||||||
|
*/
|
||||||
|
public void forceCleanResources() {
|
||||||
|
cleanResources(true); // 强制停止传感器
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,168 @@
|
|||||||
|
package cc.winboll.studio.positions.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||||
|
* @Date 2025/11/05 15:49
|
||||||
|
* @Describe LocalMotionDetector
|
||||||
|
*/
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.Sensor;
|
||||||
|
import android.hardware.SensorEvent;
|
||||||
|
import android.hardware.SensorEventListener;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.util.Log;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本机运动状态监测工具(无联网,纯传感器)
|
||||||
|
*/
|
||||||
|
public class LocalMotionDetector implements SensorEventListener {
|
||||||
|
public static final String TAG = "LocalMotionDetector";
|
||||||
|
|
||||||
|
// 配置参数(重点修改:调高运动阈值,适配坐立持机场景)
|
||||||
|
private static final float MOTION_THRESHOLD = 1.8f; // 从0.5f调高到1.8f(过滤坐立轻微晃动)
|
||||||
|
private static final long STATUS_CHECK_INTERVAL = 3000; // 3秒判断一次状态
|
||||||
|
private static final int STEP_CHANGE_THRESHOLD = 2; // 3秒≥2步判定行走
|
||||||
|
|
||||||
|
private SensorManager mSensorManager;
|
||||||
|
private Sensor mAccelerometer;
|
||||||
|
private Sensor mStepCounter;
|
||||||
|
private Handler mMainHandler;
|
||||||
|
private MotionStatusCallback mCallback;
|
||||||
|
|
||||||
|
private boolean mIsDetecting = false;
|
||||||
|
private float mLastAccelMagnitude = 0f;
|
||||||
|
private int mLastStepCount = 0;
|
||||||
|
private int mCurrentStepCount = 0;
|
||||||
|
private boolean mIsWalking = false;
|
||||||
|
|
||||||
|
// 单例模式
|
||||||
|
private static LocalMotionDetector sInstance;
|
||||||
|
public static LocalMotionDetector getInstance() {
|
||||||
|
if (sInstance == null) {
|
||||||
|
synchronized (LocalMotionDetector.class) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new LocalMotionDetector();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalMotionDetector() {
|
||||||
|
mMainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始监测运动状态
|
||||||
|
*/
|
||||||
|
public void startDetection(Context context, MotionStatusCallback callback) {
|
||||||
|
if (mIsDetecting) return;
|
||||||
|
mCallback = callback;
|
||||||
|
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||||
|
|
||||||
|
// 初始化传感器
|
||||||
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||||
|
mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
|
||||||
|
|
||||||
|
// 注册传感器监听
|
||||||
|
if (mAccelerometer != null) {
|
||||||
|
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||||
|
}
|
||||||
|
if (mStepCounter != null) {
|
||||||
|
mSensorManager.registerListener(this, mStepCounter, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||||
|
LogUtils.d(TAG, "计步传感器已启动");
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "设备不支持计步传感器,仅用加速度判断");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动定时状态检测
|
||||||
|
mMainHandler.postDelayed(mStatusCheckRunnable, STATUS_CHECK_INTERVAL);
|
||||||
|
mIsDetecting = true;
|
||||||
|
LogUtils.d(TAG, "运动状态监测已启动");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止监测
|
||||||
|
*/
|
||||||
|
public void stopDetection() {
|
||||||
|
if (!mIsDetecting) return;
|
||||||
|
if (mSensorManager != null) {
|
||||||
|
mSensorManager.unregisterListener(this);
|
||||||
|
}
|
||||||
|
mMainHandler.removeCallbacksAndMessages(null);
|
||||||
|
mIsDetecting = false;
|
||||||
|
mIsWalking = false;
|
||||||
|
mCallback = null;
|
||||||
|
LogUtils.d(TAG, "运动状态监测已停止");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSensorChanged(SensorEvent event) {
|
||||||
|
if (!mIsDetecting) return;
|
||||||
|
switch (event.sensor.getType()) {
|
||||||
|
case Sensor.TYPE_ACCELEROMETER:
|
||||||
|
// 计算加速度幅度(保留原逻辑,阈值已调高)
|
||||||
|
float accelX = Math.abs(event.values[0]);
|
||||||
|
float accelY = Math.abs(event.values[1]);
|
||||||
|
float accelZ = Math.abs(event.values[2]);
|
||||||
|
mLastAccelMagnitude = accelX + accelY + accelZ;
|
||||||
|
break;
|
||||||
|
case Sensor.TYPE_STEP_COUNTER:
|
||||||
|
// 累计步数
|
||||||
|
mCurrentStepCount = (int) event.values[0];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时判断运动状态(优化逻辑:计步为0时,即使有轻微加速度也判定为静止)
|
||||||
|
*/
|
||||||
|
private final Runnable mStatusCheckRunnable = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (!mIsDetecting || mCallback == null) return;
|
||||||
|
|
||||||
|
//LogUtils.d(TAG, "mStatusCheckRunnable run");
|
||||||
|
|
||||||
|
boolean newIsWalking = false;
|
||||||
|
// 结合计步器+加速度判断(优化:优先计步,无步数时严格按高阈值判断)
|
||||||
|
if (mStepCounter != null) {
|
||||||
|
int stepChange = mCurrentStepCount - mLastStepCount;
|
||||||
|
// 只有“步数达标” 或 “无步数但加速度远超坐立幅度”,才判定为行走
|
||||||
|
newIsWalking = (stepChange >= STEP_CHANGE_THRESHOLD)
|
||||||
|
&& (mLastAccelMagnitude >= MOTION_THRESHOLD); // 增加步数+加速度双重校验
|
||||||
|
mLastStepCount = mCurrentStepCount;
|
||||||
|
} else {
|
||||||
|
// 无计步器时,仅用高阈值判断
|
||||||
|
newIsWalking = mLastAccelMagnitude >= MOTION_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 状态变化时回调
|
||||||
|
if (newIsWalking != mIsWalking) {
|
||||||
|
mIsWalking = newIsWalking;
|
||||||
|
String statusDesc = mIsWalking ? "行走状态" : "静止/低运动状态";
|
||||||
|
LogUtils.d(TAG, "运动状态变化:" + statusDesc + " | 加速度幅度:" + mLastAccelMagnitude); // 增加日志便于调试
|
||||||
|
mCallback.onMotionStatusChanged(mIsWalking, statusDesc);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogUtils.d(TAG, String.format("运动状态 newIsWalking %s", newIsWalking));
|
||||||
|
|
||||||
|
// 循环检测
|
||||||
|
mMainHandler.postDelayed(this, STATUS_CHECK_INTERVAL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运动状态回调接口
|
||||||
|
*/
|
||||||
|
public interface MotionStatusCallback {
|
||||||
|
void onMotionStatusChanged(boolean isWalking, String statusDesc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user