1、AsynTask类结构asysTask类主要用到的几个内部回调函数有:
onPreExecute()
doInBackGround()
onProgressUpdate()
onPostExecute()
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<TextView
android:id=
"@+id/text"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"@string/hello"
/>
<Button
android:id=
"@+id/button"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"change"
/>
</LinearLayout>
|
01
02
03
04
05
06
07
08
09
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import
android.app.Activity;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;
public
class
AnsyTestActivity
extends
Activity {
/** Called when the activity is first created. */
TextView text =
null
;
Button button=
null
;
String str=
null
;
AnsyTry anys=
null
;
double
result=
0
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(TextView) findViewById(R.id.text);
button=(Button) findViewById(R.id.button);
str=
"wei"
;
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
anys=
new
AnsyTry(text);
anys.execute(str);
}
});
}
class
AnsyTry
extends
AsyncTask<String, TextView, Double>{
TextView te=
null
;
public
AnsyTry(TextView te) {
super
();
this
.te = te;
}
@Override
protected
Double doInBackground(String... params) {
// TODO Auto-generated method stub
double
dou=
0
;
if
(params[
0
].equals(
"wei"
)){
System.out.println(Thread.currentThread().getName()+
" recive wei"
);
dou=
100
;
}
publishProgress(te);
return
dou;
}
@Override
protected
void
onPostExecute(Double result) {
// TODO Auto-generated method stub
super
.onPostExecute(result);
System.out.println(
"postExecute---double---"
+result);
}
@Override
protected
void
onPreExecute() {
// TODO Auto-generated method stub\
System.out.println(
"pretExecute------"
);
super
.onPreExecute();
}
@Override
protected
void
onProgressUpdate(TextView... values) {
// TODO Auto-generated method stub
values[
0
].setText(values[
0
].getText()+
"1"
);
super
.onProgressUpdate(values);
}
}
}
|