1、由于系统时间变化会严重影响Timer的使用。因此把所有Timer都改为了
authorsnicker <snickersun@163.com>
Mon, 6 Apr 2020 15:08:39 +0000 (23:08 +0800)
committersnicker <snickersun@163.com>
Mon, 6 Apr 2020 15:08:39 +0000 (23:08 +0800)
ScheduledThreadPoolExecutor
2、修改ResetStepCount 消息处理,加入打印信息。

app/src/main/java/com/enqualcomm/support/CoreService.java
app/src/main/java/com/enqualcomm/support/GPSActivity.java
app/src/main/java/com/enqualcomm/support/LocationClient.java
app/src/main/java/com/enqualcomm/support/MainActivity.java
app/src/main/java/com/enqualcomm/support/service/HeartRateManager.java
app/src/main/java/com/enqualcomm/support/service/KeepAlive.java
app/src/main/java/com/enqualcomm/support/utils/AlarmHelper.java
app/src/main/java/com/enqualcomm/support/utils/ContextProxy.java
app/src/main/java/com/enqualcomm/support/utils/SimSignalUtil.java

index 8ce396b..3c723b1 100644 (file)
@@ -72,8 +72,7 @@ import android.os.BatteryManager;
 import android.telephony.TelephonyManager;\r
 \r
 import java.util.Random;\r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
+import java.util.concurrent.ScheduledThreadPoolExecutor;\r
 import java.util.concurrent.TimeUnit;\r
 \r
 import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;\r
