创建的Observable的onNext不起作用

时间:2018-07-27 11:46:44

标签: rx-java rx-java2 rx-android

我正在学习如何使用Observable.create()。所以我创建了以下发布的示例。但是当我运行该应用程序时。然后单击按钮中的日志语句 onNext永远不会显示。我收到以下消息:

The application may be doing too much work on its main thread.

请让我知道如何正确地操作它。

代码

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Button btnStart = null;
    private TextView tvText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.btnStart = (Button) findViewById(R.id.btnStart);
        this.tvText = (TextView) findViewById(R.id.tvText);

        final int[] i = {0};
        //how to create emitter from a worker thread.
        Observable.create(emitter -> {
            btnStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    emitter.onNext(Log.i(TAG, "i: " + i[0]++));
                }
            });
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
    }
}

0 个答案:

没有答案