|
|
@@ -1,6 +1,7 @@
|
|
|
package basepedo.service;
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
+import android.app.ActivityManager;
|
|
|
import android.app.Notification;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
|
@@ -25,7 +26,10 @@ import android.os.RemoteException;
|
|
|
import android.support.v7.app.NotificationCompat;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import com.xzjmyk.pm.activity.MyApplication;
|
|
|
import com.xzjmyk.pm.activity.R;
|
|
|
+import com.xzjmyk.pm.activity.ui.MainActivity;
|
|
|
+import com.xzjmyk.pm.activity.util.PreferenceUtils;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
|
@@ -59,7 +63,7 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
//测试
|
|
|
private static int i = 0;
|
|
|
private String DB_NAME = "basepedo";
|
|
|
-
|
|
|
+ private String uustep_service_name = "basepedo.service.StepService";
|
|
|
private static class MessenerHandler extends Handler {
|
|
|
@Override
|
|
|
public void handleMessage(Message msg) {
|
|
|
@@ -102,9 +106,9 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
initTodayData();
|
|
|
-
|
|
|
updateNotification("今日步数:" + StepDcretor.CURRENT_SETP + " 步");
|
|
|
- return START_STICKY;
|
|
|
+ flags = START_STICKY;
|
|
|
+ return super.onStartCommand(intent,flags,startId);
|
|
|
}
|
|
|
private String getTodayDate() {
|
|
|
Date date = new Date(System.currentTimeMillis());
|
|
|
@@ -192,7 +196,7 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
private void updateNotification(String content) {
|
|
|
builder = new NotificationCompat.Builder(this);
|
|
|
builder.setPriority(Notification.PRIORITY_MIN);
|
|
|
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
|
|
|
+ PendingIntent contentIntent = PendingIntent.getActivity(this, 100,
|
|
|
new Intent(this, MyPedometerActivity.class), 0);
|
|
|
builder.setContentIntent(contentIntent);
|
|
|
builder.setSmallIcon(R.drawable.uuu);
|
|
|
@@ -205,7 +209,12 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
|
|
|
startForeground(0, notification);
|
|
|
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
- nm.notify(R.string.app_name, notification);
|
|
|
+ if (PreferenceUtils.getInt(this,MyPedometerActivity.UU_STEP_NOTICE) == 0){
|
|
|
+ nm.notify(R.string.app_name, notification);
|
|
|
+ }else if (PreferenceUtils.getInt(this,MyPedometerActivity.UU_STEP_NOTICE) == 1){
|
|
|
+ nm.cancelAll();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -320,8 +329,12 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
stopForeground(true);
|
|
|
DbUtils.closeDb();
|
|
|
unregisterReceiver(mBatInfoReceiver);
|
|
|
- Intent intent = new Intent(this, StepService.class);
|
|
|
- startService(intent);
|
|
|
+ if (! isServiceRunning(uustep_service_name) && PreferenceUtils.getInt(MyApplication.getInstance(), MainActivity.UU_STEP)==1){
|
|
|
+ Intent intent = new Intent(this, StepService.class);
|
|
|
+ startService(intent);
|
|
|
+ }
|
|
|
+ Intent intent = new Intent("uu.step.destory");
|
|
|
+ sendBroadcast(intent);
|
|
|
super.onDestroy();
|
|
|
}
|
|
|
|
|
|
@@ -363,4 +376,21 @@ public class StepService extends Service implements SensorEventListener {
|
|
|
}
|
|
|
return (mWakeLock);
|
|
|
}
|
|
|
+ private boolean isServiceRunning(String servicename) { //判断UU运动服务是否已经运行
|
|
|
+ ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
|
|
|
+ for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
|
|
+ if (servicename.equals(service.service.getClassName())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ private BroadcastReceiver updateReceiver = new BroadcastReceiver(){
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ if (! isServiceRunning(uustep_service_name) && PreferenceUtils.getInt(MyApplication.getInstance(), MainActivity.UU_STEP) == 1){
|
|
|
+ startService(new Intent(context,StepService.class));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|