-
Notifications
You must be signed in to change notification settings - Fork 100
/
MyAsyncTask.java
56 lines (53 loc) · 1.18 KB
/
MyAsyncTask.java
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
package com.itheima.mobilesafe.utils;
import android.os.Handler;
import android.os.Message;
/**
* 模板设计模式
* @author yanbinadmin
*
*/
public abstract class MyAsyncTask {
protected static final int DO_PRE_EXECUTE = 0;
protected static final int DO_Post_EXECUTE = 1;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case DO_PRE_EXECUTE:
onPreExecute();
break;
case DO_Post_EXECUTE:
onPostExecute();
break;
}
};
};
/**
* Runs on the UI thread after doInBackground 在子线程之后执行的任务
*/
public abstract void onPostExecute();
/**
* run in UI thread 在子线程之前执行的任务
*/
public abstract void onPreExecute();
/**
* run in back thread在子线程中执行的任务
*/
public abstract void doInBackground();
/**
*
*/
public void execute() {
onPreExecute();
new Thread(){
public void run() {
/* Message msg=Message.obtain();
msg.what=DO_PRE_EXECUTE;
handler.sendMessage(msg);*/
doInBackground();
Message msg=Message.obtain();
msg.what=DO_Post_EXECUTE;
handler.sendMessage(msg);
};
}.start();
}
}