如何在 Android UI 线程异步执行一些代码? [英] How to execute some code in Android UI thread async?

查看:18
本文介绍了如何在 Android UI 线程异步执行一些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发新手.我已经在 Swing 和 SWT 上工作了好几年.Swing 和 SWT 都有在 UI 线程同步和异步中执行代码的策略.典型的用法是在一个线程中做一些耗时的工作,然后在 UI 线程异步显示结果.

I'm new to Android development. I've be working on Swing and SWT for several years. Both Swing and SWT has a stratage to execute code in UI thread sync and async. The typical usage is doing some time-consume staff in one thread then display the result in UI thread async.

所以我的问题是,Android 中是否有类似的策略?这是我的代码.参数 runnable 是一些耗时的代码.此方法将在执行期间显示一个等待对话框,然后 EXPECT 在完成后显示一个 Toast.但是 Toast 需要在 UI 线程中显示.那么怎么做呢?

So my question is, is there similiar stratage in Android? Here is my code. Parameter runnable is some time-consume code. This method will display a waiting dialog during the execution then EXPECT to show a Toast after it is finished. But the Toast need to be show in UI thread. So how to do that?

    public static void showWaitingDialog(final Activity parent, final Runnable runnable, String msg) {

    if (StringUtils.isEmpty(msg)) {
        msg = "processing...";
    }

    final ProgressDialog waitingDialog = ProgressDialog.show(parent, "Please Wait...", msg, true);

    // execute in a new thread instead of UI thread
    ThreadPoolUtil.execute(new Runnable() {

        public void run() {
            try {
                // some time-consume operation
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                waitingDialog.dismiss();
            }
            // TODO: How to display a Toast message here? execute some code in UI Thread.

        }

    });

}

还有关于Android UI系统的一些话吗?比如它是否线程安全,线程如何协同工作等等.非常感谢!

And is there some words about Android UI system? Such as is it Thread-Safe, how thread works together and so on. Many Thanks!

推荐答案

有几种方法可以做到这一点,

There are several ways for doing that,

  • 异步任务 -

AsyncTask 支持正确且轻松地使用 UI 线程.这个班允许执行后台操作并在 UI 上发布结果线程而无需操作线程和/或处理程序.使用 AsyncTask 的示例

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. Example for using AsyncTask

  • 服务 -
  • 服务是一个应用程序组件,代表一个应用程序希望执行长时间运行的操作而不是与用户交互或为其他用户提供功能要使用的应用程序.服务使用示例.

    A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Example for Using Service.

    • IntentService -
    • IntentService 是处理异步服务的基类按需请求(表示为意图).客户端发送请求通过 startService(Intent) 调用;根据需要启动服务,使用工作线程依次处理每个 Intent,并停止自身当它耗尽工作时.使用 IntentService 的示例.

      IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. Example for using IntentService.

      这篇关于如何在 Android UI 线程异步执行一些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