Bolts Framework: onSuccess 在 UI/Main 线程上进行更改



一旦任务调用在后台执行完毕,是否可以在UI线程上显示消息或进行更改?

如下所示:

Task.callInBackground(new Callable<String>() {
            @Override
            public String call() {

                for(int i=0; i<3; i++){
                    Log.i("I=", String.valueOf(i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                String obj = "";
                return null;
            }
        }).onSuccess(new Continuation<String, Object>() {
            @Override
            public Object then(Task<String> task) throws Exception {
                Log.i("I=", "Counter complete");
                Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
                btnAgriLoan.setText("LOL");
                return null;
            }
        });

目前没有显示Toast消息,也没有崩溃。

在Bolts Framework中寻找AsyncTask的onPostExecute的等价物,可以在其中添加对UI的更改。

没有意识到每个帮助程序函数都可以提到 EXECUTOR 的类型,如下所示:(Task.UI_THREAD_EXECUTOR(

Task.callInBackground(new Callable<String>() {
            @Override
            public String call() {
                for(int i=0; i<3; i++){
                    Log.i("I=", String.valueOf(i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                String obj = "";
                return null;
            }
        }).onSuccess(new Continuation<String, Void>() {
            public Void then(Task<String> object) throws Exception {
                Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
                btnAgriLoan.setText("LOL");
                return null;
            }
        }, Task.UI_THREAD_EXECUTOR);

文档有帮助!

相关内容

最新更新