Commit a72f39f5b1324817065ff14bdb57b810813e6eaa
1 parent
50a62a39
1.;跑步结束页面
Showing
9 changed files
with
140 additions
and
318 deletions
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/commonInfo/CommonInfo.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 | +import com.cnlive.moudle.pedometer.utils.TimeUtil; | ||
| 7 | + | ||
| 8 | +public class CommonInfo { | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * 设置计步时间 | ||
| 12 | + * | ||
| 13 | + * @param context | ||
| 14 | + */ | ||
| 15 | + public static void setStepStartTime(Context context, long currentTime) { | ||
| 16 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putLong("stepStartTime", currentTime); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * 获取计步时间 | ||
| 21 | + * | ||
| 22 | + * @param context | ||
| 23 | + */ | ||
| 24 | + public static long getStepStartTime(Context context) { | ||
| 25 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getLong("stepStartTime", 0); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 是否允许写入步数 | ||
| 30 | + * | ||
| 31 | + * @param context | ||
| 32 | + */ | ||
| 33 | + public static void setIsWriteUserStep(Context context, boolean isAllow) { | ||
| 34 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("writeUserStep:", isAllow); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 是否允许写入步数 | ||
| 39 | + * | ||
| 40 | + * @param context | ||
| 41 | + */ | ||
| 42 | + public static boolean getIsWriteUserStep(Context context) { | ||
| 43 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("writeUserStep", true); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * @param context | ||
| 48 | + * @param uid | ||
| 49 | + */ | ||
| 50 | + public static void setUserId(Context context, String uid) { | ||
| 51 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putString("userId", uid); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * @param context | ||
| 56 | + */ | ||
| 57 | + public static void getUserId(Context context) { | ||
| 58 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getString("userId", ""); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * 设置是否运行计步服务 | ||
| 63 | + * | ||
| 64 | + * @param context | ||
| 65 | + * @param isAllow | ||
| 66 | + */ | ||
| 67 | + public static void setIsRunStepService(Context context, boolean isAllow) { | ||
| 68 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).putBoolean("runStepService", isAllow); | ||
| 69 | + | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * 获取是否运行计步服务 | ||
| 74 | + * | ||
| 75 | + * @param context | ||
| 76 | + */ | ||
| 77 | + public static boolean getIsRunStepService(Context context) { | ||
| 78 | + return MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).getBoolean("runStepService", false); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /*重置 | ||
| 82 | + * @param context | ||
| 83 | + */ | ||
| 84 | + public static void reset(Context context) { | ||
| 85 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("writeUserStep"); | ||
| 86 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("stepStartTime"); | ||
| 87 | + MySpHelper.getInstance(context, Context.MODE_MULTI_PROCESS).removeKey("runStepService"); | ||
| 88 | + | ||
| 89 | + } | ||
| 90 | +} |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunMainFragmentView.java
| @@ -11,6 +11,7 @@ import android.view.MotionEvent; | @@ -11,6 +11,7 @@ import android.view.MotionEvent; | ||
| 11 | import android.view.View; | 11 | import android.view.View; |
| 12 | import android.widget.Chronometer; | 12 | import android.widget.Chronometer; |
| 13 | 13 | ||
| 14 | +import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | ||
| 14 | import com.cnlive.moudle.pedometer.model.RunningFinishBean; | 15 | import com.cnlive.moudle.pedometer.model.RunningFinishBean; |
| 15 | import com.cnlive.moudle.pedometer.model.RunningMapBean; | 16 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 16 | import com.cnlive.moudle.pedometer.model.Constant; | 17 | import com.cnlive.moudle.pedometer.model.Constant; |
| @@ -66,7 +67,7 @@ public class RunMainFragmentView implements MvpView { | @@ -66,7 +67,7 @@ public class RunMainFragmentView implements MvpView { | ||
| 66 | */ | 67 | */ |
| 67 | public void initView() { | 68 | public void initView() { |
| 68 | uid = "yangshuai"; | 69 | uid = "yangshuai"; |
| 69 | - MySpHelper.getInstance(getContext(), Context.MODE_MULTI_PROCESS).putString("userId", uid); | 70 | + CommonInfo.setUserId(getContext(), uid); |
| 70 | fragment.binding.llRunMainRoot.setVisibility(View.GONE); | 71 | fragment.binding.llRunMainRoot.setVisibility(View.GONE); |
| 71 | ButtonAnimUtil.startAnimation(fragment.binding.llStartTimeTip, false); | 72 | ButtonAnimUtil.startAnimation(fragment.binding.llStartTimeTip, false); |
| 72 | firstTimeFlag = true; | 73 | firstTimeFlag = true; |
| @@ -127,9 +128,10 @@ public class RunMainFragmentView implements MvpView { | @@ -127,9 +128,10 @@ public class RunMainFragmentView implements MvpView { | ||
| 127 | 128 | ||
| 128 | } | 129 | } |
| 129 | }); | 130 | }); |
| 130 | - UserStepEntity userStepEntity = StepUtil.getUserStepData(fragment.getActivity(), uid); | 131 | + UserStepEntity userStepEntity = StepUtil.getUserStepData(getContext(), uid); |
| 132 | + boolean isRunStep = CommonInfo.getIsRunStepService(getContext()); | ||
| 131 | //当前用户已经在运动 | 133 | //当前用户已经在运动 |
| 132 | - if (userStepEntity == null) { | 134 | + if (!isRunStep) { |
| 133 | //依次倒计时 | 135 | //依次倒计时 |
| 134 | handler.postDelayed(runnable, 1000); | 136 | handler.postDelayed(runnable, 1000); |
| 135 | } else { | 137 | } else { |
| @@ -142,12 +144,13 @@ public class RunMainFragmentView implements MvpView { | @@ -142,12 +144,13 @@ public class RunMainFragmentView implements MvpView { | ||
| 142 | //开始计时 | 144 | //开始计时 |
| 143 | if (firstTimeFlag) { | 145 | if (firstTimeFlag) { |
| 144 | firstTimeFlag = false; | 146 | firstTimeFlag = false; |
| 145 | - if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | ||
| 146 | - fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | ||
| 147 | - } else { | ||
| 148 | - fragment.binding.chronometer.setFormat("00:00:01"); | ||
| 149 | - fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | ||
| 150 | - | 147 | + if (userStepEntity != null) { |
| 148 | + if ((userStepEntity.getPauseTimeStamp() != 0) && (userStepEntity.getRecordTime() != 0)) { | ||
| 149 | + fragment.binding.chronometer.setBase(userStepEntity.getPauseTimeStamp() + (SystemClock.elapsedRealtime() - userStepEntity.getRecordTime())); | ||
| 150 | + } else { | ||
| 151 | + fragment.binding.chronometer.setFormat("00:00:01"); | ||
| 152 | + fragment.binding.chronometer.setBase(SystemClock.elapsedRealtime()); | ||
| 153 | + } | ||
| 151 | } | 154 | } |
| 152 | fragment.binding.chronometer.start(); | 155 | fragment.binding.chronometer.start(); |
| 153 | } | 156 | } |
| @@ -249,18 +252,17 @@ public class RunMainFragmentView implements MvpView { | @@ -249,18 +252,17 @@ public class RunMainFragmentView implements MvpView { | ||
| 249 | SelectDialog selectDialog = new SelectDialog(fragment.getActivity(), "确定结束运动吗?", "确定", new SelectDialog.DialogInterface() { | 252 | SelectDialog selectDialog = new SelectDialog(fragment.getActivity(), "确定结束运动吗?", "确定", new SelectDialog.DialogInterface() { |
| 250 | @Override | 253 | @Override |
| 251 | public void onClick(SelectDialog dialog) { | 254 | public void onClick(SelectDialog dialog) { |
| 252 | - MySpHelper.getInstance(getContext(), Context.MODE_MULTI_PROCESS).putBoolean("writeUserStep:" + uid, false); | 255 | + CommonInfo.setIsWriteUserStep(getContext(), false); |
| 253 | fragment.binding.cpCurrent.setVisibility(View.GONE); | 256 | fragment.binding.cpCurrent.setVisibility(View.GONE); |
| 254 | ButtonAnimUtil.setViewDefault(fragment.binding.flStop); | 257 | ButtonAnimUtil.setViewDefault(fragment.binding.flStop); |
| 255 | //清空数据 | 258 | //清空数据 |
| 256 | - StepUtil.clearRunData(getContext(), uid); | 259 | + CommonInfo.reset(getContext()); |
| 257 | dialog.dismiss(); | 260 | dialog.dismiss(); |
| 258 | if (StepService.mClient != null) { | 261 | if (StepService.mClient != null) { |
| 259 | StepService.mClient.stopGather(StepService.traceListener); | 262 | StepService.mClient.stopGather(StepService.traceListener); |
| 260 | } | 263 | } |
| 261 | - MySpHelper.getInstance(getContext(), Context.MODE_MULTI_PROCESS).putLong("stepStartTime", 0); | 264 | + CommonInfo.setStepStartTime(getContext(), 0); |
| 262 | fragment.getActivity().stopService(new Intent(fragment.getActivity(), StepService.class)); | 265 | fragment.getActivity().stopService(new Intent(fragment.getActivity(), StepService.class)); |
| 263 | - MySpHelper.getInstance(getContext(), Context.MODE_MULTI_PROCESS).putString("userId", ""); | ||
| 264 | RunningFinishBean runningFinishBean = new RunningFinishBean(); | 266 | RunningFinishBean runningFinishBean = new RunningFinishBean(); |
| 265 | runningFinishBean.setStartPoint("开始地点"); | 267 | runningFinishBean.setStartPoint("开始地点"); |
| 266 | runningFinishBean.setEndPoint("结束地点"); | 268 | runningFinishBean.setEndPoint("结束地点"); |
| @@ -405,7 +407,7 @@ public class RunMainFragmentView implements MvpView { | @@ -405,7 +407,7 @@ public class RunMainFragmentView implements MvpView { | ||
| 405 | */ | 407 | */ |
| 406 | public void startStepService(Context context, String userId) { | 408 | public void startStepService(Context context, String userId) { |
| 407 | Intent intent = new Intent(fragment.getActivity(), StepService.class); | 409 | Intent intent = new Intent(fragment.getActivity(), StepService.class); |
| 408 | -// intent.putExtra("userId", userId); | 410 | + intent.putExtra("userId", userId); |
| 409 | ContextCompat.startForegroundService(fragment.getActivity(), intent); | 411 | ContextCompat.startForegroundService(fragment.getActivity(), intent); |
| 410 | 412 | ||
| 411 | } | 413 | } |
| @@ -419,7 +421,7 @@ public class RunMainFragmentView implements MvpView { | @@ -419,7 +421,7 @@ public class RunMainFragmentView implements MvpView { | ||
| 419 | //设置行走公里 | 421 | //设置行走公里 |
| 420 | fragment.binding.tvRunDistance.setText(StepUtil.getWalkDistanceKM(step) + ""); | 422 | fragment.binding.tvRunDistance.setText(StepUtil.getWalkDistanceKM(step) + ""); |
| 421 | //设置行走步数 | 423 | //设置行走步数 |
| 422 | - fragment.binding.tvStepCount.setText(step+""); | 424 | + fragment.binding.tvStepCount.setText(step + ""); |
| 423 | //设置S速度 | 425 | //设置S速度 |
| 424 | int second = (int) ((SystemClock.elapsedRealtime() - fragment.binding.chronometer.getBase()) / 1000); | 426 | int second = (int) ((SystemClock.elapsedRealtime() - fragment.binding.chronometer.getBase()) / 1000); |
| 425 | if (second != 0) { | 427 | if (second != 0) { |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/receivers/ServiceReceiver.java
| @@ -8,7 +8,6 @@ import android.os.Build; | @@ -8,7 +8,6 @@ import android.os.Build; | ||
| 8 | import android.os.PowerManager; | 8 | import android.os.PowerManager; |
| 9 | import android.util.Log; | 9 | import android.util.Log; |
| 10 | 10 | ||
| 11 | -import com.cnlive.moudle.pedometer.service.KeepLiveService; | ||
| 12 | import com.cnlive.moudle.pedometer.utils.MySpHelper; | 11 | import com.cnlive.moudle.pedometer.utils.MySpHelper; |
| 13 | import com.cnlive.moudle.pedometer.utils.ServiceUtils; | 12 | import com.cnlive.moudle.pedometer.utils.ServiceUtils; |
| 14 | 13 | ||
| @@ -41,9 +40,9 @@ public class ServiceReceiver extends BroadcastReceiver { | @@ -41,9 +40,9 @@ public class ServiceReceiver extends BroadcastReceiver { | ||
| 41 | if (isRun) { | 40 | if (isRun) { |
| 42 | if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.KeepLiveService")) { | 41 | if (!ServiceUtils.isServiceRunning(context, "com.cnlive.moudle.pedometer.service.KeepLiveService")) { |
| 43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | 42 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 44 | - context.startForegroundService(new Intent(context, KeepLiveService.class)); | 43 | +// context.startForegroundService(new Intent(context, KeepLiveService.class)); |
| 45 | } else { | 44 | } else { |
| 46 | - context.startService(new Intent(context, KeepLiveService.class)); | 45 | +// context.startService(new Intent(context, KeepLiveService.class)); |
| 47 | } | 46 | } |
| 48 | Log.e(TAG, "启动KeepLiveService"); | 47 | Log.e(TAG, "启动KeepLiveService"); |
| 49 | } | 48 | } |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/KeepLiveService.java deleted
100644 → 0
| 1 | -package com.cnlive.moudle.pedometer.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.pedometer.receivers.ServiceReceiver; | ||
| 21 | -import com.cnlive.moudle.pedometer.utils.MySpHelper; | ||
| 22 | -import com.example.moudle_pedometer.R; | ||
| 23 | - | ||
| 24 | - | ||
| 25 | -/** | ||
| 26 | - * @author ShinnyYang | ||
| 27 | - * 维持后台运行的服务 | ||
| 28 | - */ | ||
| 29 | -public class KeepLiveService extends Service implements MediaPlayer.OnCompletionListener { | ||
| 30 | - //唤醒锁 | ||
| 31 | - private PowerManager.WakeLock wakeLock = null; | ||
| 32 | - //电源管理器 | ||
| 33 | - private PowerManager powerManager = null; | ||
| 34 | - //音频播放器 | ||
| 35 | - public static MediaPlayer mMediaPlayer; | ||
| 36 | - //广播 | ||
| 37 | - private static ServiceReceiver myReceiver; | ||
| 38 | - private String CHANNEL_ONE_ID = "com.cnlive.keep"; | ||
| 39 | - private String CHANNEL_ONE_NAME = "KeepLive"; | ||
| 40 | - private NotificationManager mNM; | ||
| 41 | - private Notification notification = null; | ||
| 42 | - //是否在后台运行 | ||
| 43 | - public static boolean isKeepService = true; | ||
| 44 | - public static KeepLiveService instance; | ||
| 45 | - private int startId; | ||
| 46 | - private final String TAG = "KeepLiveService"; | ||
| 47 | - private Handler handler; | ||
| 48 | - private Runnable runnable; | ||
| 49 | - | ||
| 50 | - public KeepLiveService() { | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - public static KeepLiveService getInstance() { | ||
| 54 | - if (instance == null) { | ||
| 55 | - instance = new KeepLiveService(); | ||
| 56 | - return instance; | ||
| 57 | - } else { | ||
| 58 | - return instance; | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - @Override | ||
| 63 | - public void onCreate() { | ||
| 64 | - super.onCreate(); | ||
| 65 | - instance = this; | ||
| 66 | -// if (!ServiceUtils.isServiceRunning(getApplicationContext(), "com.cnlive.moudle.pedometer.service.StepService")) { | ||
| 67 | -// startService(new Intent(getApplicationContext(), StepService.class)); | ||
| 68 | -// } | ||
| 69 | - init(); | ||
| 70 | - initBroadCastReceiver(); | ||
| 71 | - handler = new Handler(); | ||
| 72 | - runnable = new Runnable() { | ||
| 73 | - @Override | ||
| 74 | - public void run() { | ||
| 75 | - //跨进程读取配置文件 | ||
| 76 | -// UserService.getUid(getApplicationContext() | ||
| 77 | - boolean isRun = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getBoolean("keepService", false); | ||
| 78 | - if (isRun) { | ||
| 79 | - handler.postDelayed(this, 5000); | ||
| 80 | - } else { | ||
| 81 | - if (myReceiver != null) { | ||
| 82 | - unregisterReceiver(myReceiver); | ||
| 83 | - } | ||
| 84 | - stopPlaySong(); | ||
| 85 | - stopForeground(true); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - } | ||
| 89 | - }; | ||
| 90 | - | ||
| 91 | - } | ||
| 92 | - | ||
| 93 | - @Override | ||
| 94 | - public void onTaskRemoved(Intent rootIntent) { | ||
| 95 | - super.onTaskRemoved(rootIntent); | ||
| 96 | - Log.e("onTaskRemoved", "onTaskRemoved"); | ||
| 97 | - } | ||
| 98 | - | ||
| 99 | - /** | ||
| 100 | - * 初始化广播 | ||
| 101 | - */ | ||
| 102 | - private void initBroadCastReceiver() { | ||
| 103 | - if (wakeLock != null) { | ||
| 104 | - myReceiver = new ServiceReceiver(wakeLock); | ||
| 105 | - } else { | ||
| 106 | - myReceiver = new ServiceReceiver(); | ||
| 107 | - } | ||
| 108 | - IntentFilter intentFilter = new IntentFilter(); | ||
| 109 | - intentFilter.addAction(Intent.ACTION_SCREEN_OFF); | ||
| 110 | - intentFilter.addAction(Intent.ACTION_SCREEN_ON); | ||
| 111 | -// intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); | ||
| 112 | - intentFilter.addAction(Intent.ACTION_TIME_CHANGED); | ||
| 113 | -// intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); | ||
| 114 | -// intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); | ||
| 115 | - intentFilter.addAction("com.cnlive.strike.receiver.ServiceReceiver"); | ||
| 116 | - intentFilter.addAction("close.KeepService"); | ||
| 117 | - registerReceiver(myReceiver, intentFilter); | ||
| 118 | - } | ||
| 119 | - | ||
| 120 | - /** | ||
| 121 | - * 初始化相关参数 | ||
| 122 | - */ | ||
| 123 | - @SuppressLint("InvalidWakeLockTag") | ||
| 124 | - private void init() { | ||
| 125 | - if (null == powerManager) { | ||
| 126 | - powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); | ||
| 127 | - } | ||
| 128 | - if (null == wakeLock) { | ||
| 129 | - wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "track upload"); | ||
| 130 | - } | ||
| 131 | - if (null == mNM) { | ||
| 132 | - mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - } | ||
| 136 | - | ||
| 137 | - @SuppressLint("WrongConstant") | ||
| 138 | - @Override | ||
| 139 | - public int onStartCommand(Intent intent, int flags, int startId) { | ||
| 140 | - this.startId = startId; | ||
| 141 | - //跨进程读取配置文件 | ||
| 142 | -// UserService.getUid(getApplicationContext() | ||
| 143 | - boolean isRun = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getBoolean("keepService", false); | ||
| 144 | - Log.e("isRun onStartCommand", "" + isRun); | ||
| 145 | - if (isRun) { | ||
| 146 | - if (runnable != null) { | ||
| 147 | - handler.postDelayed(runnable, 5000); | ||
| 148 | - } | ||
| 149 | - showHideForegroundNotification(); | ||
| 150 | - Log.e(TAG, "启动HideKeepTipService"); | ||
| 151 | - startPlaySong(); | ||
| 152 | - } | ||
| 153 | - return START_STICKY; | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | - //开始、暂停播放 | ||
| 157 | - public void startPlaySong() { | ||
| 158 | - if (mMediaPlayer == null) { | ||
| 159 | - mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.no_kill); | ||
| 160 | - } | ||
| 161 | - mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); | ||
| 162 | - mMediaPlayer.setOnCompletionListener(this); | ||
| 163 | -// mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { | ||
| 164 | -// @Override | ||
| 165 | -// public boolean onError(MediaPlayer mediaPlayer, int i, int i1) { | ||
| 166 | -// mMediaPlayer.reset(); | ||
| 167 | -// mMediaPlayer.release(); | ||
| 168 | -// mMediaPlayer = null; | ||
| 169 | -// startPlaySong(); | ||
| 170 | -// return false; | ||
| 171 | -// } | ||
| 172 | -// }); | ||
| 173 | - if (!mMediaPlayer.isPlaying()) { | ||
| 174 | - mMediaPlayer.start(); | ||
| 175 | - Log.e(TAG, "开始播放"); | ||
| 176 | - } | ||
| 177 | - | ||
| 178 | - } | ||
| 179 | - | ||
| 180 | - //停止播放销毁对象 | ||
| 181 | - public void stopPlaySong() { | ||
| 182 | - | ||
| 183 | - if (mMediaPlayer != null) { | ||
| 184 | - mMediaPlayer.stop(); | ||
| 185 | - mMediaPlayer.release(); | ||
| 186 | - mMediaPlayer = null; | ||
| 187 | - Log.e("停止播放", "停止"); | ||
| 188 | - } | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - private void showHideForegroundNotification() { | ||
| 193 | - NotificationChannel channel = null; | ||
| 194 | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| 195 | - channel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, | ||
| 196 | - NotificationManager.IMPORTANCE_HIGH); | ||
| 197 | - channel.enableLights(false); | ||
| 198 | - channel.enableVibration(false); | ||
| 199 | - channel.setVibrationPattern(new long[]{0}); | ||
| 200 | - channel.setSound(null, null); | ||
| 201 | - channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PRIVATE); | ||
| 202 | - mNM.createNotificationChannel(channel); | ||
| 203 | - } | ||
| 204 | - notification = new NotificationCompat.Builder(this, CHANNEL_ONE_ID) | ||
| 205 | - .setWhen(System.currentTimeMillis()) | ||
| 206 | - .setSound(null) | ||
| 207 | - .build(); | ||
| 208 | - // 0 隐藏前台服务通知 | ||
| 209 | - startForeground(100, notification); | ||
| 210 | -// stopForeground(true); | ||
| 211 | - } | ||
| 212 | - | ||
| 213 | - @Override | ||
| 214 | - public void onDestroy() { | ||
| 215 | - super.onDestroy(); | ||
| 216 | - Log.e("onDestroy", "onDestroy:" + notification); | ||
| 217 | - if (myReceiver != null) { | ||
| 218 | - unregisterReceiver(myReceiver); | ||
| 219 | - } | ||
| 220 | - try { | ||
| 221 | -// stopForeground(true); | ||
| 222 | - } catch (Exception e) { | ||
| 223 | - } | ||
| 224 | - stopPlaySong(); | ||
| 225 | - // 重启自己 | ||
| 226 | - if (isKeepService) { | ||
| 227 | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| 228 | - startForegroundService(new Intent(getApplicationContext(), KeepLiveService.class)); | ||
| 229 | - } else { | ||
| 230 | - startService(new Intent(getApplicationContext(), KeepLiveService.class)); | ||
| 231 | - } | ||
| 232 | - //发送重启广播 | ||
| 233 | - sendRestartBroadCast(); | ||
| 234 | - } | ||
| 235 | - | ||
| 236 | - | ||
| 237 | - } | ||
| 238 | - | ||
| 239 | - | ||
| 240 | - /** | ||
| 241 | - * 发送重启广播 | ||
| 242 | - */ | ||
| 243 | - private void sendRestartBroadCast() { | ||
| 244 | - Intent intent = new Intent(" com.cnlive.moudle.pedometer.receivers.MyReceiver"); | ||
| 245 | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| 246 | - intent.setComponent(new ComponentName(getPackageName(), " com.cnlive.moudle.pedometer.receivers.MyReceiver"));//高版本Android发送广播需要加这句 | ||
| 247 | - } | ||
| 248 | - sendBroadcast(intent); | ||
| 249 | - } | ||
| 250 | - | ||
| 251 | - @Override | ||
| 252 | - public IBinder onBind(Intent intent) { | ||
| 253 | - // TODO: Return the communication channel to the service. | ||
| 254 | - throw new UnsupportedOperationException("Not yet implemented"); | ||
| 255 | - } | ||
| 256 | - | ||
| 257 | - | ||
| 258 | - /** | ||
| 259 | - * 歌曲播放完成 | ||
| 260 | - * | ||
| 261 | - * @param mediaPlayer | ||
| 262 | - */ | ||
| 263 | - @Override | ||
| 264 | - public void onCompletion(MediaPlayer mediaPlayer) { | ||
| 265 | -// UserService.getUid(getApplicationContext() | ||
| 266 | - boolean isRun = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getBoolean("keepService", false); | ||
| 267 | - | ||
| 268 | - if (isRun) { | ||
| 269 | - Log.e(TAG, "重新播放"); | ||
| 270 | - mediaPlayer.start(); | ||
| 271 | - Intent intent = new Intent("com.cnlive.strike.receiver.ServiceReceiver"); | ||
| 272 | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| 273 | - intent.setComponent(new ComponentName(getPackageName(), "com.cnlive.strike.receiver.ServiceReceiver"));//高版本Android发送广播需要加这句 | ||
| 274 | - } | ||
| 275 | - sendBroadcast(intent); | ||
| 276 | - } else { | ||
| 277 | - Log.e(TAG, "停止重新播放"); | ||
| 278 | - stopPlaySong(); | ||
| 279 | - } | ||
| 280 | - | ||
| 281 | - | ||
| 282 | - } | ||
| 283 | -} |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/StepService.java
| @@ -11,7 +11,6 @@ import android.content.ComponentName; | @@ -11,7 +11,6 @@ import android.content.ComponentName; | ||
| 11 | import android.content.Context; | 11 | import android.content.Context; |
| 12 | import android.content.Intent; | 12 | import android.content.Intent; |
| 13 | import android.content.IntentFilter; | 13 | import android.content.IntentFilter; |
| 14 | -import android.content.SharedPreferences; | ||
| 15 | import android.hardware.Sensor; | 14 | import android.hardware.Sensor; |
| 16 | import android.hardware.SensorEvent; | 15 | import android.hardware.SensorEvent; |
| 17 | import android.hardware.SensorEventListener; | 16 | import android.hardware.SensorEventListener; |
| @@ -27,19 +26,18 @@ import android.util.Log; | @@ -27,19 +26,18 @@ import android.util.Log; | ||
| 27 | import android.widget.RemoteViews; | 26 | import android.widget.RemoteViews; |
| 28 | import android.widget.Toast; | 27 | import android.widget.Toast; |
| 29 | 28 | ||
| 30 | -import com.baidu.mapapi.model.LatLng; | ||
| 31 | import com.baidu.trace.LBSTraceClient; | 29 | import com.baidu.trace.LBSTraceClient; |
| 32 | import com.baidu.trace.Trace; | 30 | import com.baidu.trace.Trace; |
| 33 | -import com.baidu.trace.api.entity.OnEntityListener; | ||
| 34 | import com.baidu.trace.api.fence.FenceAlarmPushInfo; | 31 | import com.baidu.trace.api.fence.FenceAlarmPushInfo; |
| 35 | import com.baidu.trace.api.fence.MonitoredAction; | 32 | import com.baidu.trace.api.fence.MonitoredAction; |
| 36 | import com.baidu.trace.api.track.LatestPoint; | 33 | import com.baidu.trace.api.track.LatestPoint; |
| 37 | import com.baidu.trace.api.track.LatestPointResponse; | 34 | import com.baidu.trace.api.track.LatestPointResponse; |
| 38 | import com.baidu.trace.api.track.OnTrackListener; | 35 | import com.baidu.trace.api.track.OnTrackListener; |
| 36 | +import com.baidu.trace.model.LocationMode; | ||
| 39 | import com.baidu.trace.model.OnTraceListener; | 37 | import com.baidu.trace.model.OnTraceListener; |
| 40 | import com.baidu.trace.model.PushMessage; | 38 | import com.baidu.trace.model.PushMessage; |
| 41 | import com.baidu.trace.model.StatusCodes; | 39 | import com.baidu.trace.model.StatusCodes; |
| 42 | -import com.baidu.trace.model.TraceLocation; | 40 | +import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; |
| 43 | import com.cnlive.moudle.pedometer.model.Constant; | 41 | import com.cnlive.moudle.pedometer.model.Constant; |
| 44 | import com.cnlive.moudle.pedometer.model.MessageEvent; | 42 | import com.cnlive.moudle.pedometer.model.MessageEvent; |
| 45 | import com.cnlive.moudle.pedometer.model.step.StepCount; | 43 | import com.cnlive.moudle.pedometer.model.step.StepCount; |
| @@ -49,9 +47,7 @@ import com.cnlive.moudle.pedometer.sql.db.DbCore; | @@ -49,9 +47,7 @@ import com.cnlive.moudle.pedometer.sql.db.DbCore; | ||
| 49 | import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; | 47 | import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; |
| 50 | import com.cnlive.moudle.pedometer.ui.activity.PedometerMainActivity; | 48 | import com.cnlive.moudle.pedometer.ui.activity.PedometerMainActivity; |
| 51 | import com.cnlive.moudle.pedometer.utils.MySpHelper; | 49 | import com.cnlive.moudle.pedometer.utils.MySpHelper; |
| 52 | -import com.cnlive.moudle.pedometer.utils.TimeUtil; | ||
| 53 | import com.example.moudle_pedometer.R; | 50 | import com.example.moudle_pedometer.R; |
| 54 | -import com.orhanobut.logger.Logger; | ||
| 55 | 51 | ||
| 56 | import org.greenrobot.eventbus.EventBus; | 52 | import org.greenrobot.eventbus.EventBus; |
| 57 | import org.greenrobot.eventbus.Subscribe; | 53 | import org.greenrobot.eventbus.Subscribe; |
| @@ -180,6 +176,8 @@ public class StepService extends Service implements SensorEventListener { | @@ -180,6 +176,8 @@ public class StepService extends Service implements SensorEventListener { | ||
| 180 | mClient = new LBSTraceClient(getApplicationContext()); | 176 | mClient = new LBSTraceClient(getApplicationContext()); |
| 181 | // 设置定位和打包周期 | 177 | // 设置定位和打包周期 |
| 182 | mClient.setInterval(gatherInterval, packInterval); | 178 | mClient.setInterval(gatherInterval, packInterval); |
| 179 | + //设置定位模式 | ||
| 180 | + mClient.setLocationMode(LocationMode.High_Accuracy); | ||
| 183 | } | 181 | } |
| 184 | if (mTrace == null) { | 182 | if (mTrace == null) { |
| 185 | mTrace = new Trace(serviceId, entityName); | 183 | mTrace = new Trace(serviceId, entityName); |
| @@ -314,6 +312,7 @@ public class StepService extends Service implements SensorEventListener { | @@ -314,6 +312,7 @@ public class StepService extends Service implements SensorEventListener { | ||
| 314 | @Override | 312 | @Override |
| 315 | public void onPushCallback(byte messageType, PushMessage pushMessage) { | 313 | public void onPushCallback(byte messageType, PushMessage pushMessage) { |
| 316 | Log.e(TAG, "onPushCallback: "); | 314 | Log.e(TAG, "onPushCallback: "); |
| 315 | + | ||
| 317 | if (messageType < 0x03 || messageType > 0x04) { | 316 | if (messageType < 0x03 || messageType > 0x04) { |
| 318 | return; | 317 | return; |
| 319 | } | 318 | } |
| @@ -341,9 +340,9 @@ public class StepService extends Service implements SensorEventListener { | @@ -341,9 +340,9 @@ public class StepService extends Service implements SensorEventListener { | ||
| 341 | //开启轨迹服务 | 340 | //开启轨迹服务 |
| 342 | if (mClient != null) { | 341 | if (mClient != null) { |
| 343 | mClient.startTrace(mTrace, traceListener); | 342 | mClient.startTrace(mTrace, traceListener); |
| 344 | - long stepStartTime = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getLong("stepStartTime", 0); | 343 | + long stepStartTime = CommonInfo.getStepStartTime(getApplicationContext()); |
| 345 | if (stepStartTime == 0) { | 344 | if (stepStartTime == 0) { |
| 346 | - MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).putLong("stepStartTime", TimeUtil.getCurrentTime()); | 345 | + CommonInfo.getStepStartTime(getApplicationContext()); |
| 347 | } | 346 | } |
| 348 | } | 347 | } |
| 349 | 348 | ||
| @@ -357,13 +356,16 @@ public class StepService extends Service implements SensorEventListener { | @@ -357,13 +356,16 @@ public class StepService extends Service implements SensorEventListener { | ||
| 357 | @SuppressLint("WrongConstant") | 356 | @SuppressLint("WrongConstant") |
| 358 | @Override | 357 | @Override |
| 359 | public int onStartCommand(Intent intent, int flags, int startId) { | 358 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 360 | -// userId=intent.getExtras().getString("userId"); | ||
| 361 | - userId = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getString("userId", ""); | 359 | + if (intent.getStringExtra("userId") != null) { |
| 360 | + userId = intent.getStringExtra("userId"); | ||
| 361 | + Log.e(TAG, "onStartCommand: 传入的用户:" + userId); | ||
| 362 | + } | ||
| 363 | +// userId = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getString("userId", ""); | ||
| 362 | if (userId != null && !TextUtils.isEmpty(userId)) { | 364 | if (userId != null && !TextUtils.isEmpty(userId)) { |
| 363 | if (!EventBus.getDefault().isRegistered(this)) { | 365 | if (!EventBus.getDefault().isRegistered(this)) { |
| 364 | EventBus.getDefault().register(this); | 366 | EventBus.getDefault().register(this); |
| 365 | } | 367 | } |
| 366 | - MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).putBoolean("writeUserStep:" + userId, true); | 368 | + CommonInfo.setIsWriteUserStep(getApplicationContext(), true); |
| 367 | entityName = userId; | 369 | entityName = userId; |
| 368 | initTodayData(userId); | 370 | initTodayData(userId); |
| 369 | initTrace(); | 371 | initTrace(); |
| @@ -397,6 +399,7 @@ public class StepService extends Service implements SensorEventListener { | @@ -397,6 +399,7 @@ public class StepService extends Service implements SensorEventListener { | ||
| 397 | } | 399 | } |
| 398 | 400 | ||
| 399 | private void init() { | 401 | private void init() { |
| 402 | + CommonInfo.setIsRunStepService(getApplicationContext(), true); | ||
| 400 | if (null == mNM) { | 403 | if (null == mNM) { |
| 401 | mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | 404 | mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 402 | } | 405 | } |
| @@ -896,7 +899,7 @@ public class StepService extends Service implements SensorEventListener { | @@ -896,7 +899,7 @@ public class StepService extends Service implements SensorEventListener { | ||
| 896 | * 保存记步数据 | 899 | * 保存记步数据 |
| 897 | */ | 900 | */ |
| 898 | private void save() { | 901 | private void save() { |
| 899 | - boolean isWriteUserStep = MySpHelper.getInstance(getApplicationContext(), Context.MODE_MULTI_PROCESS).getBoolean("writeUserStep:" + userId, false); | 902 | + boolean isWriteUserStep = CommonInfo.getIsWriteUserStep(getApplicationContext()); |
| 900 | if (!isWriteUserStep) { | 903 | if (!isWriteUserStep) { |
| 901 | return; | 904 | return; |
| 902 | } | 905 | } |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/activity/RunRaceDetailActivity.java
| @@ -15,6 +15,7 @@ import com.baidu.mapapi.SDKInitializer; | @@ -15,6 +15,7 @@ import com.baidu.mapapi.SDKInitializer; | ||
| 15 | import com.cnlive.libs.base.ui.QMUIFragment; | 15 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 16 | import com.cnlive.libs.base.ui.QMUIFragmentActivity; | 16 | import com.cnlive.libs.base.ui.QMUIFragmentActivity; |
| 17 | import com.cnlive.libs.base.util.StatusBarUtil2; | 17 | import com.cnlive.libs.base.util.StatusBarUtil2; |
| 18 | +import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | ||
| 18 | import com.cnlive.moudle.pedometer.model.RunningMapBean; | 19 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 19 | import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; | 20 | import com.cnlive.moudle.pedometer.sql.entity.UserStepEntity; |
| 20 | import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; | 21 | import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; |
| @@ -45,10 +46,9 @@ public class RunRaceDetailActivity extends QMUIFragmentActivity { | @@ -45,10 +46,9 @@ public class RunRaceDetailActivity extends QMUIFragmentActivity { | ||
| 45 | super.onCreate(savedInstanceState); | 46 | super.onCreate(savedInstanceState); |
| 46 | StatusBarUtil2.setColor(this, getResources().getColor(R.color.run_main_bg), 0); | 47 | StatusBarUtil2.setColor(this, getResources().getColor(R.color.run_main_bg), 0); |
| 47 | //判断用户是否结束运动 | 48 | //判断用户是否结束运动 |
| 48 | - String uid = "yangshuai"; | ||
| 49 | - UserStepEntity userStepEntity = StepUtil.getUserStepData(getApplicationContext(), uid); | ||
| 50 | - //没有在数据库中找到相关记录 | ||
| 51 | - if (userStepEntity == null) { | 49 | + boolean isRunStep = CommonInfo.getIsRunStepService(this); |
| 50 | + //没有找到相关记录 | ||
| 51 | + if (!isRunStep) { | ||
| 52 | addFragment(new RunRaceDetailFragment()); | 52 | addFragment(new RunRaceDetailFragment()); |
| 53 | } | 53 | } |
| 54 | //找到记录 | 54 | //找到记录 |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/fragment/RunningMapFragment.java
| @@ -16,6 +16,7 @@ import android.view.View; | @@ -16,6 +16,7 @@ import android.view.View; | ||
| 16 | import com.baidu.mapapi.model.LatLng; | 16 | import com.baidu.mapapi.model.LatLng; |
| 17 | import com.cnlive.libs.base.ui.QMUIFragment; | 17 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 18 | import com.cnlive.libs.base.util.StatusBarUtil2; | 18 | import com.cnlive.libs.base.util.StatusBarUtil2; |
| 19 | +import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; | ||
| 19 | import com.cnlive.moudle.pedometer.frame.presenter.RunningMapFragmentPresenter; | 20 | import com.cnlive.moudle.pedometer.frame.presenter.RunningMapFragmentPresenter; |
| 20 | import com.cnlive.moudle.pedometer.frame.view.RunningMapFragmentView; | 21 | import com.cnlive.moudle.pedometer.frame.view.RunningMapFragmentView; |
| 21 | import com.cnlive.moudle.pedometer.model.RunningMapBean; | 22 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| @@ -107,7 +108,7 @@ public class RunningMapFragment extends QMUIFragment<RunningMapFragmentView, Run | @@ -107,7 +108,7 @@ public class RunningMapFragment extends QMUIFragment<RunningMapFragmentView, Run | ||
| 107 | } | 108 | } |
| 108 | //开始进行历史轨迹查询 | 109 | //开始进行历史轨迹查询 |
| 109 | if (getPresenter() != null && !TextUtils.isEmpty(StepService.entityName)) { | 110 | if (getPresenter() != null && !TextUtils.isEmpty(StepService.entityName)) { |
| 110 | - long stepStartTime = MySpHelper.getInstance(getActivity(), Context.MODE_MULTI_PROCESS).getLong("stepStartTime", 0); | 111 | + long stepStartTime = CommonInfo.getStepStartTime(getActivity()); |
| 111 | long startTime = 0; | 112 | long startTime = 0; |
| 112 | if (stepStartTime != 0) { | 113 | if (stepStartTime != 0) { |
| 113 | startTime = stepStartTime; | 114 | startTime = stepStartTime; |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/utils/MySpHelper.java
| @@ -140,4 +140,14 @@ public class MySpHelper { | @@ -140,4 +140,14 @@ public class MySpHelper { | ||
| 140 | private SharedPreferences getSP(String name) { | 140 | private SharedPreferences getSP(String name) { |
| 141 | return context.getSharedPreferences(name, SP_MODE); | 141 | return context.getSharedPreferences(name, SP_MODE); |
| 142 | } | 142 | } |
| 143 | + | ||
| 144 | + public void removeKey(String key) { | ||
| 145 | + try { | ||
| 146 | + SharedPreferences.Editor editor = getSP().edit(); | ||
| 147 | + editor.remove(key); | ||
| 148 | + editor.commit(); | ||
| 149 | + } catch (NullPointerException e) { | ||
| 150 | + Log.d("hcj", "" + e); | ||
| 151 | + } | ||
| 152 | + } | ||
| 143 | } | 153 | } |
moudle_pedometer/src/main/res/drawable-hdpi/tc_bg_body_record_add_menu.9.png
0 → 100644
1.03 KB