/td> 57 79
                 break;
58
-            case COMMAND_STOP_LOCATION:
59
-                stopLocation();
60
-                break;
61 80
             case COMMAND_DESTROY_LOCATION:
62 81
                 destroyLocation();
63 82
                 break;
@@ -76,24 +95,93 @@ public class MyLocationService extends Service {
76 95
         if(locationClient!=null){
77 96
             return;
78 97
         }
79
-        //初始化client
80
-        locationClient = new AMapLocationClient(this.getApplicationContext());
81
-        //设置定位参数
98
+        locationClient = new AMapLocationClient(getApplicationContext());
82 99
         locationClient.setLocationOption(getDefaultOption());
83
-        // 设置定位监听
84 100
         locationClient.setLocationListener(locationListener);
85
-        status = 0;
86 101
     }
87 102
 
103
+    private void initGatherInfo(){
104
+        if(!Preferences.getInstance(this).isTourMode()&&Preferences.getInstance(this).getTourEndTime()<System.currentTimeMillis()){
105
+            return;
106
+        }
107
+        long gatherTime = Preferences.getInstance(this).getGatherTime();
108
+        if(gatherTime<System.currentTimeMillis()){
109
+            mHandler.sendEmptyMessageDelayed(MSG_FETCH_TOUR_INFO,60000);
110
+        }
111
+    }
112
+
113
+    private void fetchTourInfo(){
114
+        mHandler.sendEmptyMessageDelayed(MSG_FETCH_TOUR_INFO,FETCH_GATHER_INFO_PERIOD);
115
+        if(!Preferences.getInstance(this).isTourMode()&&Preferences.getInstance(this).getTourEndTime()<System.currentTimeMillis()){
116
+            return;
117
+        }
118
+        long gatherTime = Preferences.getInstance(this).getGatherTime();
119
+        if(gatherTime>System.currentTimeMillis()){
120
+            return;
121
+        }
122
+        if(fetchTourInfoTask!=null && fetchTourInfoTask.getStatus()== AsyncTask.Status.RUNNING){
123
+            return;
124
+        }
125
+        HashMap<String,String> params = new HashMap<String,String>();
126
+        String userId = Preferences.getInstance(this).getUserId();
127
+        if(userId.length()==0){
128
+            return;
129
+        }
130
+        params.put("user_id",userId);
131
+
132
+        fetchTourInfoTask = new HttpPostTask(this,params) {
133
+
134
+
135
+            @Override
136
+            protected boolean parseResponse(Context context, String response) {
137
+                try{
138
+                    JSONObject json = new JSONObject(response);
139
+                    int status = json.getInt("status");
140
+                    if(status == 200){
141
+                        JSONObject data = json.getJSONObject("data");
142
+                        String groupId = data.getString("group_id");
143
+                        if(!TextUtils.isEmpty(groupId)){
144
+                            Preferences.getInstance(context).setTourMode(true);
145
+                            JSONObject group = data.getJSONObject("group");
146
+                            String phone = group.getString("phone");
147
+                            Preferences.getInstance(context).setTourGuidePhone(phone);
148
+                            String gatherTimeStr = group.getString("gather_at");
149
+                            Preferences.getInstance(context).setGatherTime(TimeUtils.getStandardTimeInMiliSec(gatherTimeStr));
150
+                            String endTimeStr = group.getString("ended_at");
151
+                            Preferences.getInstance(context).setTourEndTime(TimeUtils.getStandardTimeInMiliSec(endTimeStr));
152
+                        }else{
153
+                            Preferences.getInstance(context).setTourMode(false);
154
+                            Preferences.getInstance(context).setTourGuidePhone("");
155
+                            Preferences.getInstance(context).setGatherTime(0);
156
+                            Preferences.getInstance(context).setTourEndTime(0);
157
+                        }
158
+                        return true;
159
+                    }
160
+                }catch (Exception e){
161
+                    e.printStackTrace();
162
+                }
163
+                return false;
164
+            }
88 165
 
166
+            @Override
167
+            protected void onPostSuccess(Context context) {
168
+                super.onPostSuccess(context);
169
+                if(mHandler!=null){
170
+                    mHandler.sendEmptyMessage(MSG_START_LOCATION);
171
+                }
172
+            }
173
+        };
174
+        fetchTourInfoTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.TOUR_INFO_URL);
175
+
176
+    }
89 177
     private AMapLocationClientOption getDefaultOption(){
90 178
         AMapLocationClientOption mOption = new AMapLocationClientOption();
91 179
         mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
92 180
         mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
93 181
         mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
94 182
         mOption.setInterval(30*1000);//可选,设置定位间隔。默认为30秒
95
-        mOption.setNeedAddress(false);//可选,设置是否返回逆地理地址信息。默认是true
96
-        mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
183
+        mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
184
+        mOption.setOnceLocation(true);//可选,设置是否单次定位。默认是false
97 185
         mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
98 186
         AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
99 187
         mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
@@ -106,42 +194,39 @@ public class MyLocationService extends Service {
106 194
         public void onLocationChanged(AMapLocation loc) {
107 195
             double lat = loc.getLatitude();
108 196
             double lon = loc.getLongitude();
197
+            LogHelper.dfile("czy","经纬度 lat = "+lat+"lon="+lon+" 位置 ="+loc.getAddress());
109 198
             reportLocationWhenTourMode(lat,lon);
110 199
         }
111 200
     };
112 201
 
113 202
 
114 203
     private void startLocation(){
115
-        if(status==1){
116
-            return;
204
+        initLocation();
205
+        if(Preferences.getInstance(this).isTourMode()){
206
+            locationClient.setLocationOption(locationOption);
207
+            locationClient.startLocation();
208
+            if(Preferences.getInstance(this).getGatherTime()> System.currentTimeMillis()){
209
+                mHandler.sendEmptyMessageDelayed(MSG_START_LOCATION,Preferences.getInstance(this)
210
+                        .getGatherTime()-System.currentTimeMillis()>LOCATION_PERIOD_CHANGE_DEADLINE ? LOCATION_MAX_PERIOD:LOCATION_MIN_PERIOD);
211
+            }
117 212
         }
118
-        // 设置定位参数
119
-        locationClient.setLocationOption(locationOption);
120
-        // 启动定位
121
-        locationClient.startLocation();
122
-
123
-        status = 1;
124 213
     }
125 214
 
126
-    private void stopLocation(){
127
-        if(status == 2){
128
-            return;
129
-        }
130
-        // 停止定位
131
-        locationClient.stopLocation();
132
-        status = 2;
133
-    }
134 215
 
135 216
     private void destroyLocation(){
136 217
         if (null != locationClient) {
218
+            locationClient.stopLocation();
137 219
             locationClient.onDestroy();
138 220
             locationClient = null;
139 221
             locationOption = null;
140
-            status = 0;
141 222
         }
223
+        mHandler.removeCallbacksAndMessages(null);
142 224
     }
143 225
 
144 226
 private void reportLocationWhenTourMode(double lat, double lon){
227
+    if(locationClient!=null){
228
+        locationClient.stopLocation();
229
+    }
145 230
     if(lat == lastLat && lon ==lastLon && repeatTime<5){
146 231
         repeatTime++;
147 232
         return;
@@ -149,18 +234,26 @@ private void reportLocationWhenTourMode(double lat, double lon){
149 234
     repeatTime = 0;
150 235
     lastLat = lat;
151 236
     lastLon = lon;
152
-    if(Preferences.getInstance(this).isTourMode()){
153
-        HashMap<String,String> params = new HashMap<String,String>();
154
-        String userId = Preferences.getInstance(this).getUserId();
155
-        if(userId.length()==0){
156
-            return;
157
-        }
158
-        params.put("user_id",userId);
159
-        params.put("lon",String.valueOf(lon));
160
-        params.put("lat",String.valueOf(lat));
161
-        new HttpPostTask(this,params).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.GEO_LOCATION_URL);
162
-        LogHelper.dfile("czy","上报经纬度位置 lat = "+lat+"lon="+lon);
237
+    HashMap<String,String> params = new HashMap<String,String>();
238
+    String userId = Preferences.getInstance(this).getUserId();
239
+    if(userId.length()==0){
240
+        return;
163 241
     }
242
+    params.put("user_id",userId);
243
+    params.put("lon",String.valueOf(lon));
244
+    params.put("lat",String.valueOf(lat));
245
+    new HttpPostTask(this,params).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.GEO_LOCATION_URL);
164 246
 }
165 247
 
248
+    @Override
249
+    public boolean handleMessage(Message msg) {
250
+        if(msg.what == MSG_FETCH_TOUR_INFO){
251
+            fetchTourInfo();
252
+            return true;
253
+        }else if(msg.what == MSG_START_LOCATION){
254
+            startLocation();
255
+            return true;
256
+        }
257
+        return false;
258
+    }
166 259
 }

kodo - Gogs: Go Git Service

Nav apraksta

FFIB: 06caafb5e7 新增摄影师类型字段 2 gadi atpakaļ
..
0001_initial.py eb1f19f209 add # -*- coding: utf-8 -*- 2 gadi atpakaļ
0002_auto_20240814_1821.py 26bef94de6 add get_contribtion_contract_status_api接口 2 gadi atpakaļ
0003_alter_lensmancontributioncontractinfo_contract_content_fields.py 06caafb5e7 新增摄影师类型字段 2 gadi atpakaļ
__init__.py 2fdf901626 新增摄影师合同相关接口 2 gadi atpakaļ