fix: 增加协程示例界面

This commit is contained in:
xiaoyan 2023-03-30 15:08:47 +08:00
parent f7db1859a6
commit 5457d8a468
8 changed files with 100 additions and 38 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -15,7 +15,7 @@
<!-- android:exported="true"></service>-->
<activity
android:name=".RxAndroidActivity"
android:name="com.navinfo.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -23,6 +23,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.navinfo.RxAndroidActivity"
android:exported="true">
</activity>
<activity
android:name=".RxCoroutineActivity"
android:exported="true">
</activity>
</application>
</manifest>

View File

@ -1,14 +1,12 @@
package com.navinfo.testapplication
package com.navinfo
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.navinfo.testapplication.databinding.ActivityMainBinding
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.ObservableOnSubscribe
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
@ -22,16 +20,9 @@ class MainActivity : AppCompatActivity {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
val listData = 1..20
// 最简单的RxJava示例
mainBinding.btnRxjava1.setOnClickListener {
Observable.create<String>{
for (i in listData) {
it.onNext(i.toString())
}
it.onComplete()
}.subscribe {
println(it)
}
// 最简单的协程
mainBinding.btnStartRxjava.setOnClickListener {
startActivity(Intent(this@MainActivity, RxAndroidActivity::class.java))
}
mainBinding.btnStartCoroutine.setOnClickListener {

View File

@ -1,4 +1,4 @@
package com.navinfo.testapplication;
package com.navinfo;
import android.os.Bundle;
import android.util.Log;
@ -38,7 +38,6 @@ public class RxAndroidActivity extends AppCompatActivity {
// RxAndroid测试需要使用的数据
private List<String> listData = new ArrayList<>();
private Disposable disposable;
private Subscription subscription;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -304,19 +303,20 @@ public class RxAndroidActivity extends AppCompatActivity {
Flowable.create(new FlowableOnSubscribe<String>() {
@Override
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
RxAndroidActivity.this.emitter = emitter;
int i = 1;
while (true) {
if (emitter.requested()>0) {
Log.d(TAG, "前requested"+emitter.requested());
emitter.onNext((i++)+"");
Log.d(TAG, "后requested"+emitter.requested());
Log.d(TAG, "背压发送数据:"+i+"当前的requested"+emitter.requested());
} else {
Thread.sleep(1000);
if (!emitter.isCancelled()) {
if (emitter.requested()>0) {
Log.d(TAG, "前requested"+emitter.requested());
emitter.onNext((i++)+"");
Log.d(TAG, "背压发送数据:"+i+"当前的requested"+emitter.requested());
}
}
}
}
}, BackpressureStrategy.BUFFER)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(new FlowableSubscriber<String>() {
@Override
@ -327,12 +327,14 @@ public class RxAndroidActivity extends AppCompatActivity {
@Override
public void onNext(String s) {
try {
Thread.sleep(100);
Log.d(TAG, "OnNext"+s);
subscription.request(1);
} catch (InterruptedException e) {
e.printStackTrace();
if (!emitter.isCancelled()) {
try {
subscription.request(1);
Thread.sleep(100);
Log.d(TAG, "OnNext"+s);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@ -343,10 +345,21 @@ public class RxAndroidActivity extends AppCompatActivity {
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
}
});
binding.rxSimple6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (subscription!=null) {
subscription.cancel();
}
}
});
}
private Subscription subscription;
private FlowableEmitter emitter;
}

View File

@ -0,0 +1,21 @@
package com.navinfo.testapplication
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.navinfo.testapplication.databinding.ActivityCoroutineBinding
class RxCoroutineActivity: AppCompatActivity() {
private val binding by lazy {
ActivityCoroutineBinding.inflate(LayoutInflater.from(this));
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
// 最简单的协程测试
binding.coroutineSimple1.setOnClickListener {
}
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<com.google.android.material.button.MaterialButton
android:id="@+id/coroutine_simple_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="最简单的协程">
</com.google.android.material.button.MaterialButton>
</LinearLayout>
</RelativeLayout>

View File

@ -5,24 +5,24 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context="com.navinfo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_rxjava1"
<Button
android:id="@+id/btn_start_rxjava"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Colored"
android:text="最简单的RxJava示例"></androidx.appcompat.widget.AppCompatButton>
android:text="RxJava测试界面"></Button>
<Button
android:id="@+id/btn_start_coroutine"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="启动协程"></Button>
style="@style/Widget.AppCompat.Button.Colored"
android:text="协程测试界面"></Button>
</LinearLayout>
</RelativeLayout>

View File

@ -37,5 +37,11 @@
android:layout_height="wrap_content"
android:text="背压">
</com.google.android.material.button.MaterialButton>
<com.google.android.material.button.MaterialButton
android:id="@+id/rx_simple_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消流程">
</com.google.android.material.button.MaterialButton>
</LinearLayout>
</RelativeLayout>