Android中的处理程序[英] Handlers in Android

本文是小编为大家收集整理的关于Android中的处理程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在Android Programing中使用的是什么处理程序?我们如何使用它们来解除ProgressDialog?

我已经提到了净重新制作的几件事,但不能发现它们非常令人信服.解雇进步程序和处理程序的一个例子将是一件好事.

谢谢, 大卫

推荐答案

使用ASYNCTASK.它有一个名为onpostexecute的函数,您可以在其中忽略进度对话框.

@Override
    protected void onPostExecute(Void result) {
        Toast.makeText(ctx, "Completed Synch with Server", Toast.LENGTH_SHORT)
                .show();

                mProgressDialog.dismiss();

    }

处理程序用于在预定时间运行一小部分代码.通常,一个使用AlarmManager启动意图(活动,服务,广播),但如果您有兴趣仅运行一小部分代码,您可以使用处理程序:

if(condition == true){

    Handler timer = new Handler();
    timer.postDelayed(task, (5 * 60 * 1000);
}

---

      private Runnable task = new Runnable() {
        public void run() {
            mProgressDialog.dismiss();
        }
    };

我强烈建议使用ASYNCTASK在Android上有关的任何线程.

其他推荐答案

处理程序用于UI和背景线程之间的通信.基本上处理程序将发送消息和RUNNABLE上线程的消息队列.

本文地址:https://www.itbaoku.cn/post/102739.html

问题描述

What are Handler used in android proggraming for? How do we use them to dismiss the ProgressDialog?

I have referred few things from the Net regerding this but couldn't found them pretty convincing. An example of dismissing a ProgressDialog along with Handler will be a great thing.

Thanks, david

推荐答案

Use AsyncTask instead. It has a function called onPostExecute where in you can dismiss the Progress Dialog.

@Override
    protected void onPostExecute(Void result) {
        Toast.makeText(ctx, "Completed Synch with Server", Toast.LENGTH_SHORT)
                .show();

                mProgressDialog.dismiss();

    }

Handlers are used to run a small section of code at a predetermined time. Typically one uses AlarmManager to launch intents (activities, services, broadcasts), but if you are interested in running only a small section of code you can use handlers:

if(condition == true){

    Handler timer = new Handler();
    timer.postDelayed(task, (5 * 60 * 1000);
}

---

      private Runnable task = new Runnable() {
        public void run() {
            mProgressDialog.dismiss();
        }
    };

I'd strongly recommend using a AsyncTask for anything thread related on Android.

其他推荐答案

Handlers are used for communication between the UI and background thread. Basically Handler will send messages and runnables on to the message Queue of a thread.