Commit 24d280d8b4e4ca8251c5dbcba3ee582b0200c0bb
1 parent
654f65fb
1.取消跑步页面恢复功能
Showing
31 changed files
with
2093 additions
and
174 deletions
moudle_pedometer/src/main/AndroidManifest.xml
| ... | ... | @@ -128,6 +128,46 @@ |
| 128 | 128 | android:process=":step" /> |
| 129 | 129 | |
| 130 | 130 | <!--路径工具--> |
| 131 | + <service | |
| 132 | + android:name="com.cnlive.moudle.collectionTrace.service.CollKeepLiveService" | |
| 133 | + android:enabled="true" | |
| 134 | + android:exported="true" | |
| 135 | + android:priority="1000" | |
| 136 | + android:process=":re" /> | |
| 137 | + <service | |
| 138 | + android:name="com.cnlive.moudle.collectionTrace.service.CollStepService" | |
| 139 | + android:enabled="true" | |
| 140 | + android:exported="true" | |
| 141 | + android:priority="1000" | |
| 142 | + android:process=":step" | |
| 143 | + android:stopWithTask="false"> | |
| 144 | + <intent-filter> | |
| 145 | + | |
| 146 | + <!-- 系统启动完成后会调用 --> | |
| 147 | + <action android:name="android.intent.action.BOOT_COMPLETED" /> | |
| 148 | + <action android:name="android.intent.action.DATE_CHANGED" /> | |
| 149 | + <action android:name="android.intent.action.MEDIA_MOUNTED" /> | |
| 150 | + <action android:name="android.intent.action.USER_PRESENT" /> | |
| 151 | + <action android:name="android.intent.action.ACTION_TIME_TICK" /> | |
| 152 | + <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> | |
| 153 | + <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> | |
| 154 | + </intent-filter> | |
| 155 | + <intent-filter android:priority="1000"> | |
| 156 | + <action android:name="com.cnlive.moudle.pedometer.service.StepService" /> | |
| 157 | + </intent-filter> | |
| 158 | + </service> | |
| 159 | + | |
| 160 | + <receiver | |
| 161 | + android:name="com.cnlive.moudle.collectionTrace.receivers.CollServiceReceiver" | |
| 162 | + android:enabled="true" | |
| 163 | + android:exported="true"> | |
| 164 | + <intent-filter android:priority="1000"> | |
| 165 | + <action android:name="com.cnlive.strike.receiver.ServiceReceiver" /> | |
| 166 | + <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> | |
| 167 | + <action android:name="android.net.wifi.STATE_CHANGE" /> | |
| 168 | + <action android:name="com.example.myservice.MyReceiver" /> | |
| 169 | + </intent-filter> | |
| 170 | + </receiver> | |
| 131 | 171 | <activity |
| 132 | 172 | android:name="com.cnlive.moudle.collectionTrace.ui.activity.CollPedometerMainActivity" |
| 133 | 173 | android:launchMode="singleTask" | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/frame/presenter/CollRunningMapFragmentPresenter.java
| ... | ... | @@ -406,8 +406,8 @@ public class CollRunningMapFragmentPresenter extends MvpBasePresenter<CollRunnin |
| 406 | 406 | |
| 407 | 407 | |
| 408 | 408 | // //获取围栏列表响应结果 |
| 409 | -// DeleteFenceRequest deleteFenceRequest = DeleteFenceRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null); | |
| 410 | -// StepService.mClient.deleteFence(deleteFenceRequest,this); | |
| 409 | +// DeleteFenceRequest deleteFenceRequest = DeleteFenceRequest.buildServerRequest(1, CollStepService.serviceId, "yangshuai", null); | |
| 410 | +// CollStepService.mClient.deleteFence(deleteFenceRequest,this); | |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | // 监控状态回调 |
| ... | ... | @@ -473,10 +473,10 @@ public class CollRunningMapFragmentPresenter extends MvpBasePresenter<CollRunnin |
| 473 | 473 | } |
| 474 | 474 | }; |
| 475 | 475 | // 创建本地圆形围栏 |
| 476 | -// if (StepService.mClient != null) { | |
| 476 | +// if (CollStepService.mClient != null) { | |
| 477 | 477 | // if (createFenceRequest == null) { |
| 478 | -// createFenceRequest = CreateFenceRequest.buildServerPolylineRequest(1, StepService.serviceId, "test", "yangshuai", latLngs, offset, denoise, CoordType.gcj02); | |
| 479 | -// StepService.mClient.createFence(createFenceRequest, onFenceListener); | |
| 478 | +// createFenceRequest = CreateFenceRequest.buildServerPolylineRequest(1, CollStepService.serviceId, "test", "yangshuai", latLngs, offset, denoise, CoordType.gcj02); | |
| 479 | +// CollStepService.mClient.createFence(createFenceRequest, onFenceListener); | |
| 480 | 480 | // } |
| 481 | 481 | // } |
| 482 | 482 | |
| ... | ... | @@ -506,10 +506,10 @@ public class CollRunningMapFragmentPresenter extends MvpBasePresenter<CollRunnin |
| 506 | 506 | * 以查询服务端围栏为例 |
| 507 | 507 | * fenceIds:服务端围栏编号列表,List<Long>,如传入null,表示查询所有围栏 |
| 508 | 508 | */ |
| 509 | -// FenceListRequest request = FenceListRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 509 | +// FenceListRequest request = FenceListRequest.buildServerRequest(1, CollStepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 510 | 510 | // //发起查询围栏请求 |
| 511 | -// if (StepService.mClient != null) { | |
| 512 | -// StepService.mClient.queryFenceList(request, fenceListener); | |
| 511 | +// if (CollStepService.mClient != null) { | |
| 512 | +// CollStepService.mClient.queryFenceList(request, fenceListener); | |
| 513 | 513 | // } |
| 514 | 514 | } |
| 515 | 515 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/frame/view/CollRunMainFragmentView.java
| ... | ... | @@ -6,6 +6,7 @@ import android.os.Handler; |
| 6 | 6 | import android.os.SystemClock; |
| 7 | 7 | import android.support.v4.content.ContextCompat; |
| 8 | 8 | import android.text.TextUtils; |
| 9 | +import android.util.Log; | |
| 9 | 10 | import android.view.MotionEvent; |
| 10 | 11 | import android.view.View; |
| 11 | 12 | import android.widget.Chronometer; |
| ... | ... | @@ -14,20 +15,20 @@ import com.baidu.location.BDAbstractLocationListener; |
| 14 | 15 | import com.baidu.location.BDLocation; |
| 15 | 16 | import com.baidu.location.LocationClient; |
| 16 | 17 | import com.baidu.location.LocationClientOption; |
| 18 | +import com.cnlive.moudle.collectionTrace.service.CollKeepLiveService; | |
| 19 | +import com.cnlive.moudle.collectionTrace.service.CollStepService; | |
| 17 | 20 | import com.cnlive.moudle.collectionTrace.ui.fragment.CollRunMainFragment; |
| 18 | -import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | |
| 21 | +import com.cnlive.moudle.pedometer.commonInfo.CollCommonInfo; | |
| 19 | 22 | import com.cnlive.moudle.pedometer.model.Constant; |
| 20 | 23 | import com.cnlive.moudle.pedometer.model.MessageEvent; |
| 21 | 24 | import com.cnlive.moudle.pedometer.model.RunningFinishBean; |
| 22 | 25 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 23 | -import com.cnlive.moudle.pedometer.service.StepService; | |
| 24 | -import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; | |
| 25 | -import com.cnlive.moudle.pedometer.ui.activity.RunningFinishActivity; | |
| 26 | +import com.cnlive.moudle.pedometer.sql.entity.CollUserStepEntity; | |
| 26 | 27 | import com.cnlive.moudle.pedometer.ui.activity.RunningMapActivity; |
| 27 | -import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; | |
| 28 | 28 | import com.cnlive.moudle.pedometer.ui.widget.SelectDialog; |
| 29 | 29 | import com.cnlive.moudle.pedometer.utils.ButtonAnimUtil; |
| 30 | -import com.cnlive.moudle.pedometer.utils.StepUtil; | |
| 30 | +import com.cnlive.moudle.pedometer.utils.CollStepUtil; | |
| 31 | +import com.cnlive.moudle.pedometer.utils.GsonUtil; | |
| 31 | 32 | import com.cnlive.moudle.pedometer.utils.TimeUtil; |
| 32 | 33 | import com.example.moudle_pedometer.R; |
| 33 | 34 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| ... | ... | @@ -106,8 +107,12 @@ public class CollRunMainFragmentView implements MvpView { |
| 106 | 107 | */ |
| 107 | 108 | public void initView() { |
| 108 | 109 | uid = "yangshuai"; |
| 109 | - CommonInfo.setUserId(getContext(), uid); | |
| 110 | + CollCommonInfo.setUserId(getContext(), uid); | |
| 110 | 111 | initLocationClient(); |
| 112 | + CollUserStepEntity userStepEntity = CollStepUtil.getCollUserStepData(getContext(), uid); | |
| 113 | + if (userStepEntity != null) { | |
| 114 | + CollStepUtil.clearRunData(getContext(), CollCommonInfo.getUserId(getContext())); | |
| 115 | + } | |
| 111 | 116 | fragment.binding.llRunMainRoot.setVisibility(View.GONE); |
| 112 | 117 | ButtonAnimUtil.startAnimation(fragment.binding.llStartTimeTip, false); |
| 113 | 118 | firstTimeFlag = true; |
| ... | ... | @@ -168,37 +173,43 @@ public class CollRunMainFragmentView implements MvpView { |
| 168 | 173 | |
| 169 | 174 | } |
| 170 | 175 | }); |
| 171 | - UserStepEntity userStepEntity = StepUtil.getUserStepData(getContext(), uid); | |
| 172 | - boolean isRunStep = CommonInfo.getIsRunStepService(getContext()); | |
| 176 | + | |
| 177 | + boolean isRunStep = CollCommonInfo.getIsRunStepService(getContext()); | |
| 173 | 178 | //当前用户已经在运动 |
| 174 | - if (!isRunStep) { | |
| 175 | - //依次倒计时 | |
| 176 | - handler.postDelayed(runnable, 1000); | |
| 177 | - } else { | |
| 178 | - //隐藏计时界面 | |
| 179 | - fragment.binding.llStartTimeTip.setVisibility(View.GONE); | |
| 180 | - fragment.binding.llRunMainRoot.setVisibility(View.VISIBLE); | |
| 181 | - new Handler().postDelayed(new Runnable() { | |
| 182 | - @Override | |
| 183 | - public void run() { | |
| 184 | - //开始计时 | |
| 185 | - if (firstTimeFlag) { | |
| 186 | - firstTimeFlag = false; | |
| 187 | - if (userStepEntity != null) { | |
| 188 | - if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | |
| 189 | - fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | |
| 190 | - } else { | |
| 191 | - fragment.binding.chronometer.setFormat("00:00:01"); | |
| 192 | - fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | |
| 193 | - } | |
| 194 | - } | |
| 195 | - fragment.binding.chronometer.start(); | |
| 196 | - } | |
| 197 | - } | |
| 198 | - }, 10); | |
| 199 | - //开启计步服务 | |
| 200 | - startStepService(getContext(), uid); | |
| 201 | - } | |
| 179 | +// if (!isRunStep) { | |
| 180 | + //依次倒计时 | |
| 181 | + handler.postDelayed(runnable, 1000); | |
| 182 | +// } | |
| 183 | +// else { | |
| 184 | +// //隐藏计时界面 | |
| 185 | +// fragment.binding.llStartTimeTip.setVisibility(View.GONE); | |
| 186 | +// fragment.binding.llRunMainRoot.setVisibility(View.VISIBLE); | |
| 187 | +// new Handler().postDelayed(new Runnable() { | |
| 188 | +// @Override | |
| 189 | +// public void run() { | |
| 190 | +// //开始计时 | |
| 191 | +// if (firstTimeFlag) { | |
| 192 | +// firstTimeFlag = false; | |
| 193 | +// if (userStepEntity != null) { | |
| 194 | +// Log.e(TAG, "run: "+ GsonUtil.GsonString(userStepEntity)); | |
| 195 | +// if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | |
| 196 | +// fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | |
| 197 | +// } else { | |
| 198 | +// fragment.binding.chronometer.setFormat("00:00:01"); | |
| 199 | +// fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | |
| 200 | +// } | |
| 201 | +// //设置步数 | |
| 202 | +// if (!TextUtils.isEmpty(userStepEntity.getStepCount())) { | |
| 203 | +// fragment.binding.tvStepCount.setText(userStepEntity.getStepCount()); | |
| 204 | +// } | |
| 205 | +// } | |
| 206 | +// fragment.binding.chronometer.start(); | |
| 207 | +// } | |
| 208 | +// } | |
| 209 | +// }, 100); | |
| 210 | +// //开启计步服务 | |
| 211 | +// startStepService(getContext(), uid); | |
| 212 | +// } | |
| 202 | 213 | |
| 203 | 214 | //设置继续按钮,缩放效果 |
| 204 | 215 | fragment.binding.flContinue.setOnTouchListener(new View.OnTouchListener() { |
| ... | ... | @@ -226,8 +237,9 @@ public class CollRunMainFragmentView implements MvpView { |
| 226 | 237 | @Override |
| 227 | 238 | public void onClick(View view) { |
| 228 | 239 | //继续记录轨迹 |
| 229 | - if (StepService.mClient != null && StepService.traceListener != null) { | |
| 230 | - StepService.mClient.startTrace(StepService.mTrace, StepService.traceListener); | |
| 240 | + if (CollStepService.mClient != null && CollStepService.traceListener != null) { | |
| 241 | + CollCommonInfo.setTsRunBaiduTraceService(getContext(), true); | |
| 242 | + CollStepService.mClient.startTrace(CollStepService.mTrace, CollStepService.traceListener); | |
| 231 | 243 | } |
| 232 | 244 | needWriteDataFlag = true; |
| 233 | 245 | //将当前计时器时间戳写入数据库 |
| ... | ... | @@ -292,16 +304,17 @@ public class CollRunMainFragmentView implements MvpView { |
| 292 | 304 | SelectDialog selectDialog = new SelectDialog(fragment.getActivity(), "确定结束运动吗?", "确定", new SelectDialog.DialogInterface() { |
| 293 | 305 | @Override |
| 294 | 306 | public void onClick(SelectDialog dialog) { |
| 295 | - CommonInfo.setIsWriteUserStep(getContext(), false); | |
| 307 | + CollCommonInfo.setIsRunKeepAliveService(getContext(), false); | |
| 308 | + CollCommonInfo.setIsWriteUserStep(getContext(), false); | |
| 296 | 309 | fragment.binding.cpCurrent.setVisibility(View.GONE); |
| 297 | 310 | ButtonAnimUtil.setViewDefault(fragment.binding.flStop); |
| 298 | 311 | //清空数据 |
| 299 | - CommonInfo.reset(getContext()); | |
| 312 | + CollCommonInfo.reset(getContext()); | |
| 300 | 313 | dialog.dismiss(); |
| 301 | - if (StepService.mClient != null) { | |
| 302 | - StepService.mClient.stopGather(StepService.traceListener); | |
| 314 | + if (CollStepService.mClient != null) { | |
| 315 | + CollStepService.mClient.stopGather(CollStepService.traceListener); | |
| 303 | 316 | } |
| 304 | - fragment.getActivity().stopService(new Intent(fragment.getActivity(), StepService.class)); | |
| 317 | + fragment.getActivity().stopService(new Intent(fragment.getActivity(), CollStepService.class)); | |
| 305 | 318 | RunningFinishBean runningFinishBean = new RunningFinishBean(); |
| 306 | 319 | runningFinishBean.setStartPoint("开始地点"); |
| 307 | 320 | runningFinishBean.setEndPoint("结束地点"); |
| ... | ... | @@ -310,7 +323,7 @@ public class CollRunMainFragmentView implements MvpView { |
| 310 | 323 | runningFinishBean.setStep(fragment.binding.tvStepCount.getText().toString()); |
| 311 | 324 | runningFinishBean.setTime(fragment.binding.chronometer.getText().toString()); |
| 312 | 325 | runningFinishBean.setDate(TimeUtil.getFormatNowTime()); |
| 313 | - RunningFinishActivity.startActivity(fragment.getActivity(), runningFinishBean); | |
| 326 | +// RunningFinishActivity.startActivity(fragment.getActivity(), runningFinishBean); | |
| 314 | 327 | fragment.getActivity().finish(); |
| 315 | 328 | } |
| 316 | 329 | }, "取消", new SelectDialog.DialogInterface() { |
| ... | ... | @@ -364,8 +377,9 @@ public class CollRunMainFragmentView implements MvpView { |
| 364 | 377 | @Override |
| 365 | 378 | public void onClick(View view) { |
| 366 | 379 | //暂停轨迹记录 |
| 367 | - if (StepService.mClient != null && StepService.traceListener != null) { | |
| 368 | - StepService.mClient.stopGather(StepService.traceListener); | |
| 380 | + if (CollStepService.mClient != null && CollStepService.traceListener != null) { | |
| 381 | + CollCommonInfo.setTsRunBaiduTraceService(getContext(), false); | |
| 382 | + CollStepService.mClient.stopGather(CollStepService.traceListener); | |
| 369 | 383 | } |
| 370 | 384 | //将当前计时器时间戳写入数据库 |
| 371 | 385 | setSQLPauseTime(uid); |
| ... | ... | @@ -427,13 +441,13 @@ public class CollRunMainFragmentView implements MvpView { |
| 427 | 441 | //将当前计时器时间戳写入数据库 |
| 428 | 442 | // String id = MySpHelper.getInstance(getContext(), Context.MODE_MULTI_PROCESS).getString("userId", ""); |
| 429 | 443 | if (!TextUtils.isEmpty(id)) { |
| 430 | - UserStepEntity userStepEntity = StepUtil.getUserStepData(fragment.getActivity(), uid); | |
| 444 | + CollUserStepEntity userStepEntity = CollStepUtil.getCollUserStepData(fragment.getActivity(), uid); | |
| 431 | 445 | if (userStepEntity != null) { |
| 432 | 446 | userStepEntity.setPauseTimeStamp(fragment.binding.chronometer.getBase()); |
| 433 | 447 | //设置当前暂停的时间戳 |
| 434 | 448 | userStepEntity.setRecordTime(SystemClock.elapsedRealtime()); |
| 435 | 449 | //更新数据 |
| 436 | - StepUtil.updateStepData(fragment.getActivity(), userStepEntity); | |
| 450 | + CollStepUtil.updateStepData(fragment.getActivity(), userStepEntity); | |
| 437 | 451 | } |
| 438 | 452 | } |
| 439 | 453 | } |
| ... | ... | @@ -445,11 +459,14 @@ public class CollRunMainFragmentView implements MvpView { |
| 445 | 459 | * @param userId |
| 446 | 460 | */ |
| 447 | 461 | public void startStepService(Context context, String userId) { |
| 448 | - if (CommonInfo.getStepStartTime(context) == 0) { | |
| 449 | - CommonInfo.setStepStartTime(context, TimeUtil.getNowTimeStamp()); | |
| 462 | + if (CollCommonInfo.getStepStartTime(context) == 0) { | |
| 463 | + CollCommonInfo.setStepStartTime(context, TimeUtil.getNowTimeStamp()); | |
| 450 | 464 | } |
| 451 | - Intent intent = new Intent(fragment.getActivity(), StepService.class); | |
| 452 | - intent.putExtra("userId", userId); | |
| 465 | + CollCommonInfo.setIsRunKeepAliveService(context, true); | |
| 466 | + CollCommonInfo.setIsRunStepService(context, true); | |
| 467 | + CollCommonInfo.setTsRunBaiduTraceService(context, true); | |
| 468 | + CollCommonInfo.setIsWriteUserStep(context, true); | |
| 469 | + Intent intent = new Intent(fragment.getActivity(), CollKeepLiveService.class); | |
| 453 | 470 | ContextCompat.startForegroundService(fragment.getActivity(), intent); |
| 454 | 471 | |
| 455 | 472 | } |
| ... | ... | @@ -461,14 +478,13 @@ public class CollRunMainFragmentView implements MvpView { |
| 461 | 478 | */ |
| 462 | 479 | public void updateRunDistance(int step) { |
| 463 | 480 | //设置行走公里 |
| 464 | - fragment.binding.tvRunDistance.setText(StepUtil.getWalkDistanceKM(step) + ""); | |
| 481 | + fragment.binding.tvRunDistance.setText(CollStepUtil.getWalkDistanceKM(step) + ""); | |
| 465 | 482 | //设置行走步数 |
| 466 | 483 | fragment.binding.tvStepCount.setText(step + ""); |
| 467 | 484 | //设置S速度 |
| 468 | 485 | int second = (int) ((SystemClock.elapsedRealtime() - fragment.binding.chronometer.getBase()) / 1000); |
| 469 | 486 | if (second != 0) { |
| 470 | -// fragment.binding.tvSpeed.setText(StepUtil.getCurrentSpeed(second, StepUtil.getWalkDistanceKM(step) * 1000)+""); | |
| 471 | - fragment.binding.tvSpeed.setText(StepUtil.getPaceSpeed(second, StepUtil.getWalkDistanceKM(step))); | |
| 487 | + fragment.binding.tvSpeed.setText(CollStepUtil.getPaceSpeed(second, CollStepUtil.getWalkDistanceKM(step))); | |
| 472 | 488 | } else { |
| 473 | 489 | fragment.binding.tvSpeed.setText("--"); |
| 474 | 490 | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/frame/view/CollRunningMapFragmentView.java
| ... | ... | @@ -38,8 +38,8 @@ import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 38 | 38 | import com.cnlive.moudle.pedometer.ui.fragment.RunningMapFragment; |
| 39 | 39 | import com.cnlive.moudle.pedometer.utils.BitmapUtil; |
| 40 | 40 | import com.cnlive.moudle.pedometer.utils.ButtonAnimUtil; |
| 41 | +import com.cnlive.moudle.pedometer.utils.CollStepUtil; | |
| 41 | 42 | import com.cnlive.moudle.pedometer.utils.MyMapUtil; |
| 42 | -import com.cnlive.moudle.pedometer.utils.StepUtil; | |
| 43 | 43 | import com.example.moudle_pedometer.R; |
| 44 | 44 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| 45 | 45 | |
| ... | ... | @@ -47,7 +47,7 @@ import java.util.ArrayList; |
| 47 | 47 | import java.util.List; |
| 48 | 48 | |
| 49 | 49 | public class CollRunningMapFragmentView implements MvpView, SensorEventListener { |
| 50 | - private static final String TAG = "CollRunningMapFragmentView"; | |
| 50 | + private static final String TAG = "CollRunningMap"; | |
| 51 | 51 | private CollRunningMapFragment fragment; |
| 52 | 52 | public BaiduMap baiduMap; |
| 53 | 53 | private MapView mMapView; |
| ... | ... | @@ -401,14 +401,13 @@ public class CollRunningMapFragmentView implements MvpView, SensorEventListener |
| 401 | 401 | */ |
| 402 | 402 | public void updateRunDistance(int step) { |
| 403 | 403 | //设置行走公里 |
| 404 | - fragment.binding.tvCurrentDiatance.setText(StepUtil.getWalkDistanceKM(step) + ""); | |
| 404 | + fragment.binding.tvCurrentDiatance.setText(CollStepUtil.getWalkDistanceKM(step) + ""); | |
| 405 | 405 | //设置行走步数 |
| 406 | 406 | fragment.binding.tvCurrentSpeed.setText(step + "步"); |
| 407 | 407 | //设置S速度 |
| 408 | 408 | int second = (int) ((SystemClock.elapsedRealtime() - fragment.binding.chronometer.getBase()) / 1000); |
| 409 | 409 | if (second != 0) { |
| 410 | -// fragment.binding.tvCurrentSpeed.setText(StepUtil.getCurrentSpeed(second, StepUtil.getWalkDistanceKM(step) * 1000) + ""); | |
| 411 | - fragment.binding.tvCurrentSpeed.setText(StepUtil.getPaceSpeed(second, StepUtil.getWalkDistanceKM(step))); | |
| 410 | + fragment.binding.tvCurrentSpeed.setText(CollStepUtil.getPaceSpeed(second, CollStepUtil.getWalkDistanceKM(step))); | |
| 412 | 411 | } else { |
| 413 | 412 | fragment.binding.tvCurrentSpeed.setText("--"); |
| 414 | 413 | } |
| ... | ... | @@ -440,6 +439,21 @@ public class CollRunningMapFragmentView implements MvpView, SensorEventListener |
| 440 | 439 | |
| 441 | 440 | } |
| 442 | 441 | |
| 442 | + /** | |
| 443 | + * 设置从历史轨迹中查询到的最新点 | |
| 444 | + * | |
| 445 | + * @param lastPoint | |
| 446 | + */ | |
| 447 | + public void setHistoryLastPoint(LatLng lastPoint) { | |
| 448 | + if (currentPoints != null) { | |
| 449 | + currentPoints.clear(); | |
| 450 | + currentPoints.add(lastPoint); | |
| 451 | + } else { | |
| 452 | + currentPoints = new ArrayList<>(); | |
| 453 | + currentPoints.add(lastPoint); | |
| 454 | + } | |
| 455 | + } | |
| 456 | + | |
| 443 | 457 | |
| 444 | 458 | public void onResume() { |
| 445 | 459 | if (mMapView != null) { | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/frame/view/StartTraceFragmentView.java
| ... | ... | @@ -26,6 +26,8 @@ import com.baidu.mapapi.map.MyLocationData; |
| 26 | 26 | import com.baidu.mapapi.map.UiSettings; |
| 27 | 27 | import com.baidu.mapapi.model.LatLng; |
| 28 | 28 | import com.baidu.mapapi.search.route.RoutePlanSearch; |
| 29 | +import com.cnlive.moudle.collectionTrace.ui.activity.CollPedometerMainActivity; | |
| 30 | +import com.cnlive.moudle.collectionTrace.ui.activity.CollRunRaceDetailActivity; | |
| 29 | 31 | import com.cnlive.moudle.collectionTrace.ui.fragment.CollRunRaceDetailFragment; |
| 30 | 32 | import com.cnlive.moudle.collectionTrace.ui.fragment.StartTraceFragment; |
| 31 | 33 | import com.cnlive.moudle.pedometer.ui.widget.SelectDialog; |
| ... | ... | @@ -79,7 +81,10 @@ public class StartTraceFragmentView implements MvpView, SensorEventListener { |
| 79 | 81 | }, "是", new SelectDialog.DialogInterface() { |
| 80 | 82 | @Override |
| 81 | 83 | public void onClick(SelectDialog dialog) { |
| 84 | + dialog.dismiss(); | |
| 82 | 85 | isShowFollowAddress = false; |
| 86 | + CollPedometerMainActivity.startActivity(fragment.getActivity()); | |
| 87 | + fragment.getActivity().finish(); | |
| 83 | 88 | } |
| 84 | 89 | }); |
| 85 | 90 | selectDialog.setCancelable(false); | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/receivers/CollServiceReceiver.java
0 → 100644
| 1 | +package com.cnlive.moudle.collectionTrace.receivers; | |
| 2 | + | |
| 3 | +import android.annotation.SuppressLint; | |
| 4 | +import android.content.BroadcastReceiver; | |
| 5 | +import android.content.Context; | |
| 6 | +import android.content.Intent; | |
| 7 | +import android.os.Build; | |
| 8 | +import android.os.PowerManager; | |
| 9 | +import android.util.Log; | |
| 10 | + | |
| 11 | +import com.cnlive.moudle.collectionTrace.service.CollKeepLiveService; | |
| 12 | +import com.cnlive.moudle.collectionTrace.service.CollStepService; | |
| 13 | +import com.cnlive.moudle.pedometer.commonInfo.CollCommonInfo; | |
| 14 | +import com.cnlive.moudle.pedometer.utils.ServiceUtils; | |
| 15 | + | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * @author ShinnyYang | |
| 19 | + */ | |
| 20 | +public class CollServiceReceiver extends BroadcastReceiver { | |
| 21 | + //唤醒锁 | |
| 22 | + private PowerManager.WakeLock wakeLock = null; | |
| 23 | + private final String TAG = "CollServiceReceiver"; | |
| 24 | + | |
| 25 | + public CollServiceReceiver() { | |
| 26 | + | |
| 27 | + } | |
| 28 | + | |
| 29 | + public CollServiceReceiver(PowerManager.WakeLock wakeLock) { | |
| 30 | + super(); | |
| 31 | + this.wakeLock = wakeLock; | |
| 32 | + } | |
| 33 | + | |
| 34 | + @SuppressLint("Wakelock") | |
| 35 | + @Override | |
| 36 | + public void onReceive(Context context, Intent intent) { | |
| 37 | + Log.e(TAG, "接收到广播:" + intent.getAction()); | |
| 38 | + //todo 这里判断要保活的服务 | |
| 39 | + boolean isRunStepService = CollCommonInfo.getIsRunStepService(context); | |
| 40 | + if (isRunStepService) { | |
| 41 | + if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.collectionTrace.service.CollStepService")) { | |
| 42 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 43 | + context.startForegroundService(new Intent(context, CollStepService.class)); | |
| 44 | + } else { | |
| 45 | + context.startService(new Intent(context, CollStepService.class)); | |
| 46 | + } | |
| 47 | + }else { | |
| 48 | + Log.e(TAG, "计步服务已经在运行"); | |
| 49 | + | |
| 50 | + } | |
| 51 | + //判断百度鹰眼是否运行 | |
| 52 | + boolean isRunBaiduTrace = CollCommonInfo.getIsRunBaiduTraceService(context); | |
| 53 | + if (isRunBaiduTrace) { | |
| 54 | + if (!ServiceUtils.isServiceRunning(context, "com.baidu.trace.LBSTraceService")) { | |
| 55 | + if (CollStepService.mClient != null && CollStepService.mTrace != null && CollStepService.traceListener != null) { | |
| 56 | + CollStepService.mClient.startTrace(CollStepService.mTrace, CollStepService.traceListener); | |
| 57 | + } | |
| 58 | + }else { | |
| 59 | + Log.e(TAG, "鹰眼服务已经在运行"); | |
| 60 | + | |
| 61 | + } | |
| 62 | + } | |
| 63 | + } | |
| 64 | + //todo 保活 | |
| 65 | + boolean isRunKeepAliveService = CollCommonInfo.getIsRunKeepAliveService(context); | |
| 66 | + if (isRunKeepAliveService) { | |
| 67 | + if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.collectionTrace.service.CollKeepLiveService")) { | |
| 68 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 69 | + context.startForegroundService(new Intent(context, CollKeepLiveService.class)); | |
| 70 | + } else { | |
| 71 | + context.startService(new Intent(context, CollKeepLiveService.class)); | |
| 72 | + } | |
| 73 | + Log.e(TAG, "启动KeepLiveService"); | |
| 74 | + } else { | |
| 75 | + Log.e(TAG, "KeepLiveService已经在运行"); | |
| 76 | + | |
| 77 | + } | |
| 78 | + String action = intent.getAction(); | |
| 79 | + //锁屏 | |
| 80 | + if (Intent.ACTION_SCREEN_OFF.equals(action)) { | |
| 81 | + | |
| 82 | + if (null != wakeLock && !(wakeLock.isHeld())) { | |
| 83 | + wakeLock.acquire(); | |
| 84 | + Log.e(TAG, "启动唤醒锁"); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + //亮屏 | |
| 88 | + else if (Intent.ACTION_SCREEN_ON.equals(action) || Intent.ACTION_USER_PRESENT.equals(action)) { | |
| 89 | + | |
| 90 | + if (null != wakeLock && wakeLock.isHeld()) { | |
| 91 | + wakeLock.release(); | |
| 92 | + Log.e(TAG, "重置唤醒锁"); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + } else { | |
| 96 | + Log.e(TAG, "接收到广播,什么都不做"); | |
| 97 | + | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | +// /** | |
| 102 | +// * 发送关闭广播 | |
| 103 | +// */ | |
| 104 | +// private void sendCloseBroadCast(Context context) { | |
| 105 | +// Intent intent = new Intent("close.CollStepService"); | |
| 106 | +// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 107 | +// intent.setComponent(new ComponentName(context.getPackageName(), "com.cnlive.moudle.pedometer.service.CollStepService"));//高版本Android发送广播需要加这句 | |
| 108 | +// } | |
| 109 | +// context.sendBroadcast(intent); | |
| 110 | +// } | |
| 111 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/service/CollKeepLiveService.java
0 → 100644
| 1 | +package com.cnlive.moudle.collectionTrace.service; | |
| 2 | + | |
| 3 | +import android.annotation.SuppressLint; | |
| 4 | +import android.app.Notification; | |
| 5 | +import android.app.NotificationChannel; | |
| 6 | +import android.app.NotificationManager; | |
| 7 | +import android.app.Service; | |
| 8 | +import android.content.ComponentName; | |
| 9 | +import android.content.Context; | |
| 10 | +import android.content.Intent; | |
| 11 | +import android.content.IntentFilter; | |
| 12 | +import android.media.MediaPlayer; | |
| 13 | +import android.os.Build; | |
| 14 | +import android.os.Handler; | |
| 15 | +import android.os.IBinder; | |
| 16 | +import android.os.PowerManager; | |
| 17 | +import android.support.v4.app.NotificationCompat; | |
| 18 | +import android.util.Log; | |
| 19 | + | |
| 20 | +import com.cnlive.moudle.collectionTrace.receivers.CollServiceReceiver; | |
| 21 | +import com.cnlive.moudle.pedometer.commonInfo.CollCommonInfo; | |
| 22 | +import com.cnlive.moudle.pedometer.utils.ServiceUtils; | |
| 23 | +import com.example.moudle_pedometer.R; | |
| 24 | + | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * @author ShinnyYang | |
| 28 | + * 维持后台运行的服务 | |
| 29 | + */ | |
| 30 | +public class CollKeepLiveService extends Service implements MediaPlayer.OnCompletionListener { | |
| 31 | + //唤醒锁 | |
| 32 | + private PowerManager.WakeLock wakeLock = null; | |
| 33 | + //电源管理器 | |
| 34 | + private PowerManager powerManager = null; | |
| 35 | + //音频播放器 | |
| 36 | + public static MediaPlayer mMediaPlayer; | |
| 37 | + //广播 | |
| 38 | + private static CollServiceReceiver myReceiver; | |
| 39 | + private String CHANNEL_ONE_ID = "com.cnlive.keep"; | |
| 40 | + private String CHANNEL_ONE_NAME = "KeepLive"; | |
| 41 | + private NotificationManager mNM; | |
| 42 | + private Notification notification = null; | |
| 43 | + //是否在后台运行 | |
| 44 | + public static boolean isKeepService = true; | |
| 45 | + public static CollKeepLiveService instance; | |
| 46 | + private int startId; | |
| 47 | + private final String TAG = "CollKeepLiveService"; | |
| 48 | + private Handler handler; | |
| 49 | + private Runnable runnable; | |
| 50 | + | |
| 51 | + public CollKeepLiveService() { | |
| 52 | + } | |
| 53 | + | |
| 54 | + public static CollKeepLiveService getInstance() { | |
| 55 | + if (instance == null) { | |
| 56 | + instance = new CollKeepLiveService(); | |
| 57 | + return instance; | |
| 58 | + } else { | |
| 59 | + return instance; | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void onCreate() { | |
| 65 | + super.onCreate(); | |
| 66 | + instance = this; | |
| 67 | + init(); | |
| 68 | + initBroadCastReceiver(); | |
| 69 | + handler = new Handler(); | |
| 70 | + runnable = new Runnable() { | |
| 71 | + @Override | |
| 72 | + public void run() { | |
| 73 | + //跨进程读取配置文件 | |
| 74 | + boolean isRun = CollCommonInfo.getIsRunKeepAliveService(getApplicationContext()); | |
| 75 | + if (isRun) { | |
| 76 | + handler.postDelayed(this, 5000); | |
| 77 | + } else { | |
| 78 | + try { | |
| 79 | + if (myReceiver != null) { | |
| 80 | + unregisterReceiver(myReceiver); | |
| 81 | + } | |
| 82 | + } catch (Exception e) { | |
| 83 | + Log.e(TAG, "run: "); | |
| 84 | + } | |
| 85 | + stopPlaySong(); | |
| 86 | + stopForeground(true); | |
| 87 | + } | |
| 88 | + | |
| 89 | + } | |
| 90 | + }; | |
| 91 | + | |
| 92 | + } | |
| 93 | + | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * 初始化广播 | |
| 97 | + */ | |
| 98 | + private void initBroadCastReceiver() { | |
| 99 | + if (wakeLock != null) { | |
| 100 | + myReceiver = new CollServiceReceiver(wakeLock); | |
| 101 | + } else { | |
| 102 | + myReceiver = new CollServiceReceiver(); | |
| 103 | + } | |
| 104 | + IntentFilter intentFilter = new IntentFilter(); | |
| 105 | + intentFilter.addAction(Intent.ACTION_SCREEN_OFF); | |
| 106 | + intentFilter.addAction(Intent.ACTION_SCREEN_ON); | |
| 107 | +// intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); | |
| 108 | +// intentFilter.addAction(Intent.ACTION_TIME_CHANGED); | |
| 109 | +// intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); | |
| 110 | +// intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); | |
| 111 | + intentFilter.addAction("com.cnlive.moudle.collectionTrace.receivers.CollServiceReceiver"); | |
| 112 | + registerReceiver(myReceiver, intentFilter); | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * 初始化相关参数 | |
| 117 | + */ | |
| 118 | + @SuppressLint("InvalidWakeLockTag") | |
| 119 | + private void init() { | |
| 120 | + if (null == powerManager) { | |
| 121 | + powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
| 122 | + } | |
| 123 | + if (null == wakeLock) { | |
| 124 | + wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "track upload"); | |
| 125 | + } | |
| 126 | + if (null == mNM) { | |
| 127 | + mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| 128 | + } | |
| 129 | + | |
| 130 | + } | |
| 131 | + | |
| 132 | + @SuppressLint("WrongConstant") | |
| 133 | + @Override | |
| 134 | + public int onStartCommand(Intent intent, int flags, int startId) { | |
| 135 | + //判断是否运行计步服务 | |
| 136 | + boolean isRunStepService = CollCommonInfo.getIsRunStepService(getApplicationContext()); | |
| 137 | + if (isRunStepService) { | |
| 138 | + if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.collectionTrace.service.CollStepService")) { | |
| 139 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 140 | + startForegroundService(new Intent(getApplicationContext(), CollStepService.class)); | |
| 141 | + } else { | |
| 142 | + startService(new Intent(getApplicationContext(), CollStepService.class)); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + } | |
| 146 | + this.startId = startId; | |
| 147 | + //跨进程读取配置文件 | |
| 148 | + boolean isRun = CollCommonInfo.getIsRunKeepAliveService(getApplicationContext()); | |
| 149 | + Log.e("isRun onStartCommand", "" + isRun); | |
| 150 | + | |
| 151 | + if (isRun) { | |
| 152 | + if (runnable != null) { | |
| 153 | + handler.postDelayed(runnable, 5000); | |
| 154 | + } | |
| 155 | + showHideForegroundNotification(); | |
| 156 | +// startService(new Intent(getApplicationContext(), HideKeepTipService.class)); | |
| 157 | + Log.e(TAG, "启动HideKeepTipService"); | |
| 158 | + startPlaySong(); | |
| 159 | + } | |
| 160 | + return START_STICKY; | |
| 161 | + } | |
| 162 | + | |
| 163 | + //开始、暂停播放 | |
| 164 | + public void startPlaySong() { | |
| 165 | + if (mMediaPlayer == null) { | |
| 166 | + mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.no_kill); | |
| 167 | + } | |
| 168 | + mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); | |
| 169 | + mMediaPlayer.setOnCompletionListener(this); | |
| 170 | +// mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { | |
| 171 | +// @Override | |
| 172 | +// public boolean onError(MediaPlayer mediaPlayer, int i, int i1) { | |
| 173 | +// mMediaPlayer.reset(); | |
| 174 | +// mMediaPlayer.release(); | |
| 175 | +// mMediaPlayer = null; | |
| 176 | +// startPlaySong(); | |
| 177 | +// return false; | |
| 178 | +// } | |
| 179 | +// }); | |
| 180 | + if (!mMediaPlayer.isPlaying()) { | |
| 181 | + mMediaPlayer.start(); | |
| 182 | + Log.e(TAG, "开始播放"); | |
| 183 | + } | |
| 184 | + | |
| 185 | + } | |
| 186 | + | |
| 187 | + //停止播放销毁对象 | |
| 188 | + public void stopPlaySong() { | |
| 189 | + | |
| 190 | + if (mMediaPlayer != null) { | |
| 191 | + mMediaPlayer.stop(); | |
| 192 | + mMediaPlayer.release(); | |
| 193 | + mMediaPlayer = null; | |
| 194 | + Log.e("停止播放", "停止"); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + | |
| 199 | + private void showHideForegroundNotification() { | |
| 200 | + NotificationChannel channel = null; | |
| 201 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 202 | + channel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, | |
| 203 | + NotificationManager.IMPORTANCE_HIGH); | |
| 204 | + channel.enableLights(false); | |
| 205 | + channel.enableVibration(false); | |
| 206 | + channel.setVibrationPattern(new long[]{0}); | |
| 207 | + channel.setSound(null, null); | |
| 208 | + channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE); | |
| 209 | + mNM.createNotificationChannel(channel); | |
| 210 | + } | |
| 211 | + notification = new NotificationCompat.Builder(this, CHANNEL_ONE_ID) | |
| 212 | + .setWhen(System.currentTimeMillis()) | |
| 213 | + .setSound(null) | |
| 214 | + .build(); | |
| 215 | + // 0 隐藏前台服务通知 | |
| 216 | + startForeground(100, notification); | |
| 217 | +// stopForeground(true); | |
| 218 | + } | |
| 219 | + | |
| 220 | + @Override | |
| 221 | + public void onDestroy() { | |
| 222 | + super.onDestroy(); | |
| 223 | + Log.e("onDestroy", "onDestroy:" + notification); | |
| 224 | + if (myReceiver != null) { | |
| 225 | + unregisterReceiver(myReceiver); | |
| 226 | + } | |
| 227 | + try { | |
| 228 | +// stopForeground(true); | |
| 229 | + } catch (Exception e) { | |
| 230 | + } | |
| 231 | + stopPlaySong(); | |
| 232 | + // 重启自己 | |
| 233 | + if (isKeepService) { | |
| 234 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 235 | + startForegroundService(new Intent(getApplicationContext(), CollKeepLiveService.class)); | |
| 236 | + } else { | |
| 237 | + startService(new Intent(getApplicationContext(), CollKeepLiveService.class)); | |
| 238 | + } | |
| 239 | + //发送重启广播 | |
| 240 | + sendRestartBroadCast(); | |
| 241 | + } | |
| 242 | + | |
| 243 | + | |
| 244 | + } | |
| 245 | + | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * 发送重启广播 | |
| 249 | + */ | |
| 250 | + private void sendRestartBroadCast() { | |
| 251 | + Intent intent = new Intent(" com.cnlive.moudle.pedometer.receivers.MyReceiver"); | |
| 252 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 253 | + intent.setComponent(new ComponentName(getPackageName(), " com.cnlive.moudle.pedometer.receivers.MyReceiver"));//高版本Android发送广播需要加这句 | |
| 254 | + } | |
| 255 | + sendBroadcast(intent); | |
| 256 | + } | |
| 257 | + | |
| 258 | + @Override | |
| 259 | + public IBinder onBind(Intent intent) { | |
| 260 | + // TODO: Return the communication channel to the service. | |
| 261 | + throw new UnsupportedOperationException("Not yet implemented"); | |
| 262 | + } | |
| 263 | + | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * 歌曲播放完成 | |
| 267 | + * | |
| 268 | + * @param mediaPlayer | |
| 269 | + */ | |
| 270 | + @Override | |
| 271 | + public void onCompletion(MediaPlayer mediaPlayer) { | |
| 272 | + boolean isRun = CollCommonInfo.getIsRunKeepAliveService(getApplicationContext()); | |
| 273 | + | |
| 274 | + if (isRun) { | |
| 275 | + Log.e(TAG, "重新播放"); | |
| 276 | + mediaPlayer.start(); | |
| 277 | + Intent intent = new Intent("com.cnlive.strike.receiver.CollServiceReceiver"); | |
| 278 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 279 | + intent.setComponent(new ComponentName(getPackageName(), "com.cnlive.strike.receiver.CollServiceReceiver"));//高版本Android发送广播需要加这句 | |
| 280 | + } | |
| 281 | + sendBroadcast(intent); | |
| 282 | + } else { | |
| 283 | + Log.e(TAG, "停止重新播放"); | |
| 284 | + stopPlaySong(); | |
| 285 | + } | |
| 286 | + | |
| 287 | + | |
| 288 | + } | |
| 289 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/service/CollStepService.java
0 → 100644
| 1 | +package com.cnlive.moudle.collectionTrace.service; | |
| 2 | + | |
| 3 | +import android.annotation.SuppressLint; | |
| 4 | +import android.app.Notification; | |
| 5 | +import android.app.NotificationChannel; | |
| 6 | +import android.app.NotificationManager; | |
| 7 | +import android.app.PendingIntent; | |
| 8 | +import android.app.Service; | |
| 9 | +import android.content.BroadcastReceiver; | |
| 10 | +import android.content.ComponentName; | |
| 11 | +import android.content.Context; | |
| 12 | +import android.content.Intent; | |
| 13 | +import android.content.IntentFilter; | |
| 14 | +import android.hardware.Sensor; | |
| 15 | +import android.hardware.SensorEvent; | |
| 16 | +import android.hardware.SensorEventListener; | |
| 17 | +import android.hardware.SensorManager; | |
| 18 | +import android.os.Binder; | |
| 19 | +import android.os.Build; | |
| 20 | +import android.os.CountDownTimer; | |
| 21 | +import android.os.Handler; | |
| 22 | +import android.os.IBinder; | |
| 23 | +import android.support.v4.app.NotificationCompat; | |
| 24 | +import android.text.TextUtils; | |
| 25 | +import android.util.Log; | |
| 26 | +import android.widget.RemoteViews; | |
| 27 | +import android.widget.Toast; | |
| 28 | + | |
| 29 | +import com.baidu.trace.LBSTraceClient; | |
| 30 | +import com.baidu.trace.Trace; | |
| 31 | +import com.baidu.trace.api.fence.FenceAlarmPushInfo; | |
| 32 | +import com.baidu.trace.api.fence.MonitoredAction; | |
| 33 | +import com.baidu.trace.api.track.LatestPoint; | |
| 34 | +import com.baidu.trace.api.track.LatestPointResponse; | |
| 35 | +import com.baidu.trace.api.track.OnTrackListener; | |
| 36 | +import com.baidu.trace.model.LocationMode; | |
| 37 | +import com.baidu.trace.model.OnTraceListener; | |
| 38 | +import com.baidu.trace.model.PushMessage; | |
| 39 | +import com.baidu.trace.model.StatusCodes; | |
| 40 | +import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | |
| 41 | +import com.cnlive.moudle.pedometer.model.Constant; | |
| 42 | +import com.cnlive.moudle.pedometer.model.MessageEvent; | |
| 43 | +import com.cnlive.moudle.pedometer.model.step.StepCount; | |
| 44 | +import com.cnlive.moudle.pedometer.model.step.StepValuePassListener; | |
| 45 | +import com.cnlive.moudle.pedometer.sql.dao.CollUserStepEntityDao; | |
| 46 | +import com.cnlive.moudle.pedometer.sql.db.DbCore; | |
| 47 | +import com.cnlive.moudle.pedometer.sql.entity.CollUserStepEntity; | |
| 48 | +import com.cnlive.moudle.pedometer.ui.activity.PedometerMainActivity; | |
| 49 | +import com.cnlive.moudle.pedometer.utils.ServiceUtils; | |
| 50 | +import com.cnlive.moudle.pedometer.utils.TimeUtil; | |
| 51 | +import com.example.moudle_pedometer.R; | |
| 52 | + | |
| 53 | +import org.greenrobot.eventbus.EventBus; | |
| 54 | +import org.greenrobot.eventbus.Subscribe; | |
| 55 | +import org.greenrobot.eventbus.ThreadMode; | |
| 56 | + | |
| 57 | +import java.text.SimpleDateFormat; | |
| 58 | +import java.util.Date; | |
| 59 | +import java.util.List; | |
| 60 | +import java.util.UUID; | |
| 61 | + | |
| 62 | + | |
| 63 | +public class CollStepService extends Service implements SensorEventListener { | |
| 64 | + | |
| 65 | + private String TAG = "CollStepService"; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * 通知栏相关 | |
| 69 | + */ | |
| 70 | + private String CHANNEL_ONE_ID = "com.cnlive.step"; | |
| 71 | + private String CHANNEL_ONE_NAME = "Step"; | |
| 72 | + private NotificationManager mNM; | |
| 73 | + private Notification notification = null; | |
| 74 | + private RemoteViews remoteViews; | |
| 75 | + /** | |
| 76 | + * 默认为30秒进行一次存储 | |
| 77 | + */ | |
| 78 | + private static int duration = 30 * 1000; | |
| 79 | + /** | |
| 80 | + * 当前的日期 | |
| 81 | + */ | |
| 82 | + private static String CURRENT_DATE = ""; | |
| 83 | + /** | |
| 84 | + * 传感器管理对象 | |
| 85 | + */ | |
| 86 | + private SensorManager sensorManager; | |
| 87 | + /** | |
| 88 | + * 广播接受者 | |
| 89 | + */ | |
| 90 | + private BroadcastReceiver mBatInfoReceiver; | |
| 91 | + /** | |
| 92 | + * 保存记步计时器 | |
| 93 | + */ | |
| 94 | + private TimeCount time; | |
| 95 | + /** | |
| 96 | + * 当前所走的步数 | |
| 97 | + */ | |
| 98 | + private int CURRENT_STEP; | |
| 99 | + /** | |
| 100 | + * 计步传感器类型 Sensor.TYPE_STEP_COUNTER或者Sensor.TYPE_STEP_DETECTOR | |
| 101 | + */ | |
| 102 | + private static int stepSensorType = -1; | |
| 103 | + /** | |
| 104 | + * 每次第一次启动记步服务时是否从系统中获取了已有的步数记录 | |
| 105 | + */ | |
| 106 | + private boolean hasRecord = false; | |
| 107 | + /** | |
| 108 | + * 系统中获取到的已有的步数 | |
| 109 | + */ | |
| 110 | + private int hasStepCount = 0; | |
| 111 | + /** | |
| 112 | + * 上一次的步数 | |
| 113 | + */ | |
| 114 | + private int previousStepCount = 0; | |
| 115 | + /** | |
| 116 | + * 加速度传感器中获取的步数 | |
| 117 | + */ | |
| 118 | + private StepCount mStepCount; | |
| 119 | + /** | |
| 120 | + * IBinder对象,向Activity传递数据的桥梁 | |
| 121 | + */ | |
| 122 | + private StepBinder stepBinder = new StepBinder(); | |
| 123 | + private String userId; | |
| 124 | + private Handler handler; | |
| 125 | + private Runnable speedRunnable; | |
| 126 | + private final int NOTIFICATION_ID = 100; | |
| 127 | + private static int tempStep;//计步器临时保存步数 | |
| 128 | + private static boolean pauseFlag;//是否暂停 | |
| 129 | + private static int speedCurrentStep;//用于检测速度步数 | |
| 130 | + private boolean updateSpeedStepFlag;//是否是已经更新速度 | |
| 131 | + | |
| 132 | + /****************************************************鹰眼轨迹相关************************************************/ | |
| 133 | + /** | |
| 134 | + * 轨迹服务监听器 | |
| 135 | + */ | |
| 136 | + public static OnTraceListener traceListener = null; | |
| 137 | + /** | |
| 138 | + * 轨迹监听器(用于接收纠偏后实时位置回调) | |
| 139 | + */ | |
| 140 | + private OnTrackListener realTimeTraceListener = null; | |
| 141 | + //位置采集周期 | |
| 142 | + private int gatherInterval = 3; | |
| 143 | + //打包周期 | |
| 144 | + private int packInterval = 10; | |
| 145 | + // 轨迹服务ID | |
| 146 | + public static final long serviceId = 213511; | |
| 147 | +// public static final long serviceId = 213553;//杨帅 | |
| 148 | + | |
| 149 | + // 设备标识 | |
| 150 | + public static String entityName = ""; | |
| 151 | + /** | |
| 152 | + * 轨迹客户端 | |
| 153 | + */ | |
| 154 | + public static LBSTraceClient mClient = null; | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * 轨迹服务 | |
| 158 | + */ | |
| 159 | + public static Trace mTrace = null; | |
| 160 | + | |
| 161 | + public CollStepService() { | |
| 162 | + | |
| 163 | + } | |
| 164 | + | |
| 165 | + | |
| 166 | + @Override | |
| 167 | + public void onCreate() { | |
| 168 | + super.onCreate(); | |
| 169 | + init(); | |
| 170 | + initBroadcastReceiver(); | |
| 171 | + if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.collectionTrace.service.CollKeepLiveService")) { | |
| 172 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 173 | + startForegroundService(new Intent(getApplicationContext(), CollKeepLiveService.class)); | |
| 174 | + } else { | |
| 175 | + startService(new Intent(getApplicationContext(), CollKeepLiveService.class)); | |
| 176 | + } | |
| 177 | + } | |
| 178 | + showStartForegroundNotification(); | |
| 179 | + } | |
| 180 | + | |
| 181 | + private void initTrace() { | |
| 182 | + if (mClient == null) { | |
| 183 | + mClient = new LBSTraceClient(getApplicationContext()); | |
| 184 | + // 设置定位和打包周期 | |
| 185 | + mClient.setInterval(gatherInterval, packInterval); | |
| 186 | + //设置定位模式 | |
| 187 | + mClient.setLocationMode(LocationMode.High_Accuracy); | |
| 188 | + } | |
| 189 | + if (mTrace == null) { | |
| 190 | + mTrace = new Trace(serviceId, entityName); | |
| 191 | + } | |
| 192 | + //初始化实时轨迹监听器 | |
| 193 | + realTimeTraceListener = new OnTrackListener() { | |
| 194 | + | |
| 195 | + @Override | |
| 196 | + public void onLatestPointCallback(LatestPointResponse response) { | |
| 197 | + if (StatusCodes.SUCCESS != response.getStatus()) { | |
| 198 | + return; | |
| 199 | + } | |
| 200 | + | |
| 201 | + LatestPoint point = response.getLatestPoint(); | |
| 202 | + | |
| 203 | + } | |
| 204 | + }; | |
| 205 | + | |
| 206 | + //轨迹监听 | |
| 207 | + traceListener = new OnTraceListener() { | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * 绑定服务回调接口 | |
| 211 | + * @param errorNo 状态码 | |
| 212 | + * @param message 消息 | |
| 213 | + * <p> | |
| 214 | + * <pre>0:成功 </pre> | |
| 215 | + * <pre>1:失败</pre> | |
| 216 | + */ | |
| 217 | + @Override | |
| 218 | + public void onBindServiceCallback(int errorNo, String message) { | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * 开启服务回调接口 | |
| 223 | + * @param errorNo 状态码 | |
| 224 | + * @param message 消息 | |
| 225 | + * <p> | |
| 226 | + * <pre>0:成功 </pre> | |
| 227 | + * <pre>10000:请求发送失败</pre> | |
| 228 | + * <pre>10001:服务开启失败</pre> | |
| 229 | + * <pre>10002:参数错误</pre> | |
| 230 | + * <pre>10003:网络连接失败</pre> | |
| 231 | + * <pre>10004:网络未开启</pre> | |
| 232 | + * <pre>10005:服务正在开启</pre> | |
| 233 | + * <pre>10006:服务已开启</pre> | |
| 234 | + */ | |
| 235 | + @Override | |
| 236 | + public void onStartTraceCallback(int errorNo, String message) { | |
| 237 | + if (StatusCodes.SUCCESS == errorNo || StatusCodes.START_TRACE_NETWORK_CONNECT_FAILED <= errorNo) { | |
| 238 | + //开始进行采集 | |
| 239 | + if (mClient != null) { | |
| 240 | + mClient.startGather(traceListener); | |
| 241 | + Toast.makeText(getApplicationContext(), "开启轨迹服务", Toast.LENGTH_LONG).show(); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * 停止服务回调接口 | |
| 248 | + * @param errorNo 状态码 | |
| 249 | + * @param message 消息 | |
| 250 | + * <p> | |
| 251 | + * <pre>0:成功</pre> | |
| 252 | + * <pre>11000:请求发送失败</pre> | |
| 253 | + * <pre>11001:服务停止失败</pre> | |
| 254 | + * <pre>11002:服务未开启</pre> | |
| 255 | + * <pre>11003:服务正在停止</pre> | |
| 256 | + */ | |
| 257 | + @Override | |
| 258 | + public void onStopTraceCallback(int errorNo, String message) { | |
| 259 | + if (StatusCodes.SUCCESS == errorNo || StatusCodes.CACHE_TRACK_NOT_UPLOAD == errorNo) { | |
| 260 | + if (mClient != null) { | |
| 261 | + mClient.stopTrace(mTrace, traceListener); | |
| 262 | + Toast.makeText(getApplicationContext(), "停止轨迹服务", Toast.LENGTH_LONG).show(); | |
| 263 | + | |
| 264 | + } | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + /** | |
| 269 | + * 开启采集回调接口 | |
| 270 | + * @param errorNo 状态码 | |
| 271 | + * @param message 消息 | |
| 272 | + * <p> | |
| 273 | + * <pre>0:成功</pre> | |
| 274 | + * <pre>12000:请求发送失败</pre> | |
| 275 | + * <pre>12001:采集开启失败</pre> | |
| 276 | + * <pre>12002:服务未开启</pre> | |
| 277 | + */ | |
| 278 | + @Override | |
| 279 | + public void onStartGatherCallback(int errorNo, String message) { | |
| 280 | + if (StatusCodes.SUCCESS == errorNo || StatusCodes.GATHER_STARTED == errorNo) { | |
| 281 | + Toast.makeText(getApplicationContext(), "开始采集", Toast.LENGTH_LONG).show(); | |
| 282 | + } | |
| 283 | + } | |
| 284 | + | |
| 285 | + /** | |
| 286 | + * 停止采集回调接口 | |
| 287 | + * @param errorNo 状态码 | |
| 288 | + * @param message 消息 | |
| 289 | + * <p> | |
| 290 | + * <pre>0:成功</pre> | |
| 291 | + * <pre>13000:请求发送失败</pre> | |
| 292 | + * <pre>13001:采集停止失败</pre> | |
| 293 | + * <pre>13002:服务未开启</pre> | |
| 294 | + */ | |
| 295 | + @Override | |
| 296 | + public void onStopGatherCallback(int errorNo, String message) { | |
| 297 | + if (StatusCodes.SUCCESS == errorNo || StatusCodes.GATHER_STOPPED == errorNo) { | |
| 298 | + Toast.makeText(getApplicationContext(), "停止采集", Toast.LENGTH_LONG).show(); | |
| 299 | + if (mClient != null) { | |
| 300 | + mClient.stopTrace(mTrace, traceListener); | |
| 301 | + } | |
| 302 | + } | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * 推送消息回调接口 | |
| 307 | + * | |
| 308 | + * @param messageType 状态码 | |
| 309 | + * @param pushMessage 消息 | |
| 310 | + * <p> | |
| 311 | + * <pre>0x01:配置下发</pre> | |
| 312 | + * <pre>0x02:语音消息</pre> | |
| 313 | + * <pre>0x03:服务端围栏报警消息</pre> | |
| 314 | + * <pre>0x04:本地围栏报警消息</pre> | |
| 315 | + * <pre>0x05~0x40:系统预留</pre> | |
| 316 | + * <pre>0x41~0xFF:开发者自定义</pre> | |
| 317 | + */ | |
| 318 | + @Override | |
| 319 | + public void onPushCallback(byte messageType, PushMessage pushMessage) { | |
| 320 | + Log.e(TAG, "onPushCallback: "); | |
| 321 | + | |
| 322 | + if (messageType < 0x03 || messageType > 0x04) { | |
| 323 | + return; | |
| 324 | + } | |
| 325 | + FenceAlarmPushInfo alarmPushInfo = pushMessage.getFenceAlarmPushInfo(); | |
| 326 | + if (null == alarmPushInfo) { | |
| 327 | + return; | |
| 328 | + } | |
| 329 | + //超出范围 | |
| 330 | + if (alarmPushInfo.getMonitoredAction() == MonitoredAction.exit) { | |
| 331 | + EventBus.getDefault().post(new MessageEvent(Constant.EXIT_RANGE, "")); | |
| 332 | + } | |
| 333 | + //进入范围 | |
| 334 | + else if (alarmPushInfo.getMonitoredAction() == MonitoredAction.enter) { | |
| 335 | + EventBus.getDefault().post(new MessageEvent(Constant.ENTER_RANGE, "")); | |
| 336 | + } | |
| 337 | + Toast.makeText(getApplicationContext(), "onPushCallback:" + alarmPushInfo.getMonitoredAction(), Toast.LENGTH_SHORT).show(); | |
| 338 | + | |
| 339 | + } | |
| 340 | + | |
| 341 | + @Override | |
| 342 | + public void onInitBOSCallback(int errorNo, String message) { | |
| 343 | + } | |
| 344 | + }; | |
| 345 | + | |
| 346 | + //开启轨迹服务 | |
| 347 | + if (mClient != null) { | |
| 348 | + mClient.startTrace(mTrace, traceListener); | |
| 349 | + long stepStartTime = CommonInfo.getStepStartTime(getApplicationContext()); | |
| 350 | + if (stepStartTime == 0) { | |
| 351 | + CommonInfo.setStepStartTime(getApplicationContext(), TimeUtil.getCurrentTime()); | |
| 352 | + } | |
| 353 | + } | |
| 354 | + | |
| 355 | + } | |
| 356 | + | |
| 357 | + private long i = 0; | |
| 358 | + private long index = 0; | |
| 359 | + private int mJobId; | |
| 360 | + ComponentName mServieComponent; | |
| 361 | + | |
| 362 | + @SuppressLint("WrongConstant") | |
| 363 | + @Override | |
| 364 | + public int onStartCommand(Intent intent, int flags, int startId) { | |
| 365 | + userId = CommonInfo.getUserId(getApplicationContext()); | |
| 366 | + if (userId != null && !TextUtils.isEmpty(userId)) { | |
| 367 | + if (!EventBus.getDefault().isRegistered(this)) { | |
| 368 | + EventBus.getDefault().register(this); | |
| 369 | + } | |
| 370 | + | |
| 371 | + entityName = userId; | |
| 372 | + initTodayData(userId); | |
| 373 | + initTrace(); | |
| 374 | + //显示计步通知 | |
| 375 | + showStartForegroundNotification(); | |
| 376 | + new Thread(new Runnable() { | |
| 377 | + @Override | |
| 378 | + public void run() { | |
| 379 | + startStepDetector(); | |
| 380 | + } | |
| 381 | + }).start(); | |
| 382 | + startTimeCount(); | |
| 383 | + } | |
| 384 | +// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { | |
| 385 | +// //开始配置JobInfo | |
| 386 | +// mServieComponent = new ComponentName(this, CollJobSchedulerService.class);// 获取到我们自己的jobservice,同时启动该service | |
| 387 | +// JobInfo.Builder builder = new JobInfo.Builder(mJobId++, mServieComponent); | |
| 388 | +//// builder.setOverrideDeadline(Long.valueOf(2) * 1000); | |
| 389 | +// //设置任务每隔intervalMillis运行一次 | |
| 390 | +// builder.setPeriodic(Long.valueOf(2) * 1000); | |
| 391 | +// //你的任务只有当用户没有在使用该设备且有一段时间没有使用时才会启动该任务。 | |
| 392 | +//// builder.setRequiresDeviceIdle(true); | |
| 393 | +//// builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); | |
| 394 | +// JobScheduler mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); | |
| 395 | +// mJobScheduler.schedule(builder.build()); | |
| 396 | +// | |
| 397 | +// } | |
| 398 | + | |
| 399 | + flags = START_STICKY; | |
| 400 | + return super.onStartCommand(intent, flags, startId); | |
| 401 | + } | |
| 402 | + | |
| 403 | + private void init() { | |
| 404 | + CommonInfo.setIsRunStepService(getApplicationContext(), true); | |
| 405 | + if (null == mNM) { | |
| 406 | + mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| 407 | + } | |
| 408 | + handler = new Handler(); | |
| 409 | + speedRunnable = new Runnable() { | |
| 410 | + @Override | |
| 411 | + public void run() { | |
| 412 | + //说明用户没有动 | |
| 413 | + if (!updateSpeedStepFlag) { | |
| 414 | + //重置速度 | |
| 415 | + EventBus.getDefault().post(new MessageEvent(Constant.SERVICE_STEP_COUNT_RESET_SPEED, CURRENT_STEP)); | |
| 416 | + } | |
| 417 | + } | |
| 418 | + }; | |
| 419 | + DbCore.init(getApplicationContext(), "Step.db"); | |
| 420 | + pauseFlag = false; | |
| 421 | + updateSpeedStepFlag = true; | |
| 422 | + | |
| 423 | + } | |
| 424 | + | |
| 425 | + | |
| 426 | + private void showStartForegroundNotification() { | |
| 427 | + remoteViews = new RemoteViews(getPackageName(), R.layout.custom_step_notification); | |
| 428 | + NotificationChannel channel = null; | |
| 429 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 430 | + channel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, | |
| 431 | + NotificationManager.IMPORTANCE_HIGH); | |
| 432 | + channel.enableLights(false); | |
| 433 | + channel.setSound(null, null); | |
| 434 | + channel.enableVibration(false); | |
| 435 | + channel.setVibrationPattern(new long[]{0}); | |
| 436 | + channel.setSound(null, null); | |
| 437 | + mNM.createNotificationChannel(channel); | |
| 438 | + } | |
| 439 | + | |
| 440 | + | |
| 441 | + Intent notificationIntent = new Intent(this, PedometerMainActivity.class); | |
| 442 | + notification = new NotificationCompat.Builder(this, CHANNEL_ONE_ID) | |
| 443 | + .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0)) | |
| 444 | + .setSmallIcon(R.mipmap.ic_launcher) | |
| 445 | +// .setCustomContentView(remoteViews) | |
| 446 | + .setContentTitle(getResources().getString(R.string.app_name)) | |
| 447 | + .setContentText("正在记录您的运动") | |
| 448 | + .setCategory(Notification.CATEGORY_SERVICE) | |
| 449 | + .setSound(null) | |
| 450 | + .setWhen(System.currentTimeMillis()) | |
| 451 | + .build(); | |
| 452 | +// remoteViews.setChronometer(R.id.ch, aa, null, true); | |
| 453 | + startForeground(NOTIFICATION_ID, notification); | |
| 454 | + | |
| 455 | + } | |
| 456 | + | |
| 457 | + @Override | |
| 458 | + public IBinder onBind(Intent intent) { | |
| 459 | + | |
| 460 | +// return stepBinder; | |
| 461 | + return null; | |
| 462 | + } | |
| 463 | + | |
| 464 | + | |
| 465 | + @Override | |
| 466 | + public void onDestroy() { | |
| 467 | + super.onDestroy(); | |
| 468 | + //取消前台进程 | |
| 469 | + stopForeground(true); | |
| 470 | + if (mBatInfoReceiver != null) { | |
| 471 | + unregisterReceiver(mBatInfoReceiver); | |
| 472 | + } | |
| 473 | + if (EventBus.getDefault().isRegistered(this)) { | |
| 474 | + EventBus.getDefault().unregister(this); | |
| 475 | + } | |
| 476 | + if (speedRunnable != null) { | |
| 477 | + handler.removeCallbacks(speedRunnable); | |
| 478 | + } | |
| 479 | + Log.e("杀死", "MyTestService"); | |
| 480 | + } | |
| 481 | + | |
| 482 | + //接收事件 | |
| 483 | + @Subscribe(threadMode = ThreadMode.BACKGROUND) | |
| 484 | + public void Event(MessageEvent messageEvent) { | |
| 485 | + //暂停计步 | |
| 486 | + if (messageEvent.getMessageType() == Constant.SERVICE_STEP_COUNT_PAUSE) { | |
| 487 | + pauseFlag = true; | |
| 488 | + } | |
| 489 | + //继续计步 | |
| 490 | + else if (messageEvent.getMessageType() == Constant.SERVICE_STEP_COUNT_CONTINUE) { | |
| 491 | + pauseFlag = false; | |
| 492 | + } | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 496 | + * 发送重启广播 | |
| 497 | + */ | |
| 498 | + private void sendRestartBroadCast() { | |
| 499 | + Intent intent = new Intent(" com.cnlive.moudle.pedometer.receivers.MyReceiver"); | |
| 500 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| 501 | + intent.setComponent(new ComponentName(getPackageName(), " com.cnlive.moudle.pedometer.receivers.MyReceiver"));//高版本Android发送广播需要加这句 | |
| 502 | + } | |
| 503 | + sendBroadcast(intent); | |
| 504 | + } | |
| 505 | + | |
| 506 | + | |
| 507 | + @Override | |
| 508 | + public void onAccuracyChanged(Sensor sensor, int i) { | |
| 509 | + | |
| 510 | + } | |
| 511 | + | |
| 512 | + /** | |
| 513 | + * @获取默认的pendingIntent,为了防止2.3及以下版本报错 | |
| 514 | + * @flags属性: 在顶部常驻:Notification.FLAG_ONGOING_EVENT | |
| 515 | + * 点击去除: Notification.FLAG_AUTO_CANCEL | |
| 516 | + */ | |
| 517 | + public PendingIntent getDefalutIntent(int flags) { | |
| 518 | + PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), flags); | |
| 519 | + return pendingIntent; | |
| 520 | + } | |
| 521 | + | |
| 522 | + /** | |
| 523 | + * 获取当天日期 | |
| 524 | + * | |
| 525 | + * @return | |
| 526 | + */ | |
| 527 | + private String getTodayDate() { | |
| 528 | + Date date = new Date(System.currentTimeMillis()); | |
| 529 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
| 530 | + return sdf.format(date); | |
| 531 | + } | |
| 532 | + | |
| 533 | + /** | |
| 534 | + * 初始化当天的步数 | |
| 535 | + */ | |
| 536 | + private void initTodayData(String userId) { | |
| 537 | + CURRENT_DATE = getTodayDate(); | |
| 538 | + if (userId == null || CURRENT_DATE == null) { | |
| 539 | + return; | |
| 540 | + } | |
| 541 | + //获取当天的数据,用于展示 CURRENT_DATE | |
| 542 | + List<CollUserStepEntity> list = DbCore.getDaoSession().getCollUserStepEntityDao().queryBuilder().where(CollUserStepEntityDao.Properties.UserId.eq(userId), CollUserStepEntityDao.Properties.Date.eq(CURRENT_DATE)).list(); | |
| 543 | + if (list == null) { | |
| 544 | + return; | |
| 545 | + } | |
| 546 | + if (list.size() == 0 || list.isEmpty()) { | |
| 547 | + CURRENT_STEP = 0; | |
| 548 | + } else if (list.size() == 1) { | |
| 549 | + CURRENT_STEP = Integer.parseInt(list.get(0).getStepCount()); | |
| 550 | + } else { | |
| 551 | + Log.e(TAG, "出错了!"); | |
| 552 | + } | |
| 553 | + if (mStepCount != null) { | |
| 554 | + mStepCount.setSteps(CURRENT_STEP); | |
| 555 | + } | |
| 556 | + updateNotification(); | |
| 557 | + } | |
| 558 | + | |
| 559 | + private void updateNotification() { | |
| 560 | + EventBus.getDefault().post(new MessageEvent(Constant.SERVICE_STEP_COUNT_UPDATE, CURRENT_STEP)); | |
| 561 | + updateSpeedStepFlag = false; | |
| 562 | +// remoteViews = new RemoteViews(getPackageName(), R.layout.custom_step_notification); | |
| 563 | +// NotificationChannel channel = null; | |
| 564 | +// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
| 565 | +// channel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, | |
| 566 | +// NotificationManager.IMPORTANCE_HIGH); | |
| 567 | +// channel.enableLights(false); | |
| 568 | +// channel.setSound(null, null); | |
| 569 | +// channel.enableVibration(false); | |
| 570 | +// channel.setVibrationPattern(new long[]{0}); | |
| 571 | +// channel.setSound(null, null); | |
| 572 | +// mNM.createNotificationChannel(channel); | |
| 573 | +// } | |
| 574 | +// Intent notificationIntent = new Intent(this, CollPedometerMainActivity.class); | |
| 575 | +// notification = new NotificationCompat.Builder(this, CHANNEL_ONE_ID) | |
| 576 | +// .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0)) | |
| 577 | +// .setSmallIcon(R.mipmap.ic_launcher) | |
| 578 | +// .setCustomContentView(remoteViews) | |
| 579 | +// .setSound(null) | |
| 580 | +// .setCategory(Notification.CATEGORY_SERVICE) | |
| 581 | +// .setWhen(System.currentTimeMillis()) | |
| 582 | +// .build(); | |
| 583 | +// remoteViews.setTextViewText(R.id.tv_step_count, CURRENT_STEP + ""); | |
| 584 | +// remoteViews.setChronometer(R.id.ch, aa, null, true); | |
| 585 | +// | |
| 586 | +// mNM.notify(1, notification); | |
| 587 | + } | |
| 588 | + | |
| 589 | + | |
| 590 | +// private void updateNotification(long i) { | |
| 591 | +// remoteViews = new RemoteViews(getPackageName(), R.layout.custom_step_notification); | |
| 592 | +// NotificationChannel channel = null; | |
| 593 | +// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
| 594 | +// channel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, | |
| 595 | +// NotificationManager.IMPORTANCE_HIGH); | |
| 596 | +// channel.enableLights(false); | |
| 597 | +// channel.setSound(null, null); | |
| 598 | +//// channel.setLightColor(getColor(R.color.colorPrimary)); | |
| 599 | +// channel.enableVibration(false); | |
| 600 | +// channel.setVibrationPattern(new long[]{0}); | |
| 601 | +// channel.setSound(null, null); | |
| 602 | +// mNM.createNotificationChannel(channel); | |
| 603 | +// } | |
| 604 | +// | |
| 605 | +// Intent notificationIntent = new Intent(this, CollPedometerMainActivity.class); | |
| 606 | +// notification = new NotificationCompat.Builder(this, CHANNEL_ONE_ID) | |
| 607 | +// .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0)) | |
| 608 | +// .setSmallIcon(R.mipmap.ic_launcher) | |
| 609 | +//// .setContentText("前台服务正在运行") | |
| 610 | +//// .setContentTitle("标题") | |
| 611 | +// .setCustomContentView(remoteViews) | |
| 612 | +// .setSound(null) | |
| 613 | +// .setWhen(System.currentTimeMillis()) | |
| 614 | +// .build(); | |
| 615 | +// remoteViews.setTextViewText(R.id.tv_time, getFormatHMS(i)); | |
| 616 | +// remoteViews.setTextViewText(R.id.tv_step_count, CURRENT_STEP + ""); | |
| 617 | +// mNM.notify(1, notification); | |
| 618 | +// } | |
| 619 | + | |
| 620 | + /** | |
| 621 | + * 注册广播 | |
| 622 | + */ | |
| 623 | + private void initBroadcastReceiver() { | |
| 624 | + final IntentFilter filter = new IntentFilter(); | |
| 625 | + // 屏幕灭屏广播 | |
| 626 | + filter.addAction(Intent.ACTION_SCREEN_OFF); | |
| 627 | + //关机广播 | |
| 628 | + filter.addAction(Intent.ACTION_SHUTDOWN); | |
| 629 | + // 屏幕亮屏广播 | |
| 630 | + filter.addAction(Intent.ACTION_SCREEN_ON); | |
| 631 | + // 屏幕解锁广播 | |
| 632 | +// filter.addAction(Intent.ACTION_USER_PRESENT); | |
| 633 | + // 当长按电源键弹出“关机”对话或者锁屏时系统会发出这个广播 | |
| 634 | + // example:有时候会用到系统对话框,权限可能很高,会覆盖在锁屏界面或者“关机”对话框之上, | |
| 635 | + // 所以监听这个广播,当收到时就隐藏自己的对话,如点击pad右下角部分弹出的对话框 | |
| 636 | + filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); | |
| 637 | + //监听日期变化 | |
| 638 | + filter.addAction(Intent.ACTION_DATE_CHANGED); | |
| 639 | + filter.addAction(Intent.ACTION_TIME_CHANGED); | |
| 640 | + filter.addAction(Intent.ACTION_TIME_TICK); | |
| 641 | + filter.addAction("close.CollStepService"); | |
| 642 | + mBatInfoReceiver = new BroadcastReceiver() { | |
| 643 | + @Override | |
| 644 | + public void onReceive(final Context context, final Intent intent) { | |
| 645 | + String action = intent.getAction(); | |
| 646 | + if (Intent.ACTION_SCREEN_ON.equals(action)) { | |
| 647 | + startStepDetector(); | |
| 648 | + Log.d(TAG, "screen on"); | |
| 649 | + } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { | |
| 650 | + startStepDetector(); | |
| 651 | + Log.d(TAG, "screen off"); | |
| 652 | + //改为60秒一存储 | |
| 653 | + duration = 60000; | |
| 654 | + } else if (Intent.ACTION_USER_PRESENT.equals(action)) { | |
| 655 | + Log.d(TAG, "screen unlock"); | |
| 656 | + save(); | |
| 657 | + //改为30秒一存储 | |
| 658 | + duration = 30000; | |
| 659 | + } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { | |
| 660 | + Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS"); | |
| 661 | + //保存一次 | |
| 662 | + save(); | |
| 663 | + } else if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) { | |
| 664 | + Log.i(TAG, " receive ACTION_SHUTDOWN"); | |
| 665 | + save(); | |
| 666 | + } else if (Intent.ACTION_DATE_CHANGED.equals(action)) {//日期变化步数重置为0 | |
| 667 | +// Logger.d("重置步数" + StepDcretor.CURRENT_STEP); | |
| 668 | + save(); | |
| 669 | + isNewDay(); | |
| 670 | + } else if (Intent.ACTION_TIME_CHANGED.equals(action)) { | |
| 671 | + //时间变化步数重置为0 | |
| 672 | +// isCall(); | |
| 673 | + save(); | |
| 674 | + isNewDay(); | |
| 675 | + } else if (Intent.ACTION_TIME_TICK.equals(action)) {//日期变化步数重置为0 | |
| 676 | +// isCall(); | |
| 677 | +// Logger.d("重置步数" + StepDcretor.CURRENT_STEP); | |
| 678 | + save(); | |
| 679 | +// isNewDay(); | |
| 680 | + } else if ("close.CollStepService".equals(action)) { | |
| 681 | + Log.e("关闭StepService", "关闭"); | |
| 682 | + onDestroy(); | |
| 683 | + stopSelf(); | |
| 684 | + } | |
| 685 | + } | |
| 686 | + }; | |
| 687 | + registerReceiver(mBatInfoReceiver, filter); | |
| 688 | + } | |
| 689 | + | |
| 690 | + /** | |
| 691 | + * 获取当前步数 | |
| 692 | + * | |
| 693 | + * @return | |
| 694 | + */ | |
| 695 | + public int getStepCount() { | |
| 696 | + return CURRENT_STEP; | |
| 697 | + } | |
| 698 | + | |
| 699 | + /** | |
| 700 | + * 监听晚上0点变化初始化数据 | |
| 701 | + */ | |
| 702 | + private void isNewDay() { | |
| 703 | + String time = "00:00"; | |
| 704 | + if (time.equals(new SimpleDateFormat("HH:mm").format(new Date())) || !CURRENT_DATE.equals(getTodayDate())) { | |
| 705 | + initTodayData(userId); | |
| 706 | + } | |
| 707 | + } | |
| 708 | + | |
| 709 | +// /** | |
| 710 | +// * 监听时间变化提醒用户锻炼 | |
| 711 | +// */ | |
| 712 | +// private void isCall() { | |
| 713 | +// String time = this.getSharedPreferences("share_date", Context.MODE_MULTI_PROCESS).getString("achieveTime", "21:00"); | |
| 714 | +// String plan = this.getSharedPreferences("share_date", Context.MODE_MULTI_PROCESS).getString("planWalk_QTY", "7000"); | |
| 715 | +// String remind = this.getSharedPreferences("share_date", Context.MODE_MULTI_PROCESS).getString("remind", "1"); | |
| 716 | +// Logger.d("time=" + time + "\n" + | |
| 717 | +// "new SimpleDateFormat(\"HH: mm\").format(new Date()))=" + new SimpleDateFormat("HH:mm").format(new Date())); | |
| 718 | +// if (("1".equals(remind)) && | |
| 719 | +// (CURRENT_STEP < Integer.parseInt(plan)) && | |
| 720 | +// (time.equals(new SimpleDateFormat("HH:mm").format(new Date()))) | |
| 721 | +// ) { | |
| 722 | +// remindNotify(); | |
| 723 | +// } | |
| 724 | +// | |
| 725 | +// } | |
| 726 | + /** | |
| 727 | + * 提醒锻炼通知栏 | |
| 728 | + */ | |
| 729 | +// private void remindNotify() { | |
| 730 | +// | |
| 731 | +// //设置点击跳转 | |
| 732 | +// Intent hangIntent = new Intent(this, MainActivity.class); | |
| 733 | +// PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT); | |
| 734 | +// | |
| 735 | +// String plan = this.getSharedPreferences("share_date", Context.MODE_MULTI_PROCESS).getString("planWalk_QTY", "7000"); | |
| 736 | +// NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); | |
| 737 | +// mBuilder.setContentTitle("今日步数" + CURRENT_STEP + " 步") | |
| 738 | +// .setContentText("距离目标还差" + (Integer.valueOf(plan) - CURRENT_STEP) + "步,加油!") | |
| 739 | +// .setContentIntent(hangPendingIntent) | |
| 740 | +// .setTicker(getResources().getString(R.string.app_name) + "提醒您开始锻炼了")//通知首次出现在通知栏,带上升动画效果的 | |
| 741 | +// .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示 | |
| 742 | +// .setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级 | |
| 743 | +// .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消 | |
| 744 | +// .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) | |
| 745 | +// .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合: | |
| 746 | +// //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission | |
| 747 | +// .setSmallIcon(R.mipmap.logo); | |
| 748 | +// NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
| 749 | +// mNotificationManager.notify(notify_remind_id, mBuilder.build()); | |
| 750 | +// } | |
| 751 | + | |
| 752 | + /** | |
| 753 | + * 获取传感器实例 | |
| 754 | + */ | |
| 755 | + private void startStepDetector() { | |
| 756 | + if (sensorManager != null) { | |
| 757 | + sensorManager = null; | |
| 758 | + } | |
| 759 | + // 获取传感器管理器的实例 | |
| 760 | + sensorManager = (SensorManager) this | |
| 761 | + .getSystemService(SENSOR_SERVICE); | |
| 762 | + //android4.4以后可以使用计步传感器 | |
| 763 | + int VERSION_CODES = Build.VERSION.SDK_INT; | |
| 764 | + if (VERSION_CODES >= 19) { | |
| 765 | + sensorManager.unregisterListener(CollStepService.this); | |
| 766 | + //注册传感器,传感器监听器的注册导致传感器电源又重新开启 | |
| 767 | + addCountStepListener(); | |
| 768 | + } else { | |
| 769 | + addBasePedometerListener(); | |
| 770 | + } | |
| 771 | + } | |
| 772 | + | |
| 773 | + /** | |
| 774 | + * 停止传感器 | |
| 775 | + */ | |
| 776 | + private void stopStepDetector() { | |
| 777 | + if (sensorManager != null) { | |
| 778 | + sensorManager = null; | |
| 779 | + } | |
| 780 | + // 获取传感器管理器的实例 | |
| 781 | + sensorManager = (SensorManager) this | |
| 782 | + .getSystemService(SENSOR_SERVICE); | |
| 783 | + //android4.4以后可以使用计步传感器 | |
| 784 | + sensorManager.unregisterListener(CollStepService.this); | |
| 785 | + } | |
| 786 | + | |
| 787 | + /** | |
| 788 | + * 添加传感器监听 | |
| 789 | + * 1. TYPE_STEP_COUNTER API的解释说返回从开机被激活后统计的步数,当重启手机后该数据归零, | |
| 790 | + * 该传感器是一个硬件传感器所以它是低功耗的。 | |
| 791 | + * 为了能持续的计步,请不要反注册事件,就算手机处于休眠状态它依然会计步。 | |
| 792 | + * 当激活的时候依然会上报步数。该sensor适合在长时间的计步需求。 | |
| 793 | + * <p> | |
| 794 | + * 2.TYPE_STEP_DETECTOR翻译过来就是走路检测, | |
| 795 | + * API文档也确实是这样说的,该sensor只用来监监测走步,每次返回数字1.0。 | |
| 796 | + * 如果需要长事件的计步请使用TYPE_STEP_COUNTER。 | |
| 797 | + */ | |
| 798 | + private void addCountStepListener() { | |
| 799 | + Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); | |
| 800 | + Sensor detectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR); | |
| 801 | + if (countSensor != null) { | |
| 802 | + stepSensorType = Sensor.TYPE_STEP_COUNTER; | |
| 803 | + Log.v(TAG, "Sensor.TYPE_STEP_COUNTER"); | |
| 804 | + sensorManager.registerListener(CollStepService.this, countSensor, SensorManager.SENSOR_DELAY_NORMAL); | |
| 805 | + } else if (detectorSensor != null) { | |
| 806 | + stepSensorType = Sensor.TYPE_STEP_DETECTOR; | |
| 807 | + Log.v(TAG, "Sensor.TYPE_STEP_DETECTOR"); | |
| 808 | + sensorManager.registerListener(CollStepService.this, detectorSensor, SensorManager.SENSOR_DELAY_NORMAL); | |
| 809 | + } else { | |
| 810 | + Log.v(TAG, "Count sensor not available!"); | |
| 811 | + addBasePedometerListener(); | |
| 812 | + } | |
| 813 | + } | |
| 814 | + | |
| 815 | + /** | |
| 816 | + * 通过加速度传感器来记步 | |
| 817 | + */ | |
| 818 | + private void addBasePedometerListener() { | |
| 819 | + mStepCount = new StepCount(); | |
| 820 | + mStepCount.setSteps(CURRENT_STEP); | |
| 821 | + // 获得传感器的类型,这里获得的类型是加速度传感器 | |
| 822 | + // 此方法用来注册,只有注册过才会生效,参数:SensorEventListener的实例,Sensor的实例,更新速率 | |
| 823 | + Sensor sensor = sensorManager | |
| 824 | + .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
| 825 | + boolean isAvailable = sensorManager.registerListener(mStepCount.getStepDetector(), sensor, | |
| 826 | + SensorManager.SENSOR_DELAY_UI); | |
| 827 | + mStepCount.initListener(new StepValuePassListener() { | |
| 828 | + @Override | |
| 829 | + public void stepChanged(int steps) { | |
| 830 | + CURRENT_STEP = steps; | |
| 831 | + updateNotification(); | |
| 832 | + } | |
| 833 | + }); | |
| 834 | + if (isAvailable) { | |
| 835 | + Log.e(TAG, "加速度传感器可以使用"); | |
| 836 | + } else { | |
| 837 | + Log.e(TAG, "加速度传感器无法使用"); | |
| 838 | + } | |
| 839 | + } | |
| 840 | + | |
| 841 | + /** | |
| 842 | + * 传感器监听回调 | |
| 843 | + * 记步的关键代码 | |
| 844 | + * 1. TYPE_STEP_COUNTER API的解释说返回从开机被激活后统计的步数,当重启手机后该数据归零, | |
| 845 | + * 该传感器是一个硬件传感器所以它是低功耗的。 | |
| 846 | + * 为了能持续的计步,请不要反注册事件,就算手机处于休眠状态它依然会计步。 | |
| 847 | + * 当激活的时候依然会上报步数。该sensor适合在长时间的计步需求。 | |
| 848 | + * <p> | |
| 849 | + * 2.TYPE_STEP_DETECTOR翻译过来就是走路检测, | |
| 850 | + * API文档也确实是这样说的,该sensor只用来监监测走步,每次返回数字1.0。 | |
| 851 | + * 如果需要长事件的计步请使用TYPE_STEP_COUNTER。 | |
| 852 | + * | |
| 853 | + * @param event | |
| 854 | + */ | |
| 855 | + @Override | |
| 856 | + public void onSensorChanged(SensorEvent event) { | |
| 857 | + //没有暂停 | |
| 858 | + if (stepSensorType == Sensor.TYPE_STEP_COUNTER) { | |
| 859 | + //获取当前传感器返回的临时步数 | |
| 860 | + tempStep = (int) event.values[0]; | |
| 861 | + //首次如果没有获取手机系统中已有的步数则获取一次系统中APP还未开始记步的步数 | |
| 862 | + if (!hasRecord) { | |
| 863 | + hasRecord = true; | |
| 864 | + hasStepCount = tempStep; | |
| 865 | + } else { | |
| 866 | + //获取APP打开到现在的总步数=本次系统回调的总步数-APP打开之前已有的步数 | |
| 867 | + int thisStepCount = tempStep - hasStepCount; | |
| 868 | + //本次有效步数=(APP打开后所记录的总步数-上一次APP打开后所记录的总步数) | |
| 869 | + int thisStep = thisStepCount - previousStepCount; | |
| 870 | + //当用户点击暂停了 | |
| 871 | + if (pauseFlag) { | |
| 872 | + thisStep = 0; | |
| 873 | + } | |
| 874 | + //总步数=现有的步数+本次有效步数 | |
| 875 | + CURRENT_STEP += (thisStep); | |
| 876 | + //记录最后一次APP打开到现在的总步数 | |
| 877 | + previousStepCount = thisStepCount; | |
| 878 | + } | |
| 879 | + } else if (stepSensorType == Sensor.TYPE_STEP_DETECTOR) { | |
| 880 | + if (event.values[0] == 1.0) { | |
| 881 | + if (!pauseFlag) { | |
| 882 | + CURRENT_STEP++; | |
| 883 | + } | |
| 884 | + | |
| 885 | + } | |
| 886 | + } | |
| 887 | + //如果没有暂停就更新计步器 | |
| 888 | + if (!pauseFlag) { | |
| 889 | + updateSpeedStepFlag = true; | |
| 890 | + updateNotification(); | |
| 891 | + speedCurrentStep = CURRENT_STEP; | |
| 892 | + handler.removeCallbacks(speedRunnable); | |
| 893 | + handler.postDelayed(speedRunnable, 60000); | |
| 894 | + | |
| 895 | + } | |
| 896 | + | |
| 897 | + } | |
| 898 | + | |
| 899 | + /** | |
| 900 | + * 保存记步数据 | |
| 901 | + */ | |
| 902 | + private void save() { | |
| 903 | + boolean isWriteUserStep = CommonInfo.getIsWriteUserStep(getApplicationContext()); | |
| 904 | + if (!isWriteUserStep) { | |
| 905 | + return; | |
| 906 | + } | |
| 907 | + int tempStep = CURRENT_STEP; | |
| 908 | + List<CollUserStepEntity> list = DbCore.getDaoSession().getCollUserStepEntityDao().queryBuilder().where(CollUserStepEntityDao.Properties.UserId.eq(userId), CollUserStepEntityDao.Properties.Date.eq(CURRENT_DATE)).list(); | |
| 909 | + if (list.size() == 0 || list.isEmpty()) { | |
| 910 | + CollUserStepEntity data = new CollUserStepEntity(); | |
| 911 | + data.setUuid(UUID.randomUUID().toString()); | |
| 912 | + data.setUserId(userId); | |
| 913 | + data.setDate(CURRENT_DATE); | |
| 914 | + data.setStepCount(tempStep + ""); | |
| 915 | + DbCore.getDaoSession().getCollUserStepEntityDao().insert(data); | |
| 916 | + } else if (list.size() == 1) { | |
| 917 | + CollUserStepEntity data = list.get(0); | |
| 918 | + data.setStepCount(tempStep + ""); | |
| 919 | + DbCore.getDaoSession().getCollUserStepEntityDao().update(data); | |
| 920 | + } else { | |
| 921 | + } | |
| 922 | + } | |
| 923 | + | |
| 924 | + /** | |
| 925 | + * 保存记步数据 | |
| 926 | + */ | |
| 927 | + class TimeCount extends CountDownTimer { | |
| 928 | + public TimeCount(long millisInFuture, long countDownInterval) { | |
| 929 | + super(millisInFuture, countDownInterval); | |
| 930 | + } | |
| 931 | + | |
| 932 | + @Override | |
| 933 | + public void onFinish() { | |
| 934 | + // 如果计时器正常结束,则开始计步 | |
| 935 | + time.cancel(); | |
| 936 | + save(); | |
| 937 | + startTimeCount(); | |
| 938 | + } | |
| 939 | + | |
| 940 | + @Override | |
| 941 | + public void onTick(long millisUntilFinished) { | |
| 942 | + | |
| 943 | + } | |
| 944 | + | |
| 945 | + } | |
| 946 | + | |
| 947 | + /** | |
| 948 | + * 根据行走步数,计算大概行走具体 | |
| 949 | + * 单位:公里 | |
| 950 | + * 1步的距离大概是60厘米。 | |
| 951 | + * 1万多步,距离大概就是600000厘米,即6000米(6公里) | |
| 952 | + * | |
| 953 | + * @param step | |
| 954 | + * @return | |
| 955 | + */ | |
| 956 | + private double getWalkDistance(long step) { | |
| 957 | + double result = step * 60.0 / 100.0 / 1000.0; | |
| 958 | + return result; | |
| 959 | + } | |
| 960 | + | |
| 961 | + /** | |
| 962 | + * 开始保存记步数据 | |
| 963 | + */ | |
| 964 | + private void startTimeCount() { | |
| 965 | + if (time == null) { | |
| 966 | + time = new TimeCount(duration, 1000); | |
| 967 | + } | |
| 968 | + time.start(); | |
| 969 | + } | |
| 970 | + | |
| 971 | + /** | |
| 972 | + * 向Activity传递数据的纽带 | |
| 973 | + */ | |
| 974 | + public class StepBinder extends Binder { | |
| 975 | + | |
| 976 | + /** | |
| 977 | + * 获取当前service对象 | |
| 978 | + * | |
| 979 | + * @return CollStepService | |
| 980 | + */ | |
| 981 | + public CollStepService getService() { | |
| 982 | + return CollStepService.this; | |
| 983 | + } | |
| 984 | + } | |
| 985 | + | |
| 986 | + /** | |
| 987 | + * 根据毫秒返回时分秒 | |
| 988 | + * | |
| 989 | + * @param time | |
| 990 | + * @return | |
| 991 | + */ | |
| 992 | + public String getFormatHMS(long time) { | |
| 993 | + time = time / 1000;//总秒数 | |
| 994 | + int s = (int) (time % 60);//秒 | |
| 995 | + int m = (int) (time / 60) % 60;//分 | |
| 996 | + int h = (int) (time / 3600) % 60;//小时 | |
| 997 | + return String.format("%02d:%02d:%02d", h, m, s); | |
| 998 | + } | |
| 999 | + | |
| 1000 | + | |
| 1001 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/activity/CollPedometerMainActivity.java
| 1 | 1 | package com.cnlive.moudle.collectionTrace.ui.activity; |
| 2 | 2 | |
| 3 | 3 | import android.Manifest; |
| 4 | +import android.app.Activity; | |
| 4 | 5 | import android.content.Intent; |
| 5 | 6 | import android.content.pm.PackageInfo; |
| 6 | 7 | import android.content.pm.PackageManager; |
| ... | ... | @@ -64,7 +65,7 @@ public class CollPedometerMainActivity extends QMUIFragmentActivity { |
| 64 | 65 | isOpen = notificationManagerCompat.areNotificationsEnabled(); |
| 65 | 66 | if (isOpen) { |
| 66 | 67 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 67 | -// ContextCompat.startForegroundService(CollPedometerMainActivity.this, new Intent(CollPedometerMainActivity.this, StepService.class)); | |
| 68 | +// ContextCompat.startForegroundService(CollPedometerMainActivity.this, new Intent(CollPedometerMainActivity.this, CollStepService.class)); | |
| 68 | 69 | } |
| 69 | 70 | } else { |
| 70 | 71 | goToSettingNo(); |
| ... | ... | @@ -222,4 +223,8 @@ public class CollPedometerMainActivity extends QMUIFragmentActivity { |
| 222 | 223 | return; |
| 223 | 224 | } |
| 224 | 225 | |
| 226 | + public static void startActivity(Activity activity) { | |
| 227 | + Intent intent = new Intent(activity, CollPedometerMainActivity.class); | |
| 228 | + activity.startActivity(intent); | |
| 229 | + } | |
| 225 | 230 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/activity/CollRunRaceDetailActivity.java
| 1 | 1 | package com.cnlive.moudle.collectionTrace.ui.activity; |
| 2 | 2 | |
| 3 | +import android.app.Activity; | |
| 3 | 4 | import android.content.Intent; |
| 4 | 5 | import android.os.Bundle; |
| 5 | 6 | import android.support.annotation.Nullable; |
| ... | ... | @@ -9,10 +10,9 @@ import com.cnlive.libs.base.ui.QMUIFragment; |
| 9 | 10 | import com.cnlive.libs.base.ui.QMUIFragmentActivity; |
| 10 | 11 | import com.cnlive.libs.base.util.StatusBarUtil; |
| 11 | 12 | import com.cnlive.libs.base.util.StatusBarUtil2; |
| 13 | +import com.cnlive.moudle.collectionTrace.ui.fragment.CollRunMainFragment; | |
| 12 | 14 | import com.cnlive.moudle.collectionTrace.ui.fragment.CollRunRaceDetailFragment; |
| 13 | -import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | |
| 14 | -import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; | |
| 15 | -import com.cnlive.moudle.pedometer.ui.fragment.RunRaceDetailFragment; | |
| 15 | +import com.cnlive.moudle.pedometer.commonInfo.CollCommonInfo; | |
| 16 | 16 | import com.example.moudle_pedometer.R; |
| 17 | 17 | |
| 18 | 18 | /** |
| ... | ... | @@ -34,16 +34,16 @@ public class CollRunRaceDetailActivity extends QMUIFragmentActivity { |
| 34 | 34 | super.onCreate(savedInstanceState); |
| 35 | 35 | StatusBarUtil.setStatusBarWrite(this); |
| 36 | 36 | StatusBarUtil2.setColor(this, getResources().getColor(R.color.white), 0); |
| 37 | - //判断用户是否结束运动 | |
| 38 | - boolean isRunStep = CommonInfo.getIsRunStepService(this); | |
| 39 | - //没有找到相关记录 | |
| 40 | - if (!isRunStep) { | |
| 37 | +// //判断用户是否结束运动 | |
| 38 | +// boolean isRunStep = CollCommonInfo.getIsRunStepService(this); | |
| 39 | +// //没有找到相关记录 | |
| 40 | +// if (!isRunStep) { | |
| 41 | 41 | fragment = new CollRunRaceDetailFragment(); |
| 42 | - } | |
| 43 | - //找到记录 | |
| 44 | - else { | |
| 45 | - fragment = new RunMainFragment(); | |
| 46 | - } | |
| 42 | +// } | |
| 43 | +// //找到记录 | |
| 44 | +// else { | |
| 45 | +// fragment = new CollRunMainFragment(); | |
| 46 | +// } | |
| 47 | 47 | if (fragment != null) { |
| 48 | 48 | addFragment(fragment); |
| 49 | 49 | } |
| ... | ... | @@ -68,9 +68,14 @@ public class CollRunRaceDetailActivity extends QMUIFragmentActivity { |
| 68 | 68 | public void onBackPressed() { |
| 69 | 69 | super.onBackPressed(); |
| 70 | 70 | if (fragment != null) { |
| 71 | - if (fragment instanceof RunMainFragment) { | |
| 71 | + if (fragment instanceof CollRunMainFragment) { | |
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | + | |
| 77 | + public static void startActivity(Activity activity) { | |
| 78 | + Intent intent = new Intent(activity, CollRunRaceDetailActivity.class); | |
| 79 | + activity.startActivity(intent); | |
| 80 | + } | |
| 76 | 81 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/fragment/CollRunMainFragment.java
| ... | ... | @@ -7,8 +7,9 @@ import android.text.TextUtils; |
| 7 | 7 | import android.view.View; |
| 8 | 8 | |
| 9 | 9 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 10 | -import com.cnlive.moudle.pedometer.frame.presenter.RunMainFragmentPresenter; | |
| 11 | -import com.cnlive.moudle.pedometer.frame.view.RunMainFragmentView; | |
| 10 | +import com.cnlive.libs.base.util.StatusBarUtil2; | |
| 11 | +import com.cnlive.moudle.collectionTrace.frame.presenter.CollRunMainFragmentPresenter; | |
| 12 | +import com.cnlive.moudle.collectionTrace.frame.view.CollRunMainFragmentView; | |
| 12 | 13 | import com.cnlive.moudle.pedometer.model.Constant; |
| 13 | 14 | import com.cnlive.moudle.pedometer.model.MessageEvent; |
| 14 | 15 | import com.example.moudle_pedometer.R; |
| ... | ... | @@ -18,13 +19,14 @@ import org.greenrobot.eventbus.EventBus; |
| 18 | 19 | import org.greenrobot.eventbus.Subscribe; |
| 19 | 20 | import org.greenrobot.eventbus.ThreadMode; |
| 20 | 21 | |
| 21 | -public class CollRunMainFragment extends QMUIFragment<RunMainFragmentView, RunMainFragmentPresenter, FragmentRunMainBinding> { | |
| 22 | +public class CollRunMainFragment extends QMUIFragment<CollRunMainFragmentView, CollRunMainFragmentPresenter, FragmentRunMainBinding> { | |
| 22 | 23 | private final String TAG = "CollRunMainFragment"; |
| 23 | 24 | private String userId = "yangshuai"; |
| 24 | 25 | |
| 25 | 26 | @Override |
| 26 | 27 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 27 | 28 | super.onViewCreated(view, savedInstanceState); |
| 29 | + StatusBarUtil2.setColor(getActivity(), getResources().getColor(R.color.run_main_bg), 0); | |
| 28 | 30 | EventBus.getDefault().register(this); |
| 29 | 31 | if (getMvpView() != null) { |
| 30 | 32 | new Handler().postDelayed(new Runnable() { |
| ... | ... | @@ -99,6 +101,7 @@ public class CollRunMainFragment extends QMUIFragment<RunMainFragmentView, RunMa |
| 99 | 101 | } |
| 100 | 102 | } |
| 101 | 103 | } |
| 104 | + | |
| 102 | 105 | /** |
| 103 | 106 | * 屏蔽边缘返回 |
| 104 | 107 | * 杨帅 | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/fragment/CollRunRaceDetailFragment.java
| ... | ... | @@ -5,20 +5,15 @@ import android.hardware.Sensor; |
| 5 | 5 | import android.hardware.SensorManager; |
| 6 | 6 | import android.os.Bundle; |
| 7 | 7 | import android.support.annotation.Nullable; |
| 8 | -import android.util.Log; | |
| 9 | 8 | import android.view.View; |
| 10 | 9 | import android.widget.Button; |
| 11 | 10 | |
| 12 | 11 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 13 | 12 | import com.cnlive.moudle.collectionTrace.frame.presenter.CollRunRaceDetailFragmentPresenter; |
| 14 | 13 | import com.cnlive.moudle.collectionTrace.frame.view.CollRunRaceDetailFragmentView; |
| 15 | -import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | |
| 16 | -import com.cnlive.moudle.pedometer.frame.presenter.RunRaceDetailFragmentPresenter; | |
| 17 | -import com.cnlive.moudle.pedometer.frame.view.RunRaceDetailFragmentView; | |
| 18 | 14 | import com.cnlive.moudle.pedometer.ui.activity.RunLineActivity; |
| 19 | 15 | import com.example.moudle_pedometer.R; |
| 20 | 16 | import com.example.moudle_pedometer.databinding.FragmentCollectRunRaceDetailBinding; |
| 21 | -import com.example.moudle_pedometer.databinding.FragmentRunRaceDetailBinding; | |
| 22 | 17 | import com.qmuiteam.qmui.widget.QMUITopBar; |
| 23 | 18 | |
| 24 | 19 | import static android.content.Context.SENSOR_SERVICE; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/fragment/CollRunningFinishFragment.java
| ... | ... | @@ -5,13 +5,14 @@ import android.support.annotation.Nullable; |
| 5 | 5 | import android.view.View; |
| 6 | 6 | |
| 7 | 7 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 8 | +import com.cnlive.moudle.collectionTrace.frame.presenter.CollRunningFinishFragmetPresenter; | |
| 9 | +import com.cnlive.moudle.collectionTrace.frame.view.CollRunningFinishFragmentView; | |
| 8 | 10 | import com.cnlive.moudle.pedometer.frame.presenter.RunningFinishFragmetPresenter; |
| 9 | -import com.cnlive.moudle.pedometer.frame.view.RunningFinishFragmentView; | |
| 10 | 11 | import com.cnlive.moudle.pedometer.model.RunningFinishBean; |
| 11 | 12 | import com.example.moudle_pedometer.R; |
| 12 | 13 | import com.example.moudle_pedometer.databinding.FragmentFinishRunMapBinding; |
| 13 | 14 | |
| 14 | -public class CollRunningFinishFragment extends QMUIFragment<RunningFinishFragmentView, RunningFinishFragmetPresenter, FragmentFinishRunMapBinding> { | |
| 15 | +public class CollRunningFinishFragment extends QMUIFragment<CollRunningFinishFragmentView, CollRunningFinishFragmetPresenter, FragmentFinishRunMapBinding> { | |
| 15 | 16 | private RunningFinishBean runningFinishBean; |
| 16 | 17 | |
| 17 | 18 | public static CollRunningFinishFragment instance(RunningFinishBean runningFinishBean) { | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/fragment/CollRunningMapFragment.java
| ... | ... | @@ -13,9 +13,9 @@ import android.view.View; |
| 13 | 13 | import com.baidu.mapapi.model.LatLng; |
| 14 | 14 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 15 | 15 | import com.cnlive.libs.base.util.StatusBarUtil2; |
| 16 | +import com.cnlive.moudle.collectionTrace.frame.presenter.CollRunningMapFragmentPresenter; | |
| 17 | +import com.cnlive.moudle.collectionTrace.frame.view.CollRunningMapFragmentView; | |
| 16 | 18 | import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; |
| 17 | -import com.cnlive.moudle.pedometer.frame.presenter.RunningMapFragmentPresenter; | |
| 18 | -import com.cnlive.moudle.pedometer.frame.view.RunningMapFragmentView; | |
| 19 | 19 | import com.cnlive.moudle.pedometer.model.Constant; |
| 20 | 20 | import com.cnlive.moudle.pedometer.model.MessageEvent; |
| 21 | 21 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| ... | ... | @@ -41,7 +41,7 @@ import static android.content.Context.SENSOR_SERVICE; |
| 41 | 41 | * |
| 42 | 42 | * @author ShinnyYang |
| 43 | 43 | */ |
| 44 | -public class CollRunningMapFragment extends QMUIFragment<RunningMapFragmentView, RunningMapFragmentPresenter, FragmentRunningMapBinding> { | |
| 44 | +public class CollRunningMapFragment extends QMUIFragment<CollRunningMapFragmentView, CollRunningMapFragmentPresenter, FragmentRunningMapBinding> { | |
| 45 | 45 | private static final String TAG = "CollRunningMapFragment"; |
| 46 | 46 | private RunningMapBean runningMapBean; |
| 47 | 47 | private SensorManager mSensorManager; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/collectionTrace/ui/fragment/StartTraceFragment.java
| ... | ... | @@ -4,9 +4,7 @@ import android.hardware.Sensor; |
| 4 | 4 | import android.hardware.SensorManager; |
| 5 | 5 | import android.os.Bundle; |
| 6 | 6 | import android.support.annotation.Nullable; |
| 7 | -import android.util.Log; | |
| 8 | 7 | import android.view.View; |
| 9 | - | |
| 10 | 8 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 11 | 9 | import com.cnlive.moudle.collectionTrace.frame.presenter.StartTraceFragmentPresenter; |
| 12 | 10 | import com.cnlive.moudle.collectionTrace.frame.view.StartTraceFragmentView; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/commonInfo/CollCommonInfo.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.commonInfo; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | + | |
| 5 | +import com.cnlive.moudle.pedometer.utils.MySpHelper; | |
| 6 | + | |
| 7 | +public class CollCommonInfo { | |
| 8 | + | |
| 9 | + /** | |
| 10 | + * 设置计步时间 | |
| 11 | + * | |
| 12 | + * @param context | |
| 13 | + */ | |
| 14 | + public static void setStepStartTime(Context context, long currentTime) { | |
| 15 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putLong("CollstepStartTime", currentTime); | |
| 16 | + } | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * 获取计步时间 | |
| 20 | + * | |
| 21 | + * @param context | |
| 22 | + */ | |
| 23 | + public static long getStepStartTime(Context context) { | |
| 24 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getLong("CollstepStartTime", 0); | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * 是否允许写入步数 | |
| 29 | + * | |
| 30 | + * @param context | |
| 31 | + */ | |
| 32 | + public static void setIsWriteUserStep(Context context, boolean isAllow) { | |
| 33 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("CollwriteUserStep:", isAllow); | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * 是否允许写入步数 | |
| 38 | + * | |
| 39 | + * @param context | |
| 40 | + */ | |
| 41 | + public static boolean getIsWriteUserStep(Context context) { | |
| 42 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("CollwriteUserStep", true); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * @param context | |
| 47 | + * @param uid | |
| 48 | + */ | |
| 49 | + public static void setUserId(Context context, String uid) { | |
| 50 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putString("ColluserId", uid); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * @param context | |
| 55 | + */ | |
| 56 | + public static String getUserId(Context context) { | |
| 57 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getString("ColluserId", ""); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * 设置是否运行计步服务 | |
| 62 | + * | |
| 63 | + * @param context | |
| 64 | + * @param isAllow | |
| 65 | + */ | |
| 66 | + public static void setIsRunStepService(Context context, boolean isAllow) { | |
| 67 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("CollrunStepService", isAllow); | |
| 68 | + | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * 设置是否运行保活服务 | |
| 73 | + * | |
| 74 | + * @param context | |
| 75 | + * @param isAllow | |
| 76 | + */ | |
| 77 | + public static void setIsRunKeepAliveService(Context context, boolean isAllow) { | |
| 78 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("CollKeepAliveService", isAllow); | |
| 79 | + | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * 获取是否运行保活服务 | |
| 84 | + * | |
| 85 | + * @param context | |
| 86 | + */ | |
| 87 | + public static boolean getIsRunKeepAliveService(Context context) { | |
| 88 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("CollKeepAliveService", false); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * 获取是否运行计步服务 | |
| 93 | + * | |
| 94 | + * @param context | |
| 95 | + */ | |
| 96 | + public static boolean getIsRunStepService(Context context) { | |
| 97 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("CollrunStepService", false); | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * 设置赛事ID | |
| 102 | + * | |
| 103 | + * @param context | |
| 104 | + * @param streetId | |
| 105 | + */ | |
| 106 | + public static void setStreetId(Context context, String streetId) { | |
| 107 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putString("CollstreetId", streetId); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * 设置赛事ID | |
| 112 | + * | |
| 113 | + * @param context | |
| 114 | + */ | |
| 115 | + public static String getStreetId(Context context) { | |
| 116 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getString("CollstreetId", ""); | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * 设置是否运行鹰眼服务 | |
| 121 | + * | |
| 122 | + * @param context | |
| 123 | + * @param isRun | |
| 124 | + */ | |
| 125 | + public static void setTsRunBaiduTraceService(Context context, boolean isRun) { | |
| 126 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("CollRunBaiduTraceService", isRun); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * 获取是否运行鹰眼服务 | |
| 131 | + * | |
| 132 | + * @param context | |
| 133 | + */ | |
| 134 | + public static boolean getIsRunBaiduTraceService(Context context) { | |
| 135 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("CollRunBaiduTraceService", false); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /*重置 | |
| 139 | + * @param context | |
| 140 | + */ | |
| 141 | + public static void reset(Context context) { | |
| 142 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollwriteUserStep"); | |
| 143 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollstepStartTime"); | |
| 144 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollrunStepService"); | |
| 145 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollstepStartTime"); | |
| 146 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollKeepAliveService"); | |
| 147 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollRunBaiduTraceService"); | |
| 148 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("CollstreetId"); | |
| 149 | + | |
| 150 | + } | |
| 151 | + | |
| 152 | + | |
| 153 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/presenter/RunningMapFragmentPresenter.java
| ... | ... | @@ -430,8 +430,8 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 430 | 430 | |
| 431 | 431 | |
| 432 | 432 | // //获取围栏列表响应结果 |
| 433 | -// DeleteFenceRequest deleteFenceRequest = DeleteFenceRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null); | |
| 434 | -// StepService.mClient.deleteFence(deleteFenceRequest,this); | |
| 433 | +// DeleteFenceRequest deleteFenceRequest = DeleteFenceRequest.buildServerRequest(1, CollStepService.serviceId, "yangshuai", null); | |
| 434 | +// CollStepService.mClient.deleteFence(deleteFenceRequest,this); | |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | // 监控状态回调 |
| ... | ... | @@ -497,10 +497,10 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 497 | 497 | } |
| 498 | 498 | }; |
| 499 | 499 | // 创建本地圆形围栏 |
| 500 | -// if (StepService.mClient != null) { | |
| 500 | +// if (CollStepService.mClient != null) { | |
| 501 | 501 | // if (createFenceRequest == null) { |
| 502 | -// createFenceRequest = CreateFenceRequest.buildServerPolylineRequest(1, StepService.serviceId, "test", "yangshuai", latLngs, offset, denoise, CoordType.gcj02); | |
| 503 | -// StepService.mClient.createFence(createFenceRequest, onFenceListener); | |
| 502 | +// createFenceRequest = CreateFenceRequest.buildServerPolylineRequest(1, CollStepService.serviceId, "test", "yangshuai", latLngs, offset, denoise, CoordType.gcj02); | |
| 503 | +// CollStepService.mClient.createFence(createFenceRequest, onFenceListener); | |
| 504 | 504 | // } |
| 505 | 505 | // } |
| 506 | 506 | |
| ... | ... | @@ -530,10 +530,10 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 530 | 530 | * 以查询服务端围栏为例 |
| 531 | 531 | * fenceIds:服务端围栏编号列表,List<Long>,如传入null,表示查询所有围栏 |
| 532 | 532 | */ |
| 533 | -// FenceListRequest request = FenceListRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 533 | +// FenceListRequest request = FenceListRequest.buildServerRequest(1, CollStepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 534 | 534 | // //发起查询围栏请求 |
| 535 | -// if (StepService.mClient != null) { | |
| 536 | -// StepService.mClient.queryFenceList(request, fenceListener); | |
| 535 | +// if (CollStepService.mClient != null) { | |
| 536 | +// CollStepService.mClient.queryFenceList(request, fenceListener); | |
| 537 | 537 | // } |
| 538 | 538 | } |
| 539 | 539 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunMainFragmentView.java
| ... | ... | @@ -117,6 +117,10 @@ public class RunMainFragmentView implements MvpView { |
| 117 | 117 | public void initView() { |
| 118 | 118 | uid = "yangshuai"; |
| 119 | 119 | CommonInfo.setUserId(getContext(), uid); |
| 120 | + UserStepEntity userStepEntity = StepUtil.getUserStepData(getContext(), uid); | |
| 121 | + if (userStepEntity != null) { | |
| 122 | + StepUtil.clearRunData(getContext(), CommonInfo.getUserId(getContext())); | |
| 123 | + } | |
| 120 | 124 | initLocationClient(); |
| 121 | 125 | fragment.binding.llRunMainRoot.setVisibility(View.GONE); |
| 122 | 126 | ButtonAnimUtil.startAnimation(fragment.binding.llStartTimeTip, false); |
| ... | ... | @@ -178,37 +182,41 @@ public class RunMainFragmentView implements MvpView { |
| 178 | 182 | |
| 179 | 183 | } |
| 180 | 184 | }); |
| 181 | - UserStepEntity userStepEntity = StepUtil.getUserStepData(getContext(), uid); | |
| 182 | - boolean isRunStep = CommonInfo.getIsRunStepService(getContext()); | |
| 185 | + | |
| 186 | +// boolean isRunStep = CommonInfo.getIsRunStepService(getContext()); | |
| 183 | 187 | //当前用户已经在运动 |
| 184 | - if (!isRunStep) { | |
| 185 | - //依次倒计时 | |
| 186 | - handler.postDelayed(runnable, 1000); | |
| 187 | - } else { | |
| 188 | - //隐藏计时界面 | |
| 189 | - fragment.binding.llStartTimeTip.setVisibility(View.GONE); | |
| 190 | - fragment.binding.llRunMainRoot.setVisibility(View.VISIBLE); | |
| 191 | - new Handler().postDelayed(new Runnable() { | |
| 192 | - @Override | |
| 193 | - public void run() { | |
| 194 | - //开始计时 | |
| 195 | - if (firstTimeFlag) { | |
| 196 | - firstTimeFlag = false; | |
| 197 | - if (userStepEntity != null) { | |
| 198 | - if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | |
| 199 | - fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | |
| 200 | - } else { | |
| 201 | - fragment.binding.chronometer.setFormat("00:00:01"); | |
| 202 | - fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | |
| 203 | - } | |
| 204 | - } | |
| 205 | - fragment.binding.chronometer.start(); | |
| 206 | - } | |
| 207 | - } | |
| 208 | - }, 10); | |
| 209 | - //开启计步服务 | |
| 210 | - startStepService(getContext(), uid); | |
| 211 | - } | |
| 188 | +// if (!isRunStep) { | |
| 189 | + //依次倒计时 | |
| 190 | + handler.postDelayed(runnable, 1000); | |
| 191 | +// } else { | |
| 192 | +// //隐藏计时界面 | |
| 193 | +// fragment.binding.llStartTimeTip.setVisibility(View.GONE); | |
| 194 | +// fragment.binding.llRunMainRoot.setVisibility(View.VISIBLE); | |
| 195 | +// new Handler().postDelayed(new Runnable() { | |
| 196 | +// @Override | |
| 197 | +// public void run() { | |
| 198 | +// //开始计时 | |
| 199 | +// if (firstTimeFlag) { | |
| 200 | +// firstTimeFlag = false; | |
| 201 | +// if (userStepEntity != null) { | |
| 202 | +// if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | |
| 203 | +// fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | |
| 204 | +// } else { | |
| 205 | +// fragment.binding.chronometer.setFormat("00:00:01"); | |
| 206 | +// fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | |
| 207 | +// } | |
| 208 | +// //设置步数 | |
| 209 | +// if (!TextUtils.isEmpty(userStepEntity.getStepCount())) { | |
| 210 | +// fragment.binding.tvStepCount.setText(userStepEntity.getStepCount()); | |
| 211 | +// } | |
| 212 | +// } | |
| 213 | +// fragment.binding.chronometer.start(); | |
| 214 | +// } | |
| 215 | +// } | |
| 216 | +// }, 10); | |
| 217 | +// //开启计步服务 | |
| 218 | +// startStepService(getContext(), uid); | |
| 219 | +// } | |
| 212 | 220 | |
| 213 | 221 | //设置继续按钮,缩放效果 |
| 214 | 222 | fragment.binding.flContinue.setOnTouchListener(new View.OnTouchListener() { | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/model/RunningMapBean.java
| ... | ... | @@ -11,7 +11,7 @@ public class RunningMapBean implements Serializable { |
| 11 | 11 | public RunningMapBean() { |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | -// public RunningMapBean(long timeBase, String formatTime) { | |
| 14 | +// public CollRunningMapBean(long timeBase, String formatTime) { | |
| 15 | 15 | // this.timeBase = timeBase; |
| 16 | 16 | // this.formatTime = formatTime; |
| 17 | 17 | // } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/net/bean/RunRaceDetailInfo.java
| ... | ... | @@ -20,6 +20,8 @@ public class RunRaceDetailInfo { |
| 20 | 20 | * ranking : true |
| 21 | 21 | * individualAchievement : true |
| 22 | 22 | * signResult : false |
| 23 | + * trackButton : true | |
| 24 | + * state : 0 | |
| 23 | 25 | * faceUrl : ["http://yweb0.cnliveimg.com/images/headImg/2019/0505/1557039035723_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2019/0505/1557034319038_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2018/0902/1535858459612_small.jpg"] |
| 24 | 26 | * finishFaceUrl : ["http://yweb0.cnliveimg.com/images/headImg/2019/0505/1557039035723_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2019/0523/1558599016203_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2019/0523/1558599016203_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2019/0523/1558599016203_small.jpg","http://yweb0.cnliveimg.com/images/headImg/2019/0523/1558599016203_small.jpg"] |
| 25 | 27 | * finishNum : 33 |
| ... | ... | @@ -50,6 +52,8 @@ public class RunRaceDetailInfo { |
| 50 | 52 | private boolean ranking; |
| 51 | 53 | private boolean individualAchievement; |
| 52 | 54 | private boolean signResult; |
| 55 | + private boolean trackButton; | |
| 56 | + private String state; | |
| 53 | 57 | private String finishNum; |
| 54 | 58 | private String firstEmployTime; |
| 55 | 59 | private String firstFaceUrl; |
| ... | ... | @@ -142,6 +146,22 @@ public class RunRaceDetailInfo { |
| 142 | 146 | this.signResult = signResult; |
| 143 | 147 | } |
| 144 | 148 | |
| 149 | + public boolean isTrackButton() { | |
| 150 | + return trackButton; | |
| 151 | + } | |
| 152 | + | |
| 153 | + public void setTrackButton(boolean trackButton) { | |
| 154 | + this.trackButton = trackButton; | |
| 155 | + } | |
| 156 | + | |
| 157 | + public String getState() { | |
| 158 | + return state; | |
| 159 | + } | |
| 160 | + | |
| 161 | + public void setState(String state) { | |
| 162 | + this.state = state; | |
| 163 | + } | |
| 164 | + | |
| 145 | 165 | public String getFinishNum() { |
| 146 | 166 | return finishNum; |
| 147 | 167 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/receivers/ServiceReceiver.java
| ... | ... | @@ -21,7 +21,7 @@ import com.cnlive.moudle.pedometer.utils.ServiceUtils; |
| 21 | 21 | public class ServiceReceiver extends BroadcastReceiver { |
| 22 | 22 | //唤醒锁 |
| 23 | 23 | private PowerManager.WakeLock wakeLock = null; |
| 24 | - private final String TAG = "ServiceReceiver"; | |
| 24 | + private final String TAG = "CollServiceReceiver"; | |
| 25 | 25 | |
| 26 | 26 | public ServiceReceiver() { |
| 27 | 27 | |
| ... | ... | @@ -39,7 +39,7 @@ public class ServiceReceiver extends BroadcastReceiver { |
| 39 | 39 | //todo 这里判断要保活的服务 |
| 40 | 40 | boolean isRunStepService = CommonInfo.getIsRunStepService(context); |
| 41 | 41 | if (isRunStepService) { |
| 42 | - if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.StepService")) { | |
| 42 | + if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.CollStepService")) { | |
| 43 | 43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 44 | 44 | context.startForegroundService(new Intent(context, StepService.class)); |
| 45 | 45 | } else { |
| ... | ... | @@ -65,7 +65,7 @@ public class ServiceReceiver extends BroadcastReceiver { |
| 65 | 65 | //todo 保活 |
| 66 | 66 | boolean isRunKeepAliveService = CommonInfo.getIsRunKeepAliveService(context); |
| 67 | 67 | if (isRunKeepAliveService) { |
| 68 | - if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.KeepLiveService")) { | |
| 68 | + if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.CollKeepLiveService")) { | |
| 69 | 69 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 70 | 70 | context.startForegroundService(new Intent(context, KeepLiveService.class)); |
| 71 | 71 | } else { |
| ... | ... | @@ -103,9 +103,9 @@ public class ServiceReceiver extends BroadcastReceiver { |
| 103 | 103 | // * 发送关闭广播 |
| 104 | 104 | // */ |
| 105 | 105 | // private void sendCloseBroadCast(Context context) { |
| 106 | -// Intent intent = new Intent("close.StepService"); | |
| 106 | +// Intent intent = new Intent("close.CollStepService"); | |
| 107 | 107 | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 108 | -// intent.setComponent(new ComponentName(context.getPackageName(), "com.cnlive.moudle.pedometer.service.StepService"));//高版本Android发送广播需要加这句 | |
| 108 | +// intent.setComponent(new ComponentName(context.getPackageName(), "com.cnlive.moudle.pedometer.service.CollStepService"));//高版本Android发送广播需要加这句 | |
| 109 | 109 | // } |
| 110 | 110 | // context.sendBroadcast(intent); |
| 111 | 111 | // } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/JobSchedulerService.java
| ... | ... | @@ -14,14 +14,14 @@ import android.util.Log; |
| 14 | 14 | |
| 15 | 15 | import com.cnlive.moudle.pedometer.utils.ServiceUtils; |
| 16 | 16 | |
| 17 | -//if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.StepService")) { | |
| 18 | -// startService(new Intent(getApplicationContext(), StepService.class)); | |
| 17 | +//if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.CollStepService")) { | |
| 18 | +// startService(new Intent(getApplicationContext(), CollStepService.class)); | |
| 19 | 19 | // Log.e("启动服务", "启动StepService"); |
| 20 | 20 | // } |
| 21 | 21 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) |
| 22 | 22 | public class JobSchedulerService extends JobService { |
| 23 | 23 | |
| 24 | - private static final String TAG = "JobSchedulerService"; | |
| 24 | + private static final String TAG = "CollJobSchedulerService"; | |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * @param params Parameters specifying info about this job, including the extras bundle you |
| ... | ... | @@ -32,7 +32,7 @@ public class JobSchedulerService extends JobService { |
| 32 | 32 | @Override |
| 33 | 33 | public boolean onStartJob(JobParameters params) { |
| 34 | 34 | android.util.Log.e(TAG, "onStartJob: start"); |
| 35 | - if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.StepService")) { | |
| 35 | + if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.CollStepService")) { | |
| 36 | 36 | startService(new Intent(getApplicationContext(), StepService.class)); |
| 37 | 37 | Log.e("启动服务", "启动StepService"); |
| 38 | 38 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/KeepLiveService.java
| ... | ... | @@ -44,7 +44,7 @@ public class KeepLiveService extends Service implements MediaPlayer.OnCompletion |
| 44 | 44 | public static boolean isKeepService = true; |
| 45 | 45 | public static KeepLiveService instance; |
| 46 | 46 | private int startId; |
| 47 | - private final String TAG = "KeepLiveService"; | |
| 47 | + private final String TAG = "CollKeepLiveService"; | |
| 48 | 48 | private Handler handler; |
| 49 | 49 | private Runnable runnable; |
| 50 | 50 | |
| ... | ... | @@ -108,7 +108,7 @@ public class KeepLiveService extends Service implements MediaPlayer.OnCompletion |
| 108 | 108 | // intentFilter.addAction(Intent.ACTION_TIME_CHANGED); |
| 109 | 109 | // intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); |
| 110 | 110 | // intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); |
| 111 | - intentFilter.addAction("com.cnlive.strike.receiver.ServiceReceiver"); | |
| 111 | + intentFilter.addAction("com.cnlive.strike.receiver.CollServiceReceiver"); | |
| 112 | 112 | intentFilter.addAction("close.KeepService"); |
| 113 | 113 | registerReceiver(myReceiver, intentFilter); |
| 114 | 114 | } |
| ... | ... | @@ -136,7 +136,7 @@ public class KeepLiveService extends Service implements MediaPlayer.OnCompletion |
| 136 | 136 | //判断是否运行计步服务 |
| 137 | 137 | boolean isRunStepService = CommonInfo.getIsRunStepService(getApplicationContext()); |
| 138 | 138 | if (isRunStepService) { |
| 139 | - if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.StepService")) { | |
| 139 | + if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.CollStepService")) { | |
| 140 | 140 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 141 | 141 | startForegroundService(new Intent(getApplicationContext(), StepService.class)); |
| 142 | 142 | } else { |
| ... | ... | @@ -275,9 +275,9 @@ public class KeepLiveService extends Service implements MediaPlayer.OnCompletion |
| 275 | 275 | if (isRun) { |
| 276 | 276 | Log.e(TAG, "重新播放"); |
| 277 | 277 | mediaPlayer.start(); |
| 278 | - Intent intent = new Intent("com.cnlive.strike.receiver.ServiceReceiver"); | |
| 278 | + Intent intent = new Intent("com.cnlive.strike.receiver.CollServiceReceiver"); | |
| 279 | 279 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 280 | - intent.setComponent(new ComponentName(getPackageName(), "com.cnlive.strike.receiver.ServiceReceiver"));//高版本Android发送广播需要加这句 | |
| 280 | + intent.setComponent(new ComponentName(getPackageName(), "com.cnlive.strike.receiver.CollServiceReceiver"));//高版本Android发送广播需要加这句 | |
| 281 | 281 | } |
| 282 | 282 | sendBroadcast(intent); |
| 283 | 283 | } else { | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/StepService.java
| ... | ... | @@ -63,7 +63,7 @@ import java.util.UUID; |
| 63 | 63 | |
| 64 | 64 | public class StepService extends Service implements SensorEventListener { |
| 65 | 65 | |
| 66 | - private String TAG = "StepService"; | |
| 66 | + private String TAG = "CollStepService"; | |
| 67 | 67 | public static boolean isKeep = true; |
| 68 | 68 | |
| 69 | 69 | /** |
| ... | ... | @@ -170,7 +170,7 @@ public class StepService extends Service implements SensorEventListener { |
| 170 | 170 | super.onCreate(); |
| 171 | 171 | init(); |
| 172 | 172 | initBroadcastReceiver(); |
| 173 | - if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.KeepLiveService")) { | |
| 173 | + if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.CollKeepLiveService")) { | |
| 174 | 174 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 175 | 175 | startForegroundService(new Intent(getApplicationContext(), KeepLiveService.class)); |
| 176 | 176 | } else { |
| ... | ... | @@ -385,7 +385,7 @@ public class StepService extends Service implements SensorEventListener { |
| 385 | 385 | } |
| 386 | 386 | // if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { |
| 387 | 387 | // //开始配置JobInfo |
| 388 | -// mServieComponent = new ComponentName(this, JobSchedulerService.class);// 获取到我们自己的jobservice,同时启动该service | |
| 388 | +// mServieComponent = new ComponentName(this, CollJobSchedulerService.class);// 获取到我们自己的jobservice,同时启动该service | |
| 389 | 389 | // JobInfo.Builder builder = new JobInfo.Builder(mJobId++, mServieComponent); |
| 390 | 390 | //// builder.setOverrideDeadline(Long.valueOf(2) * 1000); |
| 391 | 391 | // //设置任务每隔intervalMillis运行一次 |
| ... | ... | @@ -641,7 +641,7 @@ public class StepService extends Service implements SensorEventListener { |
| 641 | 641 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
| 642 | 642 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 643 | 643 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 644 | - filter.addAction("close.StepService"); | |
| 644 | + filter.addAction("close.CollStepService"); | |
| 645 | 645 | mBatInfoReceiver = new BroadcastReceiver() { |
| 646 | 646 | @Override |
| 647 | 647 | public void onReceive(final Context context, final Intent intent) { |
| ... | ... | @@ -680,7 +680,7 @@ public class StepService extends Service implements SensorEventListener { |
| 680 | 680 | // Logger.d("重置步数" + StepDcretor.CURRENT_STEP); |
| 681 | 681 | save(); |
| 682 | 682 | // isNewDay(); |
| 683 | - } else if ("close.StepService".equals(action)) { | |
| 683 | + } else if ("close.CollStepService".equals(action)) { | |
| 684 | 684 | Log.e("关闭StepService", "关闭"); |
| 685 | 685 | onDestroy(); |
| 686 | 686 | stopSelf(); |
| ... | ... | @@ -979,7 +979,7 @@ public class StepService extends Service implements SensorEventListener { |
| 979 | 979 | /** |
| 980 | 980 | * 获取当前service对象 |
| 981 | 981 | * |
| 982 | - * @return StepService | |
| 982 | + * @return CollStepService | |
| 983 | 983 | */ |
| 984 | 984 | public StepService getService() { |
| 985 | 985 | return StepService.this; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/sql/dao/DaoMaster.java
| ... | ... | @@ -22,11 +22,13 @@ public class DaoMaster extends AbstractDaoMaster { |
| 22 | 22 | /** Creates underlying database table using DAOs. */ |
| 23 | 23 | public static void createAllTables(Database db, boolean ifNotExists) { |
| 24 | 24 | UserStepEntityDao.createTable(db, ifNotExists); |
| 25 | + CollUserStepEntityDao.createTable(db, ifNotExists); | |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | 28 | /** Drops underlying database table using DAOs. */ |
| 28 | 29 | public static void dropAllTables(Database db, boolean ifExists) { |
| 29 | 30 | UserStepEntityDao.dropTable(db, ifExists); |
| 31 | + CollUserStepEntityDao.dropTable(db, ifExists); | |
| 30 | 32 | } |
| 31 | 33 | |
| 32 | 34 | /** |
| ... | ... | @@ -46,6 +48,7 @@ public class DaoMaster extends AbstractDaoMaster { |
| 46 | 48 | public DaoMaster(Database db) { |
| 47 | 49 | super(db, SCHEMA_VERSION); |
| 48 | 50 | registerDaoClass(UserStepEntityDao.class); |
| 51 | + registerDaoClass(CollUserStepEntityDao.class); | |
| 49 | 52 | } |
| 50 | 53 | |
| 51 | 54 | public DaoSession newSession() { | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/sql/dao/DaoSession.java
| ... | ... | @@ -9,8 +9,10 @@ import org.greenrobot.greendao.identityscope.IdentityScopeType; |
| 9 | 9 | import org.greenrobot.greendao.internal.DaoConfig; |
| 10 | 10 | |
| 11 | 11 | import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; |
| 12 | +import com.cnlive.moudle.pedometer.sql.entity.CollUserStepEntity; | |
| 12 | 13 | |
| 13 | 14 | import com.cnlive.moudle.pedometer.sql.dao.UserStepEntityDao; |
| 15 | +import com.cnlive.moudle.pedometer.sql.dao.CollUserStepEntityDao; | |
| 14 | 16 | |
| 15 | 17 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. |
| 16 | 18 | |
| ... | ... | @@ -22,8 +24,10 @@ import com.cnlive.moudle.pedometer.sql.dao.UserStepEntityDao; |
| 22 | 24 | public class DaoSession extends AbstractDaoSession { |
| 23 | 25 | |
| 24 | 26 | private final DaoConfig userStepEntityDaoConfig; |
| 27 | + private final DaoConfig collUserStepEntityDaoConfig; | |
| 25 | 28 | |
| 26 | 29 | private final UserStepEntityDao userStepEntityDao; |
| 30 | + private final CollUserStepEntityDao collUserStepEntityDao; | |
| 27 | 31 | |
| 28 | 32 | public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig> |
| 29 | 33 | daoConfigMap) { |
| ... | ... | @@ -32,17 +36,27 @@ public class DaoSession extends AbstractDaoSession { |
| 32 | 36 | userStepEntityDaoConfig = daoConfigMap.get(UserStepEntityDao.class).clone(); |
| 33 | 37 | userStepEntityDaoConfig.initIdentityScope(type); |
| 34 | 38 | |
| 39 | + collUserStepEntityDaoConfig = daoConfigMap.get(CollUserStepEntityDao.class).clone(); | |
| 40 | + collUserStepEntityDaoConfig.initIdentityScope(type); | |
| 41 | + | |
| 35 | 42 | userStepEntityDao = new UserStepEntityDao(userStepEntityDaoConfig, this); |
| 43 | + collUserStepEntityDao = new CollUserStepEntityDao(collUserStepEntityDaoConfig, this); | |
| 36 | 44 | |
| 37 | 45 | registerDao(UserStepEntity.class, userStepEntityDao); |
| 46 | + registerDao(CollUserStepEntity.class, collUserStepEntityDao); | |
| 38 | 47 | } |
| 39 | 48 | |
| 40 | 49 | public void clear() { |
| 41 | 50 | userStepEntityDaoConfig.clearIdentityScope(); |
| 51 | + collUserStepEntityDaoConfig.clearIdentityScope(); | |
| 42 | 52 | } |
| 43 | 53 | |
| 44 | 54 | public UserStepEntityDao getUserStepEntityDao() { |
| 45 | 55 | return userStepEntityDao; |
| 46 | 56 | } |
| 47 | 57 | |
| 58 | + public CollUserStepEntityDao getCollUserStepEntityDao() { | |
| 59 | + return collUserStepEntityDao; | |
| 60 | + } | |
| 61 | + | |
| 48 | 62 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/sql/entity/CollUserStepEntity.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.sql.entity; | |
| 2 | + | |
| 3 | +import org.greenrobot.greendao.annotation.Entity; | |
| 4 | +import org.greenrobot.greendao.annotation.Generated; | |
| 5 | +import org.greenrobot.greendao.annotation.Id; | |
| 6 | + | |
| 7 | +@Entity | |
| 8 | +public class CollUserStepEntity { | |
| 9 | + @Id | |
| 10 | + private String uuid; | |
| 11 | + private String userId; | |
| 12 | + private String date; | |
| 13 | + private String stepCount; | |
| 14 | + private long pauseTimeStamp;//暂停时间戳 | |
| 15 | + private long recordTime;//暂停时间 | |
| 16 | + private double caloriesCount;//卡路里 | |
| 17 | + private String serverTraceData;//服务端赛道数据 | |
| 18 | + @Generated(hash = 1080314982) | |
| 19 | + public CollUserStepEntity(String uuid, String userId, String date, | |
| 20 | + String stepCount, long pauseTimeStamp, long recordTime, | |
| 21 | + double caloriesCount, String serverTraceData) { | |
| 22 | + this.uuid = uuid; | |
| 23 | + this.userId = userId; | |
| 24 | + this.date = date; | |
| 25 | + this.stepCount = stepCount; | |
| 26 | + this.pauseTimeStamp = pauseTimeStamp; | |
| 27 | + this.recordTime = recordTime; | |
| 28 | + this.caloriesCount = caloriesCount; | |
| 29 | + this.serverTraceData = serverTraceData; | |
| 30 | + } | |
| 31 | + @Generated(hash = 681143087) | |
| 32 | + public CollUserStepEntity() { | |
| 33 | + } | |
| 34 | + public String getUuid() { | |
| 35 | + return this.uuid; | |
| 36 | + } | |
| 37 | + public void setUuid(String uuid) { | |
| 38 | + this.uuid = uuid; | |
| 39 | + } | |
| 40 | + public String getUserId() { | |
| 41 | + return this.userId; | |
| 42 | + } | |
| 43 | + public void setUserId(String userId) { | |
| 44 | + this.userId = userId; | |
| 45 | + } | |
| 46 | + public String getDate() { | |
| 47 | + return this.date; | |
| 48 | + } | |
| 49 | + public void setDate(String date) { | |
| 50 | + this.date = date; | |
| 51 | + } | |
| 52 | + public String getStepCount() { | |
| 53 | + return this.stepCount; | |
| 54 | + } | |
| 55 | + public void setStepCount(String stepCount) { | |
| 56 | + this.stepCount = stepCount; | |
| 57 | + } | |
| 58 | + public long getPauseTimeStamp() { | |
| 59 | + return this.pauseTimeStamp; | |
| 60 | + } | |
| 61 | + public void setPauseTimeStamp(long pauseTimeStamp) { | |
| 62 | + this.pauseTimeStamp = pauseTimeStamp; | |
| 63 | + } | |
| 64 | + public long getRecordTime() { | |
| 65 | + return this.recordTime; | |
| 66 | + } | |
| 67 | + public void setRecordTime(long recordTime) { | |
| 68 | + this.recordTime = recordTime; | |
| 69 | + } | |
| 70 | + public double getCaloriesCount() { | |
| 71 | + return this.caloriesCount; | |
| 72 | + } | |
| 73 | + public void setCaloriesCount(double caloriesCount) { | |
| 74 | + this.caloriesCount = caloriesCount; | |
| 75 | + } | |
| 76 | + public String getServerTraceData() { | |
| 77 | + return this.serverTraceData; | |
| 78 | + } | |
| 79 | + public void setServerTraceData(String serverTraceData) { | |
| 80 | + this.serverTraceData = serverTraceData; | |
| 81 | + } | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/activity/PedometerMainActivity.java
| ... | ... | @@ -71,7 +71,7 @@ public class PedometerMainActivity extends QMUIFragmentActivity { |
| 71 | 71 | isOpen = notificationManagerCompat.areNotificationsEnabled(); |
| 72 | 72 | if (isOpen) { |
| 73 | 73 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 74 | -// ContextCompat.startForegroundService(CollPedometerMainActivity.this, new Intent(CollPedometerMainActivity.this, StepService.class)); | |
| 74 | +// ContextCompat.startForegroundService(CollPedometerMainActivity.this, new Intent(CollPedometerMainActivity.this, CollStepService.class)); | |
| 75 | 75 | } |
| 76 | 76 | } else { |
| 77 | 77 | goToSettingNo(); | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/activity/RunRaceDetailActivity.java
| ... | ... | @@ -54,15 +54,15 @@ public class RunRaceDetailActivity extends QMUIFragmentActivity { |
| 54 | 54 | protected void onCreate(Bundle savedInstanceState) { |
| 55 | 55 | super.onCreate(savedInstanceState); |
| 56 | 56 | //判断用户是否结束运动 |
| 57 | - boolean isRunStep = CommonInfo.getIsRunStepService(this); | |
| 58 | - //没有找到相关记录 | |
| 59 | - if (!isRunStep) { | |
| 57 | +// boolean isRunStep = CommonInfo.getIsRunStepService(this); | |
| 58 | +// //没有找到相关记录 | |
| 59 | +// if (!isRunStep) { | |
| 60 | 60 | fragment = new RunRaceDetailFragment(); |
| 61 | - } | |
| 62 | - //找到记录 | |
| 63 | - else { | |
| 64 | - fragment = new RunMainFragment(); | |
| 65 | - } | |
| 61 | +// } | |
| 62 | +// //找到记录 | |
| 63 | +// else { | |
| 64 | +// fragment = new RunMainFragment(); | |
| 65 | +// } | |
| 66 | 66 | if (fragment != null) { |
| 67 | 67 | addFragment(fragment); |
| 68 | 68 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/utils/CollStepUtil.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.utils; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.text.TextUtils; | |
| 5 | +import android.util.Log; | |
| 6 | + | |
| 7 | +import com.cnlive.moudle.pedometer.sql.dao.CollUserStepEntityDao; | |
| 8 | +import com.cnlive.moudle.pedometer.sql.dao.UserStepEntityDao; | |
| 9 | +import com.cnlive.moudle.pedometer.sql.db.DbCore; | |
| 10 | +import com.cnlive.moudle.pedometer.sql.entity.CollUserStepEntity; | |
| 11 | +import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; | |
| 12 | + | |
| 13 | +import java.text.SimpleDateFormat; | |
| 14 | +import java.util.Date; | |
| 15 | +import java.util.List; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 步数工具 | |
| 19 | + * | |
| 20 | + * @author ShinnyYang | |
| 21 | + */ | |
| 22 | +public class CollStepUtil { | |
| 23 | + private static final String TAG = "CollStepUtil"; | |
| 24 | + private static final String dbName = "CollStep.db"; | |
| 25 | + | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * 计算配速 | |
| 29 | + */ | |
| 30 | + public static String getPaceSpeed(double second, double distance) { | |
| 31 | + if (distance != 0) { | |
| 32 | + //计算分 | |
| 33 | + int minute = (int) (second / distance / 60); | |
| 34 | + //获取秒 | |
| 35 | + double miaoMin = ((double) (second / distance / 60)) - ((int) (second / distance / 60)); | |
| 36 | + int miao = (int) (miaoMin * 60); | |
| 37 | + return minute + "'" + miao + "''"; | |
| 38 | + } else { | |
| 39 | + return "--"; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 更新数据 | |
| 45 | + * | |
| 46 | + * @param context | |
| 47 | + * @param userStepEntity | |
| 48 | + */ | |
| 49 | + public static void updateStepData(Context context, CollUserStepEntity userStepEntity) { | |
| 50 | + if (userStepEntity != null) { | |
| 51 | + DbCore.init(context, dbName); | |
| 52 | + DbCore.getDaoSession().getCollUserStepEntityDao().update(userStepEntity); | |
| 53 | + } | |
| 54 | + | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * 得到当前速度 | |
| 59 | + * | |
| 60 | + * @param time | |
| 61 | + * @param distance | |
| 62 | + * @return | |
| 63 | + */ | |
| 64 | + public static double getCurrentSpeed(double time, double distance) { | |
| 65 | + double result = distance / time; | |
| 66 | + result = Double.parseDouble(String.format("%1.2f", result)); | |
| 67 | + return result; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * 根据行走步数,计算大概行走具体 | |
| 72 | + * 单位:公里 | |
| 73 | + * 1步的距离大概是60厘米。 | |
| 74 | + * 1万多步,距离大概就是600000厘米,即6000米(6公里) | |
| 75 | + * | |
| 76 | + * @param step | |
| 77 | + * @return | |
| 78 | + */ | |
| 79 | + public static double getWalkDistanceKM(long step) { | |
| 80 | + double result = step * 60.0 / 100.0 / 1000.0; | |
| 81 | + result = Double.parseDouble(String.format("%1.2f", result)); | |
| 82 | + return result; | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * 清除保存数据库中的数据 | |
| 87 | + * | |
| 88 | + * @param uid | |
| 89 | + */ | |
| 90 | + public static void clearRunData(Context context, String uid) { | |
| 91 | + DbCore.init(context, dbName); | |
| 92 | + String CURRENT_DATE = getTodayDate(); | |
| 93 | + List<CollUserStepEntity> list = DbCore.getDaoSession().getCollUserStepEntityDao().queryBuilder().where(CollUserStepEntityDao.Properties.UserId.eq(uid), CollUserStepEntityDao.Properties.Date.eq(CURRENT_DATE)).list(); | |
| 94 | + if (list != null && list.size() > 0) { | |
| 95 | + Log.e(TAG, "clearRunData: " + list.size()); | |
| 96 | + DbCore.getDaoSession().getCollUserStepEntityDao().deleteByKey(list.get(0).getUuid()); | |
| 97 | + DbCore.getDaoSession().getCollUserStepEntityDao().detachAll(); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * 获取用户数据 | |
| 103 | + * | |
| 104 | + * @param context | |
| 105 | + * @param uid | |
| 106 | + * @return | |
| 107 | + */ | |
| 108 | + public static CollUserStepEntity getCollUserStepData(Context context, String uid) { | |
| 109 | + if (!TextUtils.isEmpty(uid)) { | |
| 110 | + DbCore.init(context, dbName); | |
| 111 | + DbCore.getDaoSession().getCollUserStepEntityDao().detachAll(); | |
| 112 | + DbCore.getDaoSession().clear(); | |
| 113 | + String CURRENT_DATE = getTodayDate(); | |
| 114 | + List<CollUserStepEntity> list = DbCore.getDaoSession().getCollUserStepEntityDao().queryBuilder().where(CollUserStepEntityDao.Properties.UserId.eq(uid), CollUserStepEntityDao.Properties.Date.eq(CURRENT_DATE)).list(); | |
| 115 | + if (list != null && list.size() > 0) { | |
| 116 | + return list.get(0); | |
| 117 | + } else { | |
| 118 | + return null; | |
| 119 | + } | |
| 120 | + } else { | |
| 121 | + return null; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * 获取用户数据 | |
| 127 | + * | |
| 128 | + * @param context | |
| 129 | + * @param uid | |
| 130 | + * @return | |
| 131 | + */ | |
| 132 | + public static void setServerTraceData(Context context, String uid, CollUserStepEntity userStepEntity) { | |
| 133 | + if (getCollUserStepData(context, uid) != null) { | |
| 134 | + CollUserStepEntity entity = getCollUserStepData(context, uid); | |
| 135 | + } else { | |
| 136 | + | |
| 137 | + } | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * 获取当天日期 | |
| 142 | + * | |
| 143 | + * @return | |
| 144 | + */ | |
| 145 | + private static String getTodayDate() { | |
| 146 | + Date date = new Date(System.currentTimeMillis()); | |
| 147 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
| 148 | + return sdf.format(date); | |
| 149 | + } | |
| 150 | + | |
| 151 | + | |
| 152 | +} | ... | ... |
moudle_pedometer/src/main/res/layout/fragment_run_race_detail.xml
| ... | ... | @@ -56,14 +56,7 @@ |
| 56 | 56 | android:background="@drawable/xqt_share" |
| 57 | 57 | android:visibility="gone" /> |
| 58 | 58 | <!----> |
| 59 | - <com.cnlive.strike.ui.widget.UIEmptyView | |
| 60 | - android:id="@+id/empty_layout" | |
| 61 | - android:layout_width="match_parent" | |
| 62 | - android:layout_height="match_parent" | |
| 63 | - android:background="@color/color_fff" | |
| 64 | - android:focusable="true" | |
| 65 | - android:focusableInTouchMode="true" | |
| 66 | - android:visibility="gone" /> | |
| 59 | + | |
| 67 | 60 | |
| 68 | 61 | </FrameLayout> |
| 69 | 62 | |
| ... | ... | @@ -148,5 +141,13 @@ |
| 148 | 141 | android:visibility="gone" /> |
| 149 | 142 | |
| 150 | 143 | </RelativeLayout> |
| 144 | + <com.cnlive.strike.ui.widget.UIEmptyView | |
| 145 | + android:id="@+id/empty_layout" | |
| 146 | + android:layout_width="match_parent" | |
| 147 | + android:layout_height="match_parent" | |
| 148 | + android:background="@color/color_fff" | |
| 149 | + android:focusable="true" | |
| 150 | + android:focusableInTouchMode="true" | |
| 151 | + android:visibility="gone" /> | |
| 151 | 152 | </FrameLayout> |
| 152 | 153 | </layout> |
| 153 | 154 | \ No newline at end of file | ... | ... |