@@ -26,6 +26,7 @@ public class RxAndroidActivity extends Activity {
2626 private TextView mTv1 ;
2727 private TextView mTv2 ;
2828 private TextView mTv3 ;
29+ private TextView mTimerTv ;
2930
3031 private int mCount = 0 ;
3132
@@ -41,6 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
4142 mTv1 = (TextView ) findViewById (R .id .act_rx_tv1 );
4243 mTv2 = (TextView ) findViewById (R .id .act_rx_tv2 );
4344 mTv3 = (TextView ) findViewById (R .id .act_rx_tv3 );
45+ mTimerTv = (TextView ) findViewById (R .id .act_rx_timer_tv );
4446
4547 findViewById (R .id .act_rx_btn1 ).setOnClickListener (new View .OnClickListener () {
4648 @ Override
@@ -53,8 +55,8 @@ public void onClick(View v) {
5355 @ SuppressWarnings ("unchecked" )
5456 Observable <String > observable = Observable .create (mObservableAction )
5557 .subscribeOn (AndroidSchedulers .mainThread ());
56- observable .subscribe (mSubscriber1 );
57- observable .subscribe (mActionTv2 );// 这个可以一直执行下去
58+ observable .subscribe (mSubscriber1 );// 先通知一个,再通知另一个
59+ observable .subscribe (mActionTv2 ); // 这个可以一直执行下去
5860 }
5961 });
6062
@@ -70,6 +72,29 @@ public void onClick(View v) {
7072 });
7173 }
7274
75+ @ Override
76+ protected void onResume () {
77+ super .onResume ();
78+ new Thread (new Runnable () {
79+ @ Override
80+ public void run () {
81+ int s = 0 ;
82+ while (s <= 100 ) {
83+ Observable <String > timerOb = Observable .just (String .valueOf (s ) + "s" );
84+ timerOb .observeOn (AndroidSchedulers .mainThread ());
85+ timerOb .subscribe (mActionTimer );
86+ try {
87+ Thread .sleep (1000L );
88+ } catch (InterruptedException e ) {
89+ e .printStackTrace ();
90+ break ;
91+ }
92+ s ++;
93+ }
94+ }
95+ }).start ();
96+ }
97+
7398 /**
7499 * 接收消息的订阅者
75100 */
@@ -131,4 +156,20 @@ public void call(String s) {
131156 }
132157 };
133158
159+ private Action1 <String > mActionTimer = new Action1 <String >() {
160+ @ Override
161+ public void call (String s ) {
162+ final String second = s ;
163+ /**
164+ * 跑在UI线程里更新
165+ */
166+ runOnUiThread (new Runnable () {
167+ @ Override
168+ public void run () {
169+ mTimerTv .setText (second );
170+ }
171+ });
172+ }
173+ };
174+
134175}
0 commit comments