Skip to content

Commit 34a6250

Browse files
committed
update RxAndroid demo
1 parent e1f73ef commit 34a6250

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

app/src/main/java/com/rust/aboutview/activity/RxAndroidActivity.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
android:layout_width="match_parent"
4141
android:layout_height="wrap_content"
4242
android:text="btn2" />
43+
44+
<TextView
45+
android:id="@+id/act_rx_timer_tv"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:layout_marginTop="20dp"
49+
android:textAlignment="center"
50+
android:textColor="@color/colorRed"
51+
android:textSize="16sp" />
4352
</LinearLayout>
4453

4554

0 commit comments

Comments
 (0)