@@ -637,7 +636,7 @@ public class CoreService extends Service {
             stringBuilder.append(paramInt2);\r
             ICLogger.i(stringBuilder.toString());\r
 //            if (ACTION_ALARM.equals(paramIntent.getAction())) {\r
-            if (ACTION_LOCATION_ALARM.equals(paramIntent.getAction())){\r
+            if (ACTION_LOCATION_ALARM.equals(paramIntent.getAction())) {\r
 \r
 \r
 //                    if (cmcm_call_eiot_into_powersave_mode() == 1) {\r
@@ -652,7 +651,7 @@ public class CoreService extends Service {
 //                    alarmCycle = Config.getAlarmCycle(this);\r
 //                }\r
                 AlarmWakeUp.setAlarmWakeUpTime();\r
-                boolean screenon = ((PowerManager)getSystemService(POWER_SERVICE)).isScreenOn();\r
+                boolean screenon = ((PowerManager) getSystemService(POWER_SERVICE)).isScreenOn();\r
                 if (!screenon) {\r
                     SimSignalUtil.send_at_to_update_lbs(this.runtime);\r
                 }\r
@@ -684,76 +683,75 @@ public class CoreService extends Service {
                 if (Config.LOG_TOFILE) ICLogger.logToFile("alarmCycle", stringBuilder1.toString());\r
 //                this.locationClient.triggerLocationProcess();\r
 \r
-            } else {\r
+            } else if (Constants.SLEEP_ALARM.equals(paramIntent.getAction())) {\r
                 StringBuilder stringBuilder1;\r
-                if (Constants.SLEEP_ALARM.equals(paramIntent.getAction())) {\r
-                    ICLogger.i("SLEEP_ALARM");\r
-                    is_allowed_in_sleep_mode = 1;\r
-                    if (Config.LOG_TOFILE) ICLogger.logToFile("alarmCycle", "SLEEP_ALARM  alarmCycle");\r
-                    setSleepAlarm();\r
-                    boolean bool = ((PowerManager)getSystemService(POWER_SERVICE)).isScreenOn();\r
-                    stringBuilder1 = new StringBuilder();\r
-                    stringBuilder1.append("ifOpen = ");\r
-                    stringBuilder1.append(bool);\r
-                    ICLogger.i(stringBuilder1.toString());\r
-                    if (!bool) {\r
-                        is_in_sleep_mode = true;\r
-                        if (this.mKeepAlive != null)\r
-                            this.mKeepAlive.setSleepMode(true);\r
+                ICLogger.i("SLEEP_ALARM");\r
+                is_allowed_in_sleep_mode = 1;\r
+                if (Config.LOG_TOFILE) ICLogger.logToFile("alarmCycle", "SLEEP_ALARM  alarmCycle");\r
+                setSleepAlarm();\r
+                boolean bool = ((PowerManager) getSystemService(POWER_SERVICE)).isScreenOn();\r
+                stringBuilder1 = new StringBuilder();\r
+                stringBuilder1.append("ifOpen = ");\r
+                stringBuilder1.append(bool);\r
+                ICLogger.i(stringBuilder1.toString());\r
+                if (!bool) {\r
+                    is_in_sleep_mode = true;\r
+                    if (this.mKeepAlive != null)\r
+                        this.mKeepAlive.setSleepMode(true);\r
 //                        this.connection.close();\r
-                        return Service.START_STICKY;\r
-                    }\r
-                } else {\r
-                    if (Constants.WAKE_ALARM.equals(paramIntent.getAction())) {\r
-                        if (is_in_sleep_mode) {\r
-                            is_in_sleep_mode = false;\r
-                            if (Config.LOG_TOFILE) ICLogger.logToFile("alarmCycle", "WAKE_ALARM  alarmCycle");\r
-                            if (this.mKeepAlive != null)\r
-                                this.mKeepAlive.setSleepMode(false);\r
-                            wakeUpFromSleep();\r
-                        }\r
-                        is_allowed_in_sleep_mode = 1;\r
-                        setWakeAlarm();\r
-                        return Service.START_STICKY;\r
-                    }else if (HEART_ALARM.equals(paramIntent.getAction())) {\r
-                        if (this.mKeepAlive != null) {\r
-\r
-                            if (connection.isServerConnected()&& runtime.isServerProtocolConnected()) {\r
-                                this.mKeepAlive.onHeartAlarm(this.connection, this.runtime);\r
-                            }else{\r
-                                ICLogger.i("check 发送心跳包时,如果发现服务器没有响应,或者socket连接,则重新尝试连接服务器!");\r
-                                connection.disconnectServer();\r
-                                this.startConnectionRetryTimer();\r
-                            }\r
-                            setHeartAlarm();\r
-                            if (Config.LOG_TOFILE) ICLogger.logToFile("alarmCycle", "HEART_ALARM alarmCycle  " );\r
-                            return Service.START_STICKY;\r
-                        }\r
-                    } else   if (STEP_COUNT_ALARM_ACTION.equals(paramIntent.getAction())) {\r
-                        ICLogger.i("Reset Count At 00:00 ");\r
-                        //update stepCount and broadcast before reset\r
-                        this.locationClient.updateStepCount();\r
-                        runtime.getContextProxy().brodcastResetStepCount(runtime.getTotalStepCount());\r
-                        resetStepCount();\r
-                        setCleanStepCountAlarm();\r
-\r
-                        return Service.START_STICKY;\r
-                    }else {\r
-                            if (ACTION_HEATR_RATE_START.equals(paramIntent.getAction())) {\r
-\r
-                                //此处消息从timer来\r
-                                setHeartRateDetectAlarm(runtime.getPpgTestCycle());\r
-\r
-                            }else if(ACTION_SEND_EMEGENCY_LOCATION_STOP.equals(paramIntent.getAction())){\r
-                                //stop Emegency location Stop\r
-                                Config.loadModCycleSettings(runtime); //回复之前的 工作模式即可\r
-                                setLocationAlarm(runtime.getLocationInstance().getWorkingMod());//同时重置alarm并触发定位\r
-                            }\r
-                            this.invokeHandlerServiceChain.processUserRequest(paramIntent, this.connection, this.runtime);\r
-//                        }\r
+                    return Service.START_STICKY;\r
+                }\r
+            } else if (Constants.WAKE_ALARM.equals(paramIntent.getAction())) {\r
+                if (is_in_sleep_mode) {\r
+                    is_in_sleep_mode = false;\r
+                    if (Config.LOG_TOFILE)\r
+                        ICLogger.logToFile("alarmCycle", "WAKE_ALARM  alarmCycle");\r
+                    if (this.mKeepAlive != null)\r
+                        this.mKeepAlive.setSleepMode(false);\r
+                    wakeUpFromSleep();\r
+                }\r
+                is_allowed_in_sleep_mode = 1;\r
+                setWakeAlarm();\r
+                return Service.START_STICKY;\r
+            } else if (HEART_ALARM.equals(paramIntent.getAction())) {\r
+                if (this.mKeepAlive != null) {\r
+\r
+                    if (connection.isServerConnected() && runtime.isServerProtocolConnected()) {\r
+                        this.mKeepAlive.onHeartAlarm(this.connection, this.runtime);\r
+                    } else {\r
+                        ICLogger.i("check 发送心跳包时,如果发现服务器没有响应,或者socket连接,则重新尝试连接服务器!");\r
+                        connection.disconnectServer();\r
+                        this.startConnectionRetryTimer();\r
                     }\r
+                    setHeartAlarm();\r
+                    if (Config.LOG_TOFILE)\r
+                        ICLogger.logToFile("alarmCycle", "HEART_ALARM alarmCycle  ");\r
+                    return Service.START_STICKY;\r
                 }\r
+            } else if (STEP_COUNT_ALARM_ACTION.equals(paramIntent.getAction())) {\r
+                ICLogger.i("Reset Count At 00:00 ");\r
+                //update stepCount and broadcast before reset\r
+                this.locationClient.updateStepCount();\r
+                runtime.getContextProxy().brodcastResetStepCount(runtime.getTotalStepCount());\r
+                resetStepCount();\r
+                setCleanStepCountAlarm();\r
+                return Service.START_STICKY;\r
+            } else if (ACTION_HEATR_RATE_START.equals(paramIntent.getAction())) {\r
+\r
+                //此处消息从timer来\r
+                setHeartRateDetectAlarm(runtime.getPpgTestCycle());\r
+\r
+\r
+            } else if (ACTION_SEND_EMEGENCY_LOCATION_STOP.equals(paramIntent.getAction())) {\r
+                //stop Emegency location Stop\r
+                Config.loadModCycleSettings(runtime); //回复之前的 工作模式即可\r
+                setLocationAlarm(runtime.getLocationInstance().getWorkingMod());//同时重置alarm并触发定位\r
+\r
             }\r
+\r
+\r
+            this.invokeHandlerServiceChain.processUserRequest(paramIntent, this.connection, this.runtime);\r
+\r
         }\r
         return Service.START_NOT_STICKY;\r
     }\r
@@ -866,7 +864,7 @@ public class CoreService extends Service {
 \r
      private static final String TAG = "ICSrv." + CoreService.class.getSimpleName();\r
 \r
-    private Timer mTimer_retry_connect=null;\r
+    private ScheduledThreadPoolExecutor mTimer_retry_connect=null;\r
 \r
     private static int retry_connect_count=0;  //当前重试次数\r
     private static int retry_connect_max_times=6; //最大重试次数 6次 , 即只重试2分钟,下一次重试将在下一个心跳包时进行。\r
@@ -886,7 +884,7 @@ public class CoreService extends Service {
 //        if (SimSignalUtil.checkNet(runtime.getContextProxy().context) ) {\r
              stopConnectionRetryTimer();\r
             if (mTimer_retry_connect == null) {\r
-                mTimer_retry_connect = new Timer();\r
+                mTimer_retry_connect = new ScheduledThreadPoolExecutor(2);\r
             }\r
             //如果开始进行连接重\r
                 // {\r
@@ -894,12 +892,12 @@ public class CoreService extends Service {
             retry_connect_count=0;\r
             KeepAlive.isConnected = false;\r
             if (mTimer_retry_connect != null) {\r
-                mTimer_retry_connect.schedule(new TimerTask() {\r
+                mTimer_retry_connect.scheduleAtFixedRate(new Runnable() {\r
                     @Override\r
                     public void run() {\r
                         ReConnectToServer();\r
                     }\r
-                }, 5000, 20000); //每次都是断网才需要重连,delay 5秒重连,并且每20秒重试一次\r
+                }, 5000, 20000, TimeUnit.MILLISECONDS); //每次都是断网才需要重连,delay 5秒重连,并且每20秒重试一次\r
             }\r
 //        }\r
     }\r
@@ -948,7 +946,7 @@ public class CoreService extends Service {
 \r
     public void stopConnectionRetryTimer(){\r
         if (mTimer_retry_connect !=null){\r
-            mTimer_retry_connect.cancel();\r
+            mTimer_retry_connect.shutdown();\r
             mTimer_retry_connect.purge();\r
             mTimer_retry_connect = null;\r
         }\r
index c4a644f..09c138c 100644 (file)
@@ -3,8 +3,6 @@ package com.enqualcomm.support;
 import android.graphics.Color;\r
 import android.graphics.Rect;\r
 import android.os.Bundle;\r
-import android.os.Handler;\r
-import android.os.Message;\r
 import android.os.PowerManager;\r
 import android.support.v7.app.AppCompatActivity;\r
 import android.support.v7.widget.LinearLayoutManager;\r
@@ -21,9 +19,6 @@ import com.enqualcomm.support.utils.WakeLockUtil;
 \r
 import java.util.ArrayList;\r
 import java.util.List;\r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
-\r
 public class GPSActivity extends AppCompatActivity implements location_callback {\r
 \r
     private RecyclerView mRv;\r
@@ -33,40 +28,8 @@ public class GPSActivity extends AppCompatActivity implements location_callback
     private TextView mHoop;\r
     private TextView mSearchCount;\r
     private boolean showmessage=false;\r
-    private Timer timer;\r
     final LocationSnrAdapter adapter = new LocationSnrAdapter(testSnrEntriyDate(null));\r
-//\r
-//    Handler handler=new Handler(new Handler.Callback() {\r
-//        @Override\r
-//        public boolean handleMessage(Message message) {\r
-//\r
-//\r
-//            StringBuilder sb=new StringBuilder();\r
-//            try {\r
-//\r
-//                if (showmessage) {\r
-//                    ICLocation location=CoreService.getInstance().runtime.getLocationInstance();\r
-//                    List<ICLocation.GpsSatellitePriv> gpslist =location.getNumSatelliteList();\r
-//                    adapter.updateLocationValue(testSnrEntriyDate(gpslist));\r
-//                    String fixed=(location.getLocationStat()==ICLocation.LOCATION_STAT.LOCATION_FIXED)?\r
-//                            "Fixed":(location.getLocationStat()==ICLocation.LOCATION_STAT.LOCATION_OPEN)?\r
-//                            "Opened":(location.getLocationStat()==ICLocation.LOCATION_STAT.LOCATION_GPS_TIEMOUT)?\r
-//                            "Timeout":"Closed";\r
-//                    String satlites=new StringBuilder().append(location.getLocation().used_gps_numbs).append("/").append(gpslist!=null?location.getNumSatelliteList().size():0).toString();\r
-//                    String locationTime=location.getLocationTime();\r
-//                    mFix.setText(fixed);\r
-//                    mStar.setText(satlites);\r
-//                    mHoop.setText(String.valueOf(location.getLocation().hdop));\r
-//                }\r
-//\r
-//            }catch(Exception e){\r
-//                e.printStackTrace();\r
-//            }\r
-//\r
-//\r
-//            return false;\r
-//        }\r
-//    });\r
+\r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
@@ -144,15 +107,6 @@ public class GPSActivity extends AppCompatActivity implements location_callback
     @Override\r
     protected  void onResume() {\r
         super.onResume();\r
-//        TimerTask tmt=new TimerTask() {\r
-//            @Override\r
-//            public void run() {\r
-//                handler.sendEmptyMessage(0);\r
-//            }\r
-//        };\r
-//        timer=new Timer();\r
-//        timer.schedule(tmt,0,2000);\r
-//        showmessage=true;\r
         NMEAListener.set_callback(this);\r
         if (screenOnWakelock==null)\r
             screenOnWakelock = WakeLockUtil.getScreenOnWakeLock(this,GPSActivity.class.getSimpleName());\r
@@ -161,11 +115,6 @@ public class GPSActivity extends AppCompatActivity implements location_callback
     @Override\r
     protected void onPause() {\r
         super.onPause();\r
-//        if (timer!=null) {\r
-//            timer.cancel();\r
-//            timer.purge();\r
-//            timer = null;\r
-//        }\r
         showmessage=false;\r
         if(screenOnWakelock!=null) {\r
             if (screenOnWakelock.isHeld()) {\r
index d2f739f..b6f0b15 100644 (file)
@@ -33,11 +33,10 @@ import com.enqualcomm.support.utils.SimSignalUtil;
 import com.enqualcomm.support.bean.Runtime;\r
 import com.enqualcomm.support.utils.TimeUtil;\r
 \r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
 import java.util.concurrent.ExecutorService;\r
 import java.util.concurrent.Executors;\r
 import java.util.concurrent.LinkedBlockingDeque;\r
+import java.util.concurrent.ScheduledThreadPoolExecutor;\r
 import java.util.concurrent.TimeUnit;\r
 import java.util.concurrent.locks.Condition;\r
 import java.util.concurrent.locks.ReentrantLock;\r
@@ -105,7 +104,7 @@ public class LocationClient {
 \r
   private LBSHelper mLbsHelper;\r
 \r
-  private Timer mTimer=null;\r
+  private ScheduledThreadPoolExecutor mTimer=null;\r
   \r
   private int mPedometer2;\r
   \r
@@ -268,17 +267,16 @@ public class LocationClient {
       timeout=false;\r
       startCheckTime= System.currentTimeMillis();\r
       //延时2秒执行发送“读取定位”消息\r
-      if (mTimer==null) mTimer=new Timer();\r
-      mTimer.schedule(new MyTimerTask(), 1000L,1000L);\r
+      if (mTimer==null) mTimer=new ScheduledThreadPoolExecutor(2);\r
+      mTimer.scheduleAtFixedRate(new Runnable() {\r
+        @Override\r
+        public void run() {\r
+          mhandler.sendEmptyMessage(msgWhat_WaitAndSendLocation);\r
+        }\r
+      }, 1000L, 1000L,TimeUnit.MILLISECONDS);\r
 //    AlarmManager alarmManager=(AlarmManager)runtime.getContextProxy().context.getSystemService(Context.ALARM_SERVICE);\r
 //    AlarmHelper.setAlarm(alarmManager,0,1,);\r
   }\r
-  class MyTimerTask extends TimerTask {\r
-    public void run() {\r
-      //发送读取计步消息\r
-      mhandler.sendEmptyMessage(msgWhat_WaitAndSendLocation);\r
-    }\r
-  };\r
 \r
 \r
 \r
@@ -344,7 +342,7 @@ public class LocationClient {
             if  ((startCheckTime!=0L)&& (timeCost> 50*1000L)){\r
               //有时timer计数不准,则根据实际时间计算\r
               ICLogger.i("waitAndSendLocation "+timeCost+"> 50000ms timeout");\r
-              timeout=true;\r
+              //timeout=true;\r
           }\r
 \r
             // lbs only +timeout, lbs+wifi+gpsNosignal, libs+ , lbs+gps+wifi, Timeout can override wifi and gps\r
@@ -407,7 +405,7 @@ public class LocationClient {
             if  ((startCheckTime!=0L)&& (timeCost> 20*1000L)){\r
               //有时timer计数不准,则根据实际时间计算\r
               ICLogger.i("waitAndSendLocation "+timeCost+"> 20000ms timeout");\r
-              timeout=true;\r
+             // timeout=true;\r
             }\r
 \r
             if ((lbs_ok && timeout==true) ||(lbs_ok && wifi_ok )){\r
@@ -453,7 +451,8 @@ public class LocationClient {
   public void stopCheckLocationResult(){\r
     ICLogger.i("stopCheckLocationResult!");\r
     if (mTimer!=null){\r
-      mTimer.cancel();\r
+      mTimer.shutdown();\r
+      mTimer.purge();\r
       mTimer=null;\r
     }\r
     isLocating = false;\r
index ced4dba..b61ec78 100644 (file)
@@ -24,8 +24,7 @@ import com.ic.jni.ICJniUtils;
 import com.enqualcomm.support.CoreService;
 import com.enqualcomm.support.utils.ICLogger;
 
-import java.util.Timer;
-import java.util.TimerTask;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
 import rx.Observable;
@@ -37,7 +36,7 @@ public class MainActivity extends AppCompatActivity {
     TextView tv;
 
     private boolean showmessage=false;
-    private Timer timer;
+    private ScheduledThreadPoolExecutor timer;
     Handler handler=new Handler(new Handler.Callback() {
         @Override
         public boolean handleMessage(Message message) {
@@ -116,14 +115,13 @@ public class MainActivity extends AppCompatActivity {
     @Override
     protected  void onResume() {
         super.onResume();
-        TimerTask tmt=new TimerTask() {
+        timer=new ScheduledThreadPoolExecutor(2);
+        timer.scheduleAtFixedRate(new Runnable() {
             @Override
             public void run() {
                 handler.sendEmptyMessage(0);
             }
-        };
-        timer=new Timer();
-        timer.schedule(tmt,0,2000);
+        }, 0, 2000,TimeUnit.MILLISECONDS);
         showmessage=true;
     }
 
@@ -131,7 +129,7 @@ public class MainActivity extends AppCompatActivity {
     protected void onPause() {
         super.onPause();
         if (timer!=null) {
-            timer.cancel();
+            timer.shutdown();
             timer.purge();
             timer = null;
         }
index b78e8f8..b3a1881 100644 (file)
@@ -27,8 +27,8 @@ import com.enqualcomm.support.utils.NettyConnection;
 import com.enqualcomm.support.utils.SimSignalUtil;\r
 import com.enqualcomm.support.utils.TimeUtil;\r
 \r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
+import java.util.concurrent.ScheduledThreadPoolExecutor;\r
+import java.util.concurrent.TimeUnit;\r
 \r
 import static com.enqualcomm.support.Constants.ACTION_BROADCAST_PPG;\r
 import static com.enqualcomm.support.Constants.ACTION_HEATR_RATE_START;\r
@@ -44,9 +44,8 @@ public class HeartRateManager extends AbstractInvokeHandlerService {
 \r
   private Runtime runtime;\r
   private NettyConnection connection;\r
-  private TimerTaskPPG timerTaskPPG;\r
 \r
-  private Timer timerPPG;\r
+  private ScheduledThreadPoolExecutor timerPPG=null;\r
   private static Handler mHandler=null;\r
 \r
 \r
@@ -134,13 +133,6 @@ public class HeartRateManager extends AbstractInvokeHandlerService {
     runtime.getContextProxy().context.unregisterReceiver(PPGStartReceiver);\r
   }\r
 \r
-  public class TimerTaskPPG extends TimerTask{\r
-    @Override\r
-    public void run() {\r
-      doPPGtest();\r
-    }\r
-  };\r
-\r
   int PPG;\r
   int BPH;\r
   int BPL;\r
@@ -234,9 +226,14 @@ public class HeartRateManager extends AbstractInvokeHandlerService {
       isTimerStarted=true;\r
       ICJniUtils.enablePPG();\r
       getPPGWakeLock();\r
-      timerTaskPPG = new TimerTaskPPG();\r
-      timerPPG = new Timer();\r
-      timerPPG.scheduleAtFixedRate(timerTaskPPG,0, 1000); //1 秒更新一次数据\r
+\r
+      timerPPG = new ScheduledThreadPoolExecutor(2);\r
+      timerPPG.scheduleAtFixedRate(new Runnable() {\r
+        @Override\r
+        public void run() {\r
+          doPPGtest();\r
+        }\r
+      }, 0, 1000, TimeUnit.MILLISECONDS); //1 秒更新一次数据\r
 \r
     }\r
   }\r
@@ -244,7 +241,8 @@ public class HeartRateManager extends AbstractInvokeHandlerService {
   public void stopPPG(){\r
     if( isTimerStarted )\r
     {\r
-      timerPPG.cancel();\r
+      timerPPG.shutdown();\r
+      timerPPG.purge();\r
       isTimerStarted=false;\r
     }\r
     ICJniUtils.disablePPG();\r
index 4ae50ab..f2e6a75 100644 (file)
@@ -24,18 +24,13 @@ import com.enqualcomm.support.utils.ServiceUtil;
 import com.enqualcomm.support.bean.Runtime;\r
 import com.enqualcomm.support.utils.SimSignalUtil;\r
 \r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
+import java.util.concurrent.ScheduledThreadPoolExecutor;\r
 import java.util.concurrent.TimeUnit;\r
 \r
 import rx.Observable;\r
 import rx.Subscription;\r
 import rx.functions.Action1;\r
 \r
-import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;\r
-//import static com.eiot.watch_lib.LauncherCtrl.STATUS_BAR_STATE_SERVER_CONNECTED_OFF;\r
-//import static com.eiot.watch_lib.LauncherCtrl.STATUS_BAR_STATE_SERVER_CONNECTED_ON;\r
-\r
 public class KeepAlive extends AbstractConnectionService {\r
   private static final long HEARTBEAT_TIMEOUT = 660000L;\r
   private static String TAG = KeepAlive.class.getSimpleName();\r
@@ -44,23 +39,9 @@ public class KeepAlive extends AbstractConnectionService {
   public static boolean isConnected = false;\r
   \r
   private static boolean isInSleepMode = false;\r
-  \r
-  private static int mFlag = 30;\r
-  \r
+\r
   Connection connection;\r
   \r
-  private int currentReConnectInterval = 11;\r
-  \r
-  private boolean firstReConnected = true;\r
-  \r
-  private long idleTimeMillis;\r
-  \r
-  private int isFirstAsjust;\r
-  \r
-  private int lastReConnectInterval = -1;\r
-  \r
-  private int lastReConnectionNumber = -1;\r
-  \r
   Context mContext;\r
   \r
   NetworkObservable mNetworkObservable = new NetworkObservable();\r
@@ -72,14 +53,6 @@ public class KeepAlive extends AbstractConnectionService {
   private Subscription mSubscription;\r
   \r
   private int reConnectionNumber;\r
-  \r
-  private int reCycleTimes;\r
-  \r
-  private int timeout = 660;\r
-  \r
-  private int totalAdjustTimes;\r
-  \r
-  private int whichTimeAdjust;\r
 \r
 //服务器收到AP03 会返回BP03,如果PB03 规定时间没收到,则认为发送心跳失败,需要重发,\r
   private boolean is_heartBeat_send_success=false;\r
@@ -89,30 +62,28 @@ public class KeepAlive extends AbstractConnectionService {
   //每次retry发送心跳的间隔为20秒\r
   private int heartBeatRetryTimeout=20;\r
   //监测 心跳服务器响应的timer\r
-  private Timer mTimer_check_server_response=null;\r
-\r
+  private ScheduledThreadPoolExecutor scheduled = null;\r
+  private Runnable CheckSrvResponseTimerTask=new Runnable() {\r
+    @Override\r
+    public void run() {\r
+      checkSrvResponse();\r
+    }\r
+  };\r
 \r
   public void startResponseCheckTimer(){\r
     //联网再重试连接服务器\r
     if (SimSignalUtil.checkNet(mRuntime.getContextProxy().context) ) {\r
       stopResponseCheckTimer();\r
-      if (mTimer_check_server_response == null) {\r
-        mTimer_check_server_response = new Timer();\r
+      if (scheduled == null) {\r
+        scheduled = new ScheduledThreadPoolExecutor(2);\r
       }\r
-\r
       //开始检测重新计数\r
       hearBeatResendCount=0;\r
       //如果开始进行连接重\r
       // {\r
       // 试,证明网络断开的,那么KeepAlive也需要重连(心跳包)\r
-\r
-      if (mTimer_check_server_response != null) {\r
-        mTimer_check_server_response.schedule(new TimerTask() {\r
-          @Override\r
-          public void run() {\r
-            checkSrvResponse();\r
-          }\r
-        }, heartBeatRetryTimeout*1000, heartBeatRetryTimeout*1000); //delay 3秒先检查一次,然后每5秒重试一次\r
+      if (scheduled != null) {\r
+        scheduled.scheduleAtFixedRate(CheckSrvResponseTimerTask , heartBeatRetryTimeout*1000, heartBeatRetryTimeout*1000,TimeUnit.MILLISECONDS); //delay 3秒先检查一次,然后每5秒重试一次\r
       }\r
     }\r
   }\r
@@ -154,9 +125,10 @@ public class KeepAlive extends AbstractConnectionService {
   }\r
 \r
   public void stopResponseCheckTimer(){\r
-    if (mTimer_check_server_response !=null){\r
-      mTimer_check_server_response.cancel();\r
-      mTimer_check_server_response = null;\r
+    if (scheduled !=null){\r
+      scheduled.shutdown();\r
+      scheduled.purge();\r
+      scheduled = null;\r
     }\r
   }\r
 \r
@@ -166,21 +138,6 @@ public class KeepAlive extends AbstractConnectionService {
 \r
     ICLogger.i("onAlarmClock");\r
 \r
-    long l = SystemClock.elapsedRealtime();\r
-\r
-//    //如果 没有连接成功 或者 时间超过了11分钟 ,则进行重新连接服务\r
-//    if (!isConnected || l - this.idleTimeMillis > 660000L) {\r
-//      StringBuilder stringBuilder = new StringBuilder();\r
-//      stringBuilder.append("重连");\r
-//      stringBuilder.append(isConnected);\r
-//      ICLogger.i(stringBuilder.toString());\r
-//      if (!isConnected && this.firstReConnected) {\r
-//        this.reConnectionNumber++;\r
-//        this.firstReConnected = false;\r
-//      }\r
-//\r
-//      CoreService.getInstance().ReConnectToServer();\r
-//    }\r
   }\r
   \r
   public void onAttach(final Connection connection, final Runtime runtime) {\r
@@ -272,8 +229,8 @@ public class KeepAlive extends AbstractConnectionService {
 \r
 \r
      isConnected = true;\r
-    this.firstReConnected = true;\r
-    this.idleTimeMillis = SystemClock.elapsedRealtime();\r
+//    this.firstReConnected = true;\r
+//    this.idleTimeMillis = SystemClock.elapsedRealtime();\r
 \r
     return false;\r
   }\r
@@ -302,7 +259,7 @@ public class KeepAlive extends AbstractConnectionService {
   }\r
 \r
   public boolean onReceive(StringMessage paramStringMessage, Connection paramConnection, Runtime paramRuntime) {\r
-    this.idleTimeMillis = SystemClock.elapsedRealtime();\r
+//    this.idleTimeMillis = SystemClock.elapsedRealtime();\r
     if ("heart".equals(paramStringMessage.cmd())) {\r
       AlarmWakeUp.isNeedWakeUpAlarm(paramRuntime);\r
       return true;\r
index 1c9dbcd..7a1fca7 100644 (file)
@@ -62,6 +62,7 @@ public class AlarmHelper {
     Intent intent = new Intent(context, com.enqualcomm.support.CoreService.class);\r
     intent.setAction(paramString);\r
     PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);\r
+    alarmManager.cancel(pendingIntent);\r
     long currentMS = System.currentTimeMillis();\r
     Calendar calendar = Calendar.getInstance();\r
     calendar.set(Calendar.HOUR_OF_DAY, hour);\r
@@ -71,7 +72,6 @@ public class AlarmHelper {
     long targetMS = targetMStmp;\r
     if (targetMStmp < currentMS)\r
       targetMS = targetMStmp + ONE_DAY;\r
-    alarmManager.cancel(pendingIntent);\r
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetMS, pendingIntent);\r
   }\r
   \r
index d566d2e..4840c92 100644 (file)
@@ -286,6 +286,7 @@ public class ContextProxy {
     Intent intent = new Intent();\r
     intent.putExtra(PED_TOTAL_KEY, StepCount);\r
     intent.setAction(PED_FINISH_ACTION);\r
+    ICLogger.i("send broadcast  action "+PED_FINISH_ACTION+", key "+PED_TOTAL_KEY+" value "+StepCount);\r
     this.context.sendBroadcast(intent);\r
   }\r
   public void sendBrodcast(String action,Intent intent){\r
index 613edd5..c5d0a9e 100644 (file)
@@ -228,7 +228,7 @@ public class SimSignalUtil {
     intent.putExtra("command", "AT+SFUN=2"); //重新初始化sim卡\r
     intent.putExtra("arg2", "");\r
     ( runtime.getContextProxy()).context.sendBroadcast(intent);\r
-    ICLogger.i("send_at_to_update_lbs ");\r
+    ICLogger.i("send_at_to_reset_simcard ");\r
   }\r
 \r
   public static void send_at_to_update_lbs(Runtime runtime) {\r