实现位置管理窗口实时位置显示。
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Thu Oct 02 13:16:14 GMT 2025 | ||||
| #Thu Oct 02 18:42:32 GMT 2025 | ||||
| stageCount=8 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.7 | ||||
| buildCount=29 | ||||
| buildCount=36 | ||||
| baseBetaVersion=15.0.8 | ||||
|   | ||||
| @@ -22,6 +22,8 @@ import cc.winboll.studio.positions.models.PositionTaskModel; | ||||
| import cc.winboll.studio.positions.services.MainService; | ||||
| import cc.winboll.studio.positions.R; | ||||
| import java.util.ArrayList; | ||||
| import cc.winboll.studio.libappbase.ToastUtils; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| /** | ||||
|  * Java 7 语法适配: | ||||
| @@ -30,7 +32,7 @@ import java.util.ArrayList; | ||||
|  * 3. 所有位置/任务操作通过 MainService 接口执行 | ||||
|  */ | ||||
| public class LocationActivity extends Activity { | ||||
|     private static final String TAG = "LocationActivity"; | ||||
|     public static final String TAG = "LocationActivity"; | ||||
|  | ||||
|     private RecyclerView mRvPosition; | ||||
|     private PositionAdapter mPositionAdapter; | ||||
| @@ -39,6 +41,11 @@ public class LocationActivity extends Activity { | ||||
|     // MainService 引用+绑定状态 | ||||
|     private MainService mMainService; | ||||
|     private boolean isServiceBound = false; | ||||
| 	 | ||||
| 	// ---------------------- 新增:GPS监听核心变量 ---------------------- | ||||
|     private MainService.GpsUpdateListener mGpsUpdateListener; // GPS监听实例 | ||||
|     private PositionModel mCurrentGpsPos; // 缓存当前GPS位置(供页面使用) | ||||
| 	 | ||||
|  | ||||
|     // 服务连接(Java 7 匿名内部类实现) | ||||
|     private ServiceConnection mServiceConnection = new ServiceConnection() { | ||||
| @@ -54,6 +61,7 @@ public class LocationActivity extends Activity { | ||||
|             syncDataFromMainService(); | ||||
|             // 初始化Adapter(传入MainService实例,确保任务数据从服务获取) | ||||
|             initPositionAdapter(); | ||||
| 			registerGpsListener(); | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
| @@ -75,6 +83,9 @@ public class LocationActivity extends Activity { | ||||
|  | ||||
|         // 绑定MainService(确保Activity启动时就拿到服务实例) | ||||
|         bindMainService(); | ||||
| 		 | ||||
| 		// 初始化GPS监听(提前创建,避免空指针) | ||||
| 		initGpsUpdateListener(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -192,6 +203,80 @@ public class LocationActivity extends Activity { | ||||
|     private void showToast(String content) { | ||||
|         Toast.makeText(this, content, Toast.LENGTH_SHORT).show(); | ||||
|     } | ||||
| 	 | ||||
| 	// ---------------------- 新增:GPS监听初始化+注册/反注册(核心适配逻辑) ---------------------- | ||||
|     /** | ||||
|      * 初始化GPS监听:实现MainService.GpsUpdateListener,接收实时GPS数据 | ||||
|      */ | ||||
|     private void initGpsUpdateListener() { | ||||
| 		LogUtils.d(TAG, "initGpsUpdateListener()"); | ||||
|         mGpsUpdateListener = new MainService.GpsUpdateListener() { | ||||
|             // 回调1:GPS位置更新(实时接收经纬度,更新缓存+刷新Adapter) | ||||
|             @Override | ||||
|             public void onGpsPositionUpdated(PositionModel currentGpsPos) { | ||||
|                 if (currentGpsPos == null) { | ||||
|                     LogUtils.w(TAG, "GPS位置更新:数据为空"); | ||||
|                     return; | ||||
|                 } | ||||
|                 // 缓存当前GPS位置(供页面其他逻辑使用) | ||||
|                 mCurrentGpsPos = currentGpsPos; | ||||
|                 LogUtils.d(TAG, String.format("收到GPS更新:纬度=%.4f,经度=%.4f" | ||||
| 											  , currentGpsPos.getLatitude(), currentGpsPos.getLongitude())); | ||||
| 				((TextView)findViewById(R.id.tv_latitude)).setText(String.format("当前纬度:%f", currentGpsPos.getLatitude())); | ||||
| 				((TextView)findViewById(R.id.tv_longitude)).setText(String.format("当前经度:%f", currentGpsPos.getLongitude())); | ||||
|  | ||||
|                 // 1. 同步GPS位置到MainService(确保服务数据与页面一致,触发距离计算) | ||||
|                 if (mMainService != null) { | ||||
| //                    mMainService.syncCurrentGpsPosition(currentGpsPos); | ||||
| //                    // 2. 强制刷新距离计算+Adapter(显示最新距离) | ||||
| //                    mMainService.forceRefreshDistance(); | ||||
| //                    refreshCachedDataAndAdapter(); | ||||
|                 } | ||||
|  | ||||
|                 // 3. (可选)显示GPS位置Toast提示(如调试场景) | ||||
|                 // ToastUtils.show("GPS更新:" + currentGpsPos.getLatitude() + "," + currentGpsPos.getLongitude()); | ||||
|             } | ||||
|  | ||||
|             // 回调2:GPS状态变化(如开启/关闭、信号弱,提示用户) | ||||
|             @Override | ||||
|             public void onGpsStatusChanged(String status) { | ||||
|                 if (status == null) return; | ||||
|                 LogUtils.d(TAG, "GPS状态变化:" + status); | ||||
|                 // 显示GPS状态(可通过TextView在页面上展示,此处用Toast示例) | ||||
|                 if (status.contains("未开启") || status.contains("权限") || status.contains("失败")) { | ||||
|                     // 异常状态:弹出提示引导用户处理 | ||||
|                     ToastUtils.show("GPS提示:" + status); | ||||
|                 } | ||||
|             } | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 注册GPS监听:调用MainService的PUBLIC方法,绑定监听 | ||||
|      */ | ||||
|     private void registerGpsListener() { | ||||
| 		LogUtils.d(TAG, "registerGpsListener()"); | ||||
|         if (mMainService == null || mGpsUpdateListener == null) { | ||||
|             LogUtils.w(TAG, "GPS监听注册失败:服务未初始化或监听未创建"); | ||||
|             return; | ||||
|         } | ||||
|         // 调用MainService的registerGpsUpdateListener方法注册 | ||||
|         mMainService.registerGpsUpdateListener(mGpsUpdateListener); | ||||
|         LogUtils.d(TAG, "GPS监听已注册"); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 反注册GPS监听:调用MainService的PUBLIC方法,解绑监听(核心防内存泄漏) | ||||
|      */ | ||||
|     private void unregisterGpsListener() { | ||||
|         if (mMainService == null || mGpsUpdateListener == null) { | ||||
|             LogUtils.w(TAG, "GPS监听反注册失败:服务未初始化或监听未创建"); | ||||
|             return; | ||||
|         } | ||||
|         // 调用MainService的unregisterGpsUpdateListener方法反注册 | ||||
|         mMainService.unregisterGpsUpdateListener(mGpsUpdateListener); | ||||
|         LogUtils.d(TAG, "GPS监听已反注册"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     protected void onDestroy() { | ||||
| @@ -207,6 +292,8 @@ public class LocationActivity extends Activity { | ||||
|             unbindService(mServiceConnection); | ||||
|             LogUtils.d(TAG, "MainService解绑完成"); | ||||
|         } | ||||
| 		 | ||||
| 		unregisterGpsListener(); | ||||
|     } | ||||
| 	 | ||||
| 	public static class LocalBinder extends android.os.Binder { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen