3
3
import android .content .Context ;
4
4
import android .content .Intent ;
5
5
import android .os .Bundle ;
6
- import android .os .Handler ;
7
- import android .os .Message ;
8
6
import android .support .annotation .Nullable ;
9
7
import android .view .View ;
10
8
import android .widget .TextView ;
11
9
12
10
import com .blankj .androidutilcode .R ;
13
11
import com .blankj .androidutilcode .base .BaseBackActivity ;
14
- import com .blankj .subutil .util .ThreadPoolUtils ;
15
12
import com .blankj .utilcode .util .NetworkUtils ;
16
13
import com .blankj .utilcode .util .SpanUtils ;
14
+ import com .blankj .utilcode .util .ThreadUtils ;
17
15
18
16
/**
19
17
* <pre>
@@ -27,29 +25,18 @@ public class NetworkActivity extends BaseBackActivity {
27
25
28
26
TextView tvAboutNetwork ;
29
27
TextView tvAboutNetworkAsync ;
30
- ThreadPoolUtils threadPoolUtils = new ThreadPoolUtils (ThreadPoolUtils .SingleThread , 1 );
28
+ ThreadUtils .SimpleTask mSimpleTask = new ThreadUtils .SimpleTask <String >() {
29
+ @ Override
30
+ public String doInBackground () throws Throwable {
31
+ return "isAvailableByPing: " + NetworkUtils .isAvailableByPing ()
32
+ + "\n getDomainAddress: " + NetworkUtils .getDomainAddress ("baidu.com" );
33
+ }
31
34
32
- Handler mHandler = new Handler (new Handler .Callback () {
33
35
@ Override
34
- public boolean handleMessage (Message msg ) {
35
- String text = tvAboutNetworkAsync .getText ().toString ();
36
- if (text .length () != 0 ) {
37
- text += '\n' ;
38
- }
39
- if (msg .what == 1 ) {
40
- tvAboutNetworkAsync .setText (new SpanUtils ()
41
- .append (text + "isAvailableByPing: " + msg .obj )
42
- .create ()
43
- );
44
- } else {
45
- tvAboutNetworkAsync .setText (new SpanUtils ()
46
- .append (text + "getDomainAddress: " + msg .obj )
47
- .create ()
48
- );
49
- }
50
- return true ;
36
+ public void onSuccess (String result ) {
37
+ tvAboutNetworkAsync .setText (result );
51
38
}
52
- }) ;
39
+ };
53
40
54
41
public static void start (Context context ) {
55
42
Intent starter = new Intent (context , NetworkActivity .class );
@@ -79,25 +66,7 @@ public void initView(Bundle savedInstanceState, View contentView) {
79
66
80
67
@ Override
81
68
public void doBusiness () {
82
- threadPoolUtils .execute (new Runnable () {
83
- @ Override
84
- public void run () {
85
- Message msg = Message .obtain ();
86
- msg .what = 1 ;
87
- msg .obj = NetworkUtils .isAvailableByPing ();
88
- mHandler .sendMessage (msg );
89
- }
90
- });
91
-
92
- threadPoolUtils .execute (new Runnable () {
93
- @ Override
94
- public void run () {
95
- Message msg = Message .obtain ();
96
- msg .what = 2 ;
97
- msg .obj = NetworkUtils .getDomainAddress ("baidu.com" );
98
- mHandler .sendMessage (msg );
99
- }
100
- });
69
+ ThreadUtils .executeBySingle (mSimpleTask );
101
70
}
102
71
103
72
@ Override
@@ -134,8 +103,7 @@ private void updateAboutNetwork() {
134
103
135
104
@ Override
136
105
protected void onDestroy () {
137
- threadPoolUtils .shutDownNow ();
138
- mHandler .removeCallbacksAndMessages (null );
106
+ ThreadUtils .cancel (mSimpleTask );
139
107
super .onDestroy ();
140
108
}
141
109
}
0 commit comments