|
@@ -0,0 +1,149 @@
|
|
1
|
+package com.kuxuan.moneynote.servier;
|
|
2
|
+
|
|
3
|
+import android.app.AlarmManager;
|
|
4
|
+import android.app.PendingIntent;
|
|
5
|
+import android.app.Service;
|
|
6
|
+import android.content.Intent;
|
|
7
|
+import android.os.IBinder;
|
|
8
|
+import android.os.SystemClock;
|
|
9
|
+import android.support.annotation.Nullable;
|
|
10
|
+
|
|
11
|
+import com.kuxuan.moneynote.api.ExceptionHandle;
|
|
12
|
+import com.kuxuan.moneynote.api.MyObsever;
|
|
13
|
+import com.kuxuan.moneynote.api.RetrofitClient;
|
|
14
|
+import com.kuxuan.moneynote.db.BillCategoreDaoOperator;
|
|
15
|
+import com.kuxuan.moneynote.db.CategoryDBbean;
|
|
16
|
+import com.kuxuan.moneynote.db.CategoryDaoOperator;
|
|
17
|
+import com.kuxuan.moneynote.json.BeanNewJson;
|
|
18
|
+import com.kuxuan.moneynote.json.UploadBeanJson;
|
|
19
|
+import com.kuxuan.moneynote.json.UploadDbjson;
|
|
20
|
+import com.kuxuan.moneynote.receiver.DownLoadReceiver;
|
|
21
|
+import com.kuxuan.moneynote.ui.activitys.eventbus.RefreshEvent;
|
|
22
|
+import com.kuxuan.moneynote.utils.LoginStatusUtil;
|
|
23
|
+import com.kuxuan.moneynote.utils.TimeUtlis;
|
|
24
|
+
|
|
25
|
+import org.greenrobot.eventbus.EventBus;
|
|
26
|
+
|
|
27
|
+import java.util.ArrayList;
|
|
28
|
+
|
|
29
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
30
|
+import io.reactivex.schedulers.Schedulers;
|
|
31
|
+
|
|
32
|
+/**
|
|
33
|
+ * Created by xieshengqi on 2018/4/9.
|
|
34
|
+ */
|
|
35
|
+
|
|
36
|
+public class DownLoadService extends Service {
|
|
37
|
+
|
|
38
|
+ /**
|
|
39
|
+ * 每3分钟更新一次数据
|
|
40
|
+ */
|
|
41
|
+ private static final int ONE_Miniute = 30 * 1000;
|
|
42
|
+ private static final int PENDING_REQUEST = 0;
|
|
43
|
+ AlarmManager alarmManager;
|
|
44
|
+ PendingIntent pIntent;
|
|
45
|
+
|
|
46
|
+ public DownLoadService() {
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @Override
|
|
50
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
51
|
+ //这里模拟后台操作
|
|
52
|
+//上传数据
|
|
53
|
+ if (LoginStatusUtil.isLoginin()) {
|
|
54
|
+ syncData(0);
|
|
55
|
+
|
|
56
|
+// Log.e("UpDataService", "我在执行");
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+ return super.onStartCommand(intent, flags, startId);
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+ @Override
|
|
64
|
+ public void onDestroy() {
|
|
65
|
+ if(alarmManager!=null)
|
|
66
|
+ alarmManager.cancel(pIntent);//关闭的服务的时候同时关闭广播注册者
|
|
67
|
+ super.onDestroy();
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ @Nullable
|
|
71
|
+ @Override
|
|
72
|
+ public IBinder onBind(Intent intent) {
|
|
73
|
+ return null;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ /**
|
|
77
|
+ * 同步数据
|
|
78
|
+ */
|
|
79
|
+ private void syncData(final int page) {
|
|
80
|
+ RetrofitClient.getApiService().getDownLoadData(page).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MyObsever<BeanNewJson<UploadBeanJson>>() {
|
|
81
|
+ @Override
|
|
82
|
+ public void onError(ExceptionHandle.ResponeThrowable e) {
|
|
83
|
+ alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
|
|
84
|
+ long triggerAtTime = SystemClock.elapsedRealtime() + ONE_Miniute;//从开机到现在的毫秒书(手机睡眠(sleep)的时间也包括在内
|
|
85
|
+ Intent i = new Intent(DownLoadService.this, DownLoadReceiver.class);
|
|
86
|
+ pIntent = PendingIntent.getBroadcast(DownLoadService.this, PENDING_REQUEST, i, PENDING_REQUEST);
|
|
87
|
+ alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pIntent);
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ @Override
|
|
91
|
+ public void onSuccess(BeanNewJson<UploadBeanJson> objectBaseJson) {
|
|
92
|
+ if (objectBaseJson != null && objectBaseJson.getCode() == 0) {
|
|
93
|
+ UploadBeanJson res = objectBaseJson.getRes();
|
|
94
|
+ if (res != null) {
|
|
95
|
+ int number = res.getNumber();
|
|
96
|
+ if (res.getData() != null) {
|
|
97
|
+ insertDb(objectBaseJson.getRes().getData());
|
|
98
|
+ if (res.getData().size() < number) {
|
|
99
|
+ //结束
|
|
100
|
+ EventBus.getDefault().post(new RefreshEvent());
|
|
101
|
+ //结束服务
|
|
102
|
+ stopService(new Intent(DownLoadService.this, DownLoadService.class));
|
|
103
|
+ } else {
|
|
104
|
+ int count = page;
|
|
105
|
+ syncData(count++);
|
|
106
|
+ }
|
|
107
|
+ } else {
|
|
108
|
+
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ } else {
|
|
112
|
+
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ }
|
|
117
|
+ });
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ private void insertDb(ArrayList<UploadDbjson> uploadDbjsons) {
|
|
121
|
+ if (uploadDbjsons == null) {
|
|
122
|
+ return;
|
|
123
|
+ }
|
|
124
|
+ CategoryDaoOperator categoryDaoOperator = CategoryDaoOperator.newInstance();
|
|
125
|
+ int loginUserId = LoginStatusUtil.getLoginUserId();
|
|
126
|
+ for (UploadDbjson uploadDbjson : uploadDbjsons) {
|
|
127
|
+ CategoryDBbean categoryDBbean = new CategoryDBbean();
|
|
128
|
+ categoryDBbean.setName(uploadDbjson.getCategory_name());
|
|
129
|
+ categoryDBbean.setType_imagepath(BillCategoreDaoOperator.newInstance().getDetaillIconUrlById(uploadDbjson.getCategory_id()));
|
|
130
|
+ categoryDBbean.setType(uploadDbjson.getType());
|
|
131
|
+ categoryDBbean.setCategory_id(uploadDbjson.getCategory_id());
|
|
132
|
+ categoryDBbean.setDemo(uploadDbjson.getDemo());
|
|
133
|
+ long time = uploadDbjson.getTime() * 1000;
|
|
134
|
+ categoryDBbean.setCreateTime(uploadDbjson.getTime());
|
|
135
|
+ categoryDBbean.setUpdateTime(uploadDbjson.getTime());
|
|
136
|
+ String data = TimeUtlis.getData(time);
|
|
137
|
+ String[] split = data.split("-");
|
|
138
|
+ categoryDBbean.setYear(Integer.parseInt(split[0]));
|
|
139
|
+ categoryDBbean.setMonth(Integer.parseInt(split[1]));
|
|
140
|
+ categoryDBbean.setDay(Integer.parseInt(split[2]));
|
|
141
|
+ categoryDBbean.setUser_id(loginUserId);
|
|
142
|
+ categoryDBbean.setAccount(Double.parseDouble(uploadDbjson.getAccount()));
|
|
143
|
+ categoryDBbean.setBill_id(uploadDbjson.getIdentification());
|
|
144
|
+ categoryDaoOperator.insert(categoryDBbean, true);
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ }
|
|
148
|
+}
|
|
149
|
+
|