File tree Expand file tree Collapse file tree 4 files changed +43
-3
lines changed
java/org/lzh/framework/updateplugin
updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib Expand file tree Collapse file tree 4 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ public class SampleActivity extends Activity {
38
38
@ BindView (R .id .create_new_config ) CheckedView newConfig ; // 选择使用的apk下载网络任务
39
39
boolean isPermissionGrant ;// 程序是否被允许持有写入权限
40
40
ToastCallback callback ;
41
+ UpdateBuilder daemonTask ;
41
42
@ Override
42
43
protected void onCreate (Bundle savedInstanceState ) {
43
44
super .onCreate (savedInstanceState );
@@ -61,14 +62,23 @@ public void call(Boolean aBoolean) {
61
62
62
63
@ OnClick (R .id .start_daemon_update )
63
64
void onDaemonStartClick () {
64
- createBuilder ().checkWithDaemon (1000 * 5 );// 后台更新时间间隔设置为5秒。
65
+ daemonTask = createBuilder ();
66
+ daemonTask .checkWithDaemon (5 );// 后台更新时间间隔设置为5秒。
65
67
}
66
68
67
69
@ OnClick (R .id .start_update )
68
70
void onStartClick () {
69
71
createBuilder ().check ();
70
72
}
71
73
74
+ @ OnClick (R .id .stop_daemon_update )
75
+ void onStopDaemonClick () {
76
+ if (daemonTask != null ) {
77
+ daemonTask .shutdown ();
78
+ daemonTask = null ;
79
+ }
80
+ }
81
+
72
82
@ NonNull
73
83
private UpdateBuilder createBuilder () {
74
84
UpdateBuilder builder = UpdateBuilder .create ();
Original file line number Diff line number Diff line change 71
71
android : text =" @string/start_daemon_update"
72
72
android : layout_width =" match_parent"
73
73
android : layout_height =" wrap_content" />
74
+ <Button
75
+ android : id =" @+id/stop_daemon_update"
76
+ android : text =" 停止后台任务"
77
+ android : layout_width =" match_parent"
78
+ android : layout_height =" wrap_content" />
74
79
75
80
</LinearLayout >
Original file line number Diff line number Diff line change @@ -289,10 +289,26 @@ public final UpdateConfig getConfig() {
289
289
return config ;
290
290
}
291
291
292
+ /**
293
+ * 判断当前所运行的任务是否是后台更新任务
294
+ * @return True表示当前以后台更新模式执行
295
+ */
292
296
public boolean isDaemon () {
293
297
return isDaemon ;
294
298
}
295
299
300
+ /**
301
+ * 停止后台任务:此方法只在当前为后台任务,且后台更新任务正在执行时启用。
302
+ *
303
+ * <p>请注意此方法并不会让当前的更新任务停止,而是停止更新失败后的自动重启功能。</p>
304
+ */
305
+ public void shutdown () {
306
+ if (isDaemon && retryCallback != null ) {
307
+ retryCallback .detach ();
308
+ retryCallback = null ;
309
+ }
310
+ }
311
+
296
312
RetryCallback getRetryCallback () {
297
313
if (retryCallback == null ) {
298
314
retryCallback = new RetryCallback (this );
Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ public void setRetryTime(long retryTime) {
26
26
this .retryTime = Math .max (1 , retryTime );
27
27
}
28
28
29
+ public void detach () {
30
+ this .builder = null ;
31
+ }
32
+
29
33
@ Override
30
34
public void onDownloadStart () {
31
35
@@ -76,13 +80,18 @@ public void onCheckIgnore(Update update) {
76
80
}
77
81
78
82
private synchronized void retry () {
83
+ if (builder == null ) {
84
+ return ;
85
+ }
79
86
Utils .getMainHandler ().removeCallbacks (this );
80
87
Utils .getMainHandler ().postDelayed (this , retryTime * 1000 );
81
88
}
82
89
83
90
@ Override
84
91
public void run () {
85
- L .d ("Restart update for daemon" );
86
- builder .checkWithDaemon (retryTime );
92
+ if (builder != null ) {
93
+ L .d ("Restart update for daemon" );
94
+ builder .checkWithDaemon (retryTime );
95
+ }
87
96
}
88
97
}
You can’t perform that action at this time.
0 commit comments