Selaa lähdekoodia

添加登录同步失败后服务开启数据同步

Xieshengqi 6 vuotta sitten
vanhempi
commit
6b4ee49fc3

+ 5 - 0
app/src/main/AndroidManifest.xml

@@ -193,6 +193,11 @@
193 193
 
194 194
 
195 195
         <receiver android:name=".receiver.UpDataReceiver"/>
196
+        <!--下载数据服务-->
197
+        <service android:name=".servier.DownLoadService"/>
198
+
199
+
200
+        <receiver android:name=".receiver.DownLoadReceiver"/>
196 201
     </application>
197 202
 
198 203
 

+ 17 - 0
app/src/main/java/com/kuxuan/moneynote/receiver/DownLoadReceiver.java

@@ -0,0 +1,17 @@
1
+package com.kuxuan.moneynote.receiver;
2
+
3
+import android.content.BroadcastReceiver;
4
+import android.content.Context;
5
+import android.content.Intent;
6
+
7
+/**
8
+ * Created by xieshengqi on 2018/4/9.
9
+ */
10
+
11
+public class DownLoadReceiver  extends BroadcastReceiver {
12
+    @Override
13
+    public void onReceive(Context context, Intent intent) {
14
+        Intent i = new Intent(context, com.kuxuan.moneynote.servier.DownLoadService.class);
15
+        context.startService(i);
16
+    }
17
+}

+ 149 - 0
app/src/main/java/com/kuxuan/moneynote/servier/DownLoadService.java

@@ -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
+

+ 10 - 2
app/src/main/java/com/kuxuan/moneynote/servier/ServiceUtil.java

@@ -12,7 +12,7 @@ public class ServiceUtil {
12 12
 
13 13
 
14 14
     /**
15
-     * 开启服务
15
+     * 开启上传服务
16 16
      *
17 17
      * @param context
18 18
      */
@@ -20,7 +20,15 @@ public class ServiceUtil {
20 20
         Intent intent = new Intent(context, UpDataService.class);
21 21
         context.startService(intent);
22 22
     }
23
-
23
+    /**
24
+     * 开启下载服务
25
+     *
26
+     * @param context
27
+     */
28
+    public static void startDownLoadData(Context context) {
29
+        Intent intent = new Intent(context, DownLoadService.class);
30
+        context.startService(intent);
31
+    }
24 32
 
25 33
     /**
26 34
      * 关闭服务

+ 2 - 2
app/src/main/java/com/kuxuan/moneynote/ui/activitys/login/LoginActivity.java

@@ -30,7 +30,6 @@ import com.kuxuan.moneynote.ui.activitys.MainActivity;
30 30
 import com.kuxuan.moneynote.ui.activitys.eventbus.LoginEvent;
31 31
 import com.kuxuan.moneynote.ui.activitys.register.RegisterFirstActivity;
32 32
 import com.kuxuan.moneynote.ui.weight.ActionSheetDialog;
33
-import com.kuxuan.moneynote.ui.weight.MyToast;
34 33
 import com.kuxuan.moneynote.utils.AppManager;
35 34
 import com.kuxuan.moneynote.utils.GlideRoundTransform;
36 35
 import com.kuxuan.moneynote.utils.LoginStatusUtil;
@@ -297,8 +296,9 @@ public class LoginActivity extends BaseFragmentActivity {
297 296
             @Override
298 297
             public void onError(ExceptionHandle.ResponeThrowable e) {
299 298
                 closeProgressDialog();
299
+                ServiceUtil.startDownLoadData(LoginActivity.this);
300 300
                 goToNext();
301
-                MyToast.makeText(LoginActivity.this, "数据同步失败", Toast.LENGTH_SHORT);
301
+//                MyToast.makeText(LoginActivity.this, "数据同步失败", Toast.LENGTH_SHORT);
302 302
             }
303 303
 
304 304
             @Override

+ 2 - 0
app/src/main/java/com/kuxuan/moneynote/ui/activitys/login/PhoneLoginActivity.java

@@ -379,6 +379,7 @@ public class PhoneLoginActivity extends BaseActivity {
379 379
                     LoginStatusUtil.setToken(loginJsonBaseJson.getData().getToken(), loginJsonBaseJson.getData().getId());
380 380
                     SPUtil.putAndApply(PhoneLoginActivity.this, Constant.IsFirstWEiChatLogin.ISWEICHATLOGIN, false);
381 381
                     syncData(0);
382
+                    goMain();
382 383
                 } else {
383 384
                     closeProgressDialog();
384 385
                     ToastUtil.show(PhoneLoginActivity.this, loginJsonBaseJson.getError().get(0));
@@ -412,6 +413,7 @@ public class PhoneLoginActivity extends BaseActivity {
412 413
             @Override
413 414
             public void onError(ExceptionHandle.ResponeThrowable e) {
414 415
                 closeProgressDialog();
416
+                ServiceUtil.startDownLoadData(PhoneLoginActivity.this);
415 417
                 goMain();
416 418
                 MyToast.makeText(PhoneLoginActivity.this, "数据同步失败", Toast.LENGTH_SHORT);
417 419
             }

+ 2 - 2
app/src/main/java/com/kuxuan/moneynote/ui/fragments/details/DetialPresent.java

@@ -337,8 +337,8 @@ public class DetialPresent extends DetialContract.DetialPresent implements View.
337 337
                     BillData billData = bill_data.get(i);
338 338
                     List<TypeDataJson> day_data = billData.getDay_data();
339 339
                     if (day_data != null) {
340
-                        for (int j = 0; j < day_data.size(); j++) {
341
-                            if (j == 0) {
340
+                        for (int j = day_data.size()-1; j>=0; j--) {
341
+                            if (j == day_data.size()-1) {
342 342
                                 day_data.get(j).setFirst(true);
343 343
                             }
344 344
                             day_data.get(j).setDay_type(billData.getTime());