fix: 增加协程示例界面
This commit is contained in:
parent
f7db1859a6
commit
5457d8a468
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal 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>
|
@ -15,7 +15,7 @@
|
|||||||
<!-- android:exported="true"></service>-->
|
<!-- android:exported="true"></service>-->
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".RxAndroidActivity"
|
android:name="com.navinfo.MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
@ -23,6 +23,14 @@
|
|||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name="com.navinfo.RxAndroidActivity"
|
||||||
|
android:exported="true">
|
||||||
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name=".RxCoroutineActivity"
|
||||||
|
android:exported="true">
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -1,14 +1,12 @@
|
|||||||
package com.navinfo.testapplication
|
package com.navinfo
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.PersistableBundle
|
import android.os.PersistableBundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.navinfo.testapplication.databinding.ActivityMainBinding
|
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.GlobalScope
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -22,16 +20,9 @@ class MainActivity : AppCompatActivity {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
|
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
|
||||||
super.onCreate(savedInstanceState, persistentState)
|
super.onCreate(savedInstanceState, persistentState)
|
||||||
val listData = 1..20
|
val listData = 1..20
|
||||||
// 最简单的RxJava示例
|
// 最简单的协程
|
||||||
mainBinding.btnRxjava1.setOnClickListener {
|
mainBinding.btnStartRxjava.setOnClickListener {
|
||||||
Observable.create<String>{
|
startActivity(Intent(this@MainActivity, RxAndroidActivity::class.java))
|
||||||
for (i in listData) {
|
|
||||||
it.onNext(i.toString())
|
|
||||||
}
|
|
||||||
it.onComplete()
|
|
||||||
}.subscribe {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mainBinding.btnStartCoroutine.setOnClickListener {
|
mainBinding.btnStartCoroutine.setOnClickListener {
|
@ -1,4 +1,4 @@
|
|||||||
package com.navinfo.testapplication;
|
package com.navinfo;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -38,7 +38,6 @@ public class RxAndroidActivity extends AppCompatActivity {
|
|||||||
// RxAndroid测试需要使用的数据
|
// RxAndroid测试需要使用的数据
|
||||||
private List<String> listData = new ArrayList<>();
|
private List<String> listData = new ArrayList<>();
|
||||||
private Disposable disposable;
|
private Disposable disposable;
|
||||||
private Subscription subscription;
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -304,19 +303,20 @@ public class RxAndroidActivity extends AppCompatActivity {
|
|||||||
Flowable.create(new FlowableOnSubscribe<String>() {
|
Flowable.create(new FlowableOnSubscribe<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
|
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
|
||||||
|
RxAndroidActivity.this.emitter = emitter;
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (emitter.requested()>0) {
|
if (!emitter.isCancelled()) {
|
||||||
Log.d(TAG, "前requested:"+emitter.requested());
|
if (emitter.requested()>0) {
|
||||||
emitter.onNext((i++)+"");
|
Log.d(TAG, "前requested:"+emitter.requested());
|
||||||
Log.d(TAG, "后requested:"+emitter.requested());
|
emitter.onNext((i++)+"");
|
||||||
Log.d(TAG, "背压发送数据:"+i+",当前的requested:"+emitter.requested());
|
Log.d(TAG, "背压发送数据:"+i+",当前的requested:"+emitter.requested());
|
||||||
} else {
|
}
|
||||||
Thread.sleep(1000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, BackpressureStrategy.BUFFER)
|
}, BackpressureStrategy.BUFFER)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(Schedulers.computation())
|
.observeOn(Schedulers.computation())
|
||||||
.subscribe(new FlowableSubscriber<String>() {
|
.subscribe(new FlowableSubscriber<String>() {
|
||||||
@Override
|
@Override
|
||||||
@ -327,12 +327,14 @@ public class RxAndroidActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(String s) {
|
public void onNext(String s) {
|
||||||
try {
|
if (!emitter.isCancelled()) {
|
||||||
Thread.sleep(100);
|
try {
|
||||||
Log.d(TAG, "OnNext:"+s);
|
subscription.request(1);
|
||||||
subscription.request(1);
|
Thread.sleep(100);
|
||||||
} catch (InterruptedException e) {
|
Log.d(TAG, "OnNext:"+s);
|
||||||
e.printStackTrace();
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,10 +345,21 @@ public class RxAndroidActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
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;
|
||||||
}
|
}
|
@ -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 {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
app/src/main/res/layout/activity_coroutine.xml
Normal file
17
app/src/main/res/layout/activity_coroutine.xml
Normal 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>
|
@ -5,24 +5,24 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
tools:context=".MainActivity">
|
tools:context="com.navinfo.MainActivity">
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_centerInParent="true">
|
android:layout_centerInParent="true">
|
||||||
<androidx.appcompat.widget.AppCompatButton
|
<Button
|
||||||
android:id="@+id/btn_rxjava1"
|
android:id="@+id/btn_start_rxjava"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
style="@style/Widget.AppCompat.Button.Colored"
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
android:text="最简单的RxJava示例"></androidx.appcompat.widget.AppCompatButton>
|
android:text="RxJava测试界面"></Button>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_start_coroutine"
|
android:id="@+id/btn_start_coroutine"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
android:text="启动协程"></Button>
|
android:text="协程测试界面"></Button>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
@ -37,5 +37,11 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="背压">
|
android:text="背压">
|
||||||
</com.google.android.material.button.MaterialButton>
|
</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>
|
</LinearLayout>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
Loading…
x
Reference in New Issue
Block a user