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

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

问题描述

我是新来的Andr​​oid的发展。我是工作在Swing和SWT了好几年。无论Swing和SWT有对策 - 以执行code在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的similiar对策 - ?这是我的code。参数可运行一些时间消耗code。这种方法将在执行过程中显示一个对话框等待,然后希望它完成后,显示敬酒。但吐司必须展现在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!

推荐答案

有几种方法做的,

  • 的AsyncTask -
  • AsyncTask -

的AsyncTask能够适当且易于使用的用户界面线程。这个类   允许在UI上进行后台操作并公布结果   螺纹,而不必操纵线程和/或处理程序。 <一href="http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency_asynchtask"><$c$c>Example使用的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

  • 服务 -

  • Service -
  • 一个服务是一种应用程序组件重新presenting要么是   应用程序的愿望,执行长时间运行的操作,而不是   与用户交互,或提供功能性的其他   应用程序使用。 示例使用服务。

    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 -
    • IntentService对于那些处理异步服务的基类   请求(pssed的意图EX $ P $)需求。客户端发送请求   通过startService(意向)调用;根据需要该服务已启动,   处理依次使用辅助线程每个意图,并停止本身   当它运行失去工作。 <一href="http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/"><$c$c>Example使用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.

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

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