Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Lifecycle可在后台观察单击事件_Java_Android_Rx Java_Rx Android - Fatal编程技术网

Java Lifecycle可在后台观察单击事件

Java Lifecycle可在后台观察单击事件,java,android,rx-java,rx-android,Java,Android,Rx Java,Rx Android,我稍微更改了一点,以便在后台执行单击任务,但它不起作用:-( 这是我的代码: /** * Simple example of creating a Subscription that is bound to the lifecycle * (and thus automatically unsubscribed when the Activity is destroyed). */ public class LifecycleObservableActivity extends RxAct

我稍微更改了一点,以便在后台执行单击任务,但它不起作用:-(

这是我的代码:

/**
 * Simple example of creating a Subscription that is bound to the lifecycle
 * (and thus automatically unsubscribed when the Activity is destroyed).
 */
public class LifecycleObservableActivity extends RxActivity {

    private static final String TAG = LifecycleObservableActivity.class.getSimpleName();

    private Button button;

    private Subscription subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        button = new Button(this);
        button.setText("Click Me!");
        setContentView(button);
    }

    @Override
    protected void onStart() {

        super.onStart();

        Observable<OnClickEvent> clicks = ViewObservable.clicks(button)
                // .subscribeOn(Schedulers.computation());  IllegalStateException: Observers must subscribe from the main UI thread, but was Thread[RxComputationThreadPool-3,5,main]
                .observeOn(Schedulers.computation());

        subscription =
                LifecycleObservable.bindActivityLifecycle(lifecycle(), clicks)
                        .map(x -> {
                            Log.i(TAG, "Func1   - main thread: " + isCurrentlyOnMainThread() + " (" + Thread.currentThread() + ")");
                            return "hallo welt";
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(string -> {
                            Log.i(TAG, "Action1 - main thread: " + isCurrentlyOnMainThread() + " (" + Thread.currentThread() + ")");
                            Toast.makeText(LifecycleObservableActivity.this,
                                    string,
                                    Toast.LENGTH_SHORT)
                                    .show();
                        });
    }

    @Override
    protected void onPause() {

        super.onPause();

        // Should output "false"
        Log.i(TAG, "onPause(), isUnsubscribed=" + subscription.isUnsubscribed());
    }

    @Override
    protected void onStop() {

        super.onStop();

        // Should output "true"
        Log.i(TAG, "onStop(), isUnsubscribed=" + subscription.isUnsubscribed());
    }

    private boolean isCurrentlyOnMainThread() {

        return Looper.myLooper() == Looper.getMainLooper();
    }
}
Func1在后台运行,到目前为止是正确的。但中的订阅未取消订阅。它仅在Func1也在UI线程中运行时取消订阅

我必须改变什么,Func1在后台运行,订阅将取消订阅?

好的,我想我明白了:

    subscription = LifecycleObservable.bindActivityLifecycle(lifecycle(),
            AppObservable.bindActivity(this, ViewObservable.clicks(button))
                    .observeOn(Schedulers.computation())
                    .map(new Func1<OnClickEvent, String>() {
                        @Override
                        public String call(OnClickEvent onClickEvent) {
                            Log.i(TAG, "1 " + Thread.currentThread());
                            return ((Button) onClickEvent.view()).getText().toString();
                        }
                    })
                    .map(new Func1<String, String>() {
                        @Override
                        public String call(String o) {
                            Log.i(TAG, "2 " + Thread.currentThread());
                            return "hallo " + o;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread()))
            .subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "3 " + Thread.currentThread());
                    Log.i(TAG, "s: " + s);
                }
            });
    subscription = LifecycleObservable.bindActivityLifecycle(lifecycle(),
            AppObservable.bindActivity(this, ViewObservable.clicks(button))
                    .observeOn(Schedulers.computation())
                    .map(new Func1<OnClickEvent, String>() {
                        @Override
                        public String call(OnClickEvent onClickEvent) {
                            Log.i(TAG, "1 " + Thread.currentThread());
                            return ((Button) onClickEvent.view()).getText().toString();
                        }
                    })
                    .map(new Func1<String, String>() {
                        @Override
                        public String call(String o) {
                            Log.i(TAG, "2 " + Thread.currentThread());
                            return "hallo " + o;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread()))
            .subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "3 " + Thread.currentThread());
                    Log.i(TAG, "s: " + s);
                }
            });
1 Thread[RxComputationThreadPool-3,5,main]
2 Thread[RxComputationThreadPool-3,5,main]
3 Thread[main,5,main]
s: hallo ralph
onPause subscription isUnsubscribed: false
onStop subscription isUnsubscribed: true