@@ -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  
		
	
		
			
				 * 功能: ,   
		
	
		
			
				 */  
		
	
		
			
				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键: ,   
		
	
		
			
					 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 ,   " 无效广播:  " ) ;  
		
	
		
			
							 return ;  
		
	
		
			
						 }  
		
	
		
			
				 
		
	
		
			
						 // 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未启动,  " ) ;  
		
	
		
			
							 return ;  
		
	
		
			
						 }  
		
	
		
			
				 
		
	
		
			
						 // 3. 根据运动状态处理GPS逻辑  
		
	
		
			
						 if   ( motionStatus   = =   1 )   {  
		
	
		
			
							 // 3.1 行走中: ( )   
		
	
		
			
							 handleWalkingStatus ( mainService ,   context ) ;  
		
	
		
			
						 }   else   {  
		
	
		
			
							 // 3.2 静止/低运动:   
		
	
		
			
							 handleStaticStatus ( mainService ) ;  
		
	
		
			
						 }  
		
	
		
			
					 }  
		
	
		
			
				 
		
	
		
			
					 /**  
		
	
		
			
					 * 处理行走状态:   
		
	
		
			
					 */  
		
	
		
			
					 private   void   handleWalkingStatus ( MainService   mainService ,   Context   context )   {  
		
	
		
			
						 // 检查GPS权限(   
		
	
		
			
						 if   ( Build . VERSION . SDK_INT   > =   Build . VERSION_CODES . M   & &    
		
	
		
			
							 context . checkSelfPermission ( android . Manifest . permission . ACCESS_FINE_LOCATION )    
		
	
		
			
							 ! =   PackageManager . PERMISSION_GRANTED )   {  
		
	
		
			
							 // 发送权限申请广播( , )   
		
	
		
			
							 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 ,   " 行走中: ,  " ) ;  
		
	
		
			
							 return ;  
		
	
		
			
						 }  
		
	
		
			
				 
		
	
		
			
						 // 权限已授予: ( )   
		
	
		
			
						 if   ( ! mainService . isGpsListening ( ) )   {   // 需在MainService中新增isGpsListening()方法  
		
	
		
			
							 mainService . startGpsLocation ( ) ;  
		
	
		
			
							 LogUtils . d ( TAG ,   " 行走中:  " ) ;  
		
	
		
			
						 }  
		
	
		
			
				 
		
	
		
			
						 // 停止静止时的GPS定时任务( )   
		
	
		
			
						 mainService . stopGpsStaticTimer ( ) ;  
		
	
		
			
					 }  
		
	
		
			
				 
		
	
		
			
					 /**  
		
	
		
			
					 * 处理静止状态:   
		
	
		
			
					 */  
		
	
		
			
					 private   void   handleStaticStatus ( MainService   mainService )   {  
		
	
		
			
						 // 关闭持续GPS监听( )   
		
	
		
			
						 if   ( mainService . isGpsListening ( ) )   {  
		
	
		
			
							 mainService . stopGpsLocation ( ) ;  
		
	
		
			
							 LogUtils . d ( TAG ,   " 静止中:  " ) ;  
		
	
		
			
						 }  
		
	
		
			
				 
		
	
		
			
						 // 启动定时GPS获取( , )   
		
	
		
			
						 mainService . startGpsStaticTimer ( GPS_STATIC_INTERVAL ) ;  
		
	
		
			
					 }  
		
	
		
			
				 
		
	
		
			
					 /**  
		
	
		
			
					 * 获取MainService实例( , )   
		
	
		
			
					 */  
		
	
		
			
					 private   MainService   getMainService ( Context   context )   {  
		
	
		
			
						 // 方式1: , ( )   
		
	
		
			
						 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 ) ) ;  
		
	
		
			
							 // 等待服务启动( , )   
		
	
		
			
							 try   {  
		
	
		
			
								 Thread . sleep ( 500 ) ;  
		
	
		
			
							 }   catch   ( InterruptedException   e )   {  
		
	
		
			
								 Thread . currentThread ( ) . interrupt ( ) ;  
		
	
		
			
							 }  
		
	
		
			
						 }  
		
	
		
			
						 return   MainService . getInstance ( context ) ;  
		
	
		
			
					 }  
		
	
		
			
				}