Skip to content

Commit 552fee2

Browse files
committed
添加后台任务停止功能
1 parent e78fdb2 commit 552fee2

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

app/src/main/java/org/lzh/framework/updateplugin/SampleActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class SampleActivity extends Activity {
3838
@BindView(R.id.create_new_config) CheckedView newConfig; // 选择使用的apk下载网络任务
3939
boolean isPermissionGrant;// 程序是否被允许持有写入权限
4040
ToastCallback callback;
41+
UpdateBuilder daemonTask;
4142
@Override
4243
protected void onCreate(Bundle savedInstanceState) {
4344
super.onCreate(savedInstanceState);
@@ -61,14 +62,23 @@ public void call(Boolean aBoolean) {
6162

6263
@OnClick(R.id.start_daemon_update)
6364
void onDaemonStartClick() {
64-
createBuilder().checkWithDaemon(1000 * 5);// 后台更新时间间隔设置为5秒。
65+
daemonTask = createBuilder();
66+
daemonTask.checkWithDaemon(5);// 后台更新时间间隔设置为5秒。
6567
}
6668

6769
@OnClick(R.id.start_update)
6870
void onStartClick () {
6971
createBuilder().check();
7072
}
7173

74+
@OnClick(R.id.stop_daemon_update)
75+
void onStopDaemonClick() {
76+
if (daemonTask != null) {
77+
daemonTask.shutdown();
78+
daemonTask = null;
79+
}
80+
}
81+
7282
@NonNull
7383
private UpdateBuilder createBuilder() {
7484
UpdateBuilder builder = UpdateBuilder.create();

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@
7171
android:text="@string/start_daemon_update"
7272
android:layout_width="match_parent"
7373
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" />
7479

7580
</LinearLayout>

updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/UpdateBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,26 @@ public final UpdateConfig getConfig() {
289289
return config;
290290
}
291291

292+
/**
293+
* 判断当前所运行的任务是否是后台更新任务
294+
* @return True表示当前以后台更新模式执行
295+
*/
292296
public boolean isDaemon() {
293297
return isDaemon;
294298
}
295299

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+
296312
RetryCallback getRetryCallback() {
297313
if (retryCallback == null) {
298314
retryCallback = new RetryCallback(this);

updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/flow/RetryCallback.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public void setRetryTime(long retryTime) {
2626
this.retryTime = Math.max(1, retryTime);
2727
}
2828

29+
public void detach() {
30+
this.builder = null;
31+
}
32+
2933
@Override
3034
public void onDownloadStart() {
3135

@@ -76,13 +80,18 @@ public void onCheckIgnore(Update update) {
7680
}
7781

7882
private synchronized void retry() {
83+
if (builder == null) {
84+
return;
85+
}
7986
Utils.getMainHandler().removeCallbacks(this);
8087
Utils.getMainHandler().postDelayed(this, retryTime * 1000);
8188
}
8289

8390
@Override
8491
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+
}
8796
}
8897
}

0 commit comments

Comments
 (0)