doum
2 天以前 6c12dd77bc481aeabec568bfed3dd68e81b80f8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.doumee.keyCabinet.ui.service;
 
 
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
 
import androidx.annotation.Nullable;
 
import com.doumee.keyCabinet.MApplication;
import com.doumee.keyCabinet.bean.DevConfigBean;
import com.doumee.keyCabinet.event.DevConfigEvent;
import com.doumee.keyCabinet.event.HeartEvent;
import com.doumee.keyCabinet.http.Apis;
import com.doumee.keyCabinet.http.param.BaseResponse;
import com.doumee.keyCabinet.http.param.DevLoginParam;
import com.doumee.keyCabinet.http.param.RequestBaseObject;
import com.doumee.keyCabinet.utils.LMobileInfo;
import com.doumee.keyCabinet.utils.OkHttpClientUtil;
import com.doumee.lib_coremodel.http.utils.GsonTools;
import com.doumee.lib_coremodel.util.SpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuyh.library.imgsel.utils.LogUtils;
 
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
 
import java.util.HashMap;
import java.util.Map;
 
import timber.log.Timber;
 
public class HeartbeatService extends Service {
    private CountDownTimer countTimer;
 
    @Override
    public void onCreate() {
        //Log.i("Kathy","onCreate - Thread ID = " + Thread.currentThread().getId());
        super.onCreate();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(countTimer==null){
            countTimer = new CountDownTimer(300000, 300000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    //Timber.tag("CountTimer==>").d(millisUntilFinished+"");
                }
 
                @Override
                public void onFinish() {
                    Timber.tag("CountTimer==>").d("心跳 onFinish");
                    try {
                        sendPost();
                    } catch (JSONException e) {
 
                    }
                }
            };
        }
        try {
            sendPost();
        } catch (JSONException e) {
 
        }
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //Log.i("Kathy", "onBind - Thread ID = " + Thread.currentThread().getId());
        return null;
    }
 
    @Override
    public void onDestroy() {
        //Log.i("Kathy", "onDestroy - Thread ID = " + Thread.currentThread().getId());
        super.onDestroy();
        if(countTimer!=null){
            countTimer.cancel();
            countTimer = null;
        }
    }
 
    private void sendPost() throws JSONException {
        EventBus.getDefault().post(new HeartEvent());
    }
}