Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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
Android 如何在可运行线程中实现简单的RxJava调用?_Android_Rx Java_Rx Android - Fatal编程技术网

Android 如何在可运行线程中实现简单的RxJava调用?

Android 如何在可运行线程中实现简单的RxJava调用?,android,rx-java,rx-android,Android,Rx Java,Rx Android,我尝试将一个简单的RxJava调用添加到一个可运行的线程中,以便在线程完成后更新UI。我该怎么做呢?这是我的活动代码: public class PrintActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

我尝试将一个简单的RxJava调用添加到一个可运行的线程中,以便在线程完成后更新UI。我该怎么做呢?这是我的活动代码:

public class PrintActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
        printZpl("^XA^LL360^POI^FO20,20^A0N,25,25^FDThis is a test of the ZPL file printing on " + Helper.getCurrDateTime() + "^FS^XZ");
    }
}
下面是执行可运行线程的类:

public class PrinterManager {
    private static void printZpl(final String body, final String footer) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Connection btCon = new BluetoothConnectionInsecure("AC:3F:A4:0E:22:05");
                    btCon.open();
                    btCon.write(body.getBytes());
                    Thread.sleep(500);
                    btCon.close();
                    // Insert RxJava return here to update the UI in the activity once the thread is completed.
                } catch (Exception e) {
                    Timber.e(e.getMessage());
                }
            }
        }).start();
    }
}

我简化了这个帖子的代码。实际的代码要复杂得多…

将代码的异步部分包装成如下可见的形式:

public class PrinterManager {
    public static Observable<Void> printZpl(final String body, final String footer) {

    return Observable.fromCallable(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            try {
                Connection btCon = new BluetoothConnectionInsecure("AC:3F:A4:0E:22:05");
                btCon.open();
                btCon.write(body.getBytes());
                Thread.sleep(500);
                btCon.close();                  
            } catch (Exception e) {
                Timber.e(e.getMessage());
            }
            return null;
        }
    });
}
}
如果尚未添加,则需要将依赖项添加到
app.gradle
文件:

compile "io.reactivex:rxandroid:1.2.0"
compile "io.reactivex:rxjava:1.2.0"
如果要更新UI,请将观测者传递给
subscribe
方法,而不是像上面的示例那样使用空的观测者。

使用RxJava2:

Completable.fromAction(() -> {
            Connection btCon = new BluetoothConnectionInsecure("AC:3F:A4:0E:22:05");
            btCon.open();
            btCon.write(body.getBytes());
            Thread.sleep(500);
            btCon.close();
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();

使用
可完成的
而不是
可观察的
,因为除了完成事件之外,您不会发出任何东西。

欢迎使用堆栈溢出。请注意,在这里说“谢谢”的首选方式是投票选出好的问题和有用的答案(一旦你有足够的声誉这么做),并接受对你提出的任何问题最有用的答案(这也会给你的声誉带来一点提升)。请查看页面,并且当我尝试此代码时,我得到一个错误:Lambda表达式在此语言级别不受支持。一旦我将返回值更改为Observable,它就可以工作了。i、 公共静态可观察rxJavaPrint2(最终字符串体){return Observable.fromCallable(new Callable(){@Override public Void call()抛出异常{try{@Jalsa true!使用
return
语句和正确的类型更新到。我如何使用类似于:finishTextView.setText(“打印完成”);?对不起,我是新手:-)的内容更新UI有不同版本的
subscribe()
方法。需要一个
观察者
类型。我建议实现和
观察者
,在它的
onNext
onCompleted
中,您可以编写代码来更新UI。只需将一次性观察者添加到订阅()中,如下所示:
Completable.fromAction(() -> {
            Connection btCon = new BluetoothConnectionInsecure("AC:3F:A4:0E:22:05");
            btCon.open();
            btCon.write(body.getBytes());
            Thread.sleep(500);
            btCon.close();
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();