美文网首页
Android更新UI的常用方法

Android更新UI的常用方法

作者: zizi192 | 来源:发表于2017-06-14 19:41 被阅读0次

Android开发中经常使用后台线程做些耗时的工作,任务完成后可以通过以下4种方法来更新ui界面。对应的函数功能说明附上,以做备份。

1、Activity.runOnUiThread(Runnable action)

Runs the specified action on the UI thread. If the current thread is the UI
     * thread, then the action is executed immediately. If the current thread is
     * not the UI thread, the action is posted to the event queue of the UI thread.

2、View.post(Runnable action)

 Causes the Runnable to be added to the message queue.
     * The runnable will be run on the user interface thread.

类似的方法包括View.postDelayed(Runnable action, long delayMillis)来延时触发ui事件

3、Handler.sendMessage(Message msg)

Pushes a message onto the end of the message queue after all pending messages
     * before the current time. It will be received in {@link #handleMessage},
     * in the thread attached to this handler.

注意,Handler必须在ui线程中初始化(Handler handler = new Handler()),或者调用Handler mainHandler = new Handler(Looper.getMainLooper())

Message创建时,推荐使用以下方法,提高创建效率:

Message msg = mainHandler.obtainMessage();
Message mst = Message.obtain();

//Message的参数解析如下
msg.what = 1000;  //消息标识。每个handler有各自的命名空间,可以不必担心冲突
msg.arg1=2;   //存放整形数据,如果携带数据简单,优先使用arg1和arg2,比Bundle更节省内存。
msg.arg2=3;   //存放整形数据

Bundle bundle=new Bundle();
bundle.putString("dd","adfasd");
bundle.putInt("love",5);
msg.setData(bundle);

msg.obj=“test”;   //用来存放Object类型的任意对象。使用messenger跨进程传消息时,需要实现Parcelable接口

发送消息的其他方法:
1)sendEmptyMessage(int what); //发送设置了what消息标志的空消息
2)sendEmptyMessageAtTime(int what, long uptimeMillis); //定时发送空消息
3)sendEmptyMessageDelayed(int what, long delayMillis); //延时发送空消息
4)sendMessageAtTime(Message msg, long uptimeMillis); //定时发送消息
5)sendMessageDelayed(Message msg, long delayMillis); //延时发送消息
6)sendMessageAtFrontOfQueue(Message msg); //最先处理消息(慎用)

4、Handler.post(Runnable r)

Causes the Runnable r to be added to the message queue.
        * The runnable will be run on the thread to which this handler is attached. 

注意,Handler必须在ui线程中初始化(Handler handler = new Handler()),或者调用Handler mainHandler = new Handler(Looper.getMainLooper())
类似的方法还有:
1)postAtTime(Runnable r, long uptimeMillis); //在某一时刻发送消息
2)postAtDelayed(Runnable r, long delayMillis); //延迟delayMillis毫秒再发送消息

相关文章

  • Android更新UI的常用方法

    Android开发中经常使用后台线程做些耗时的工作,任务完成后可以通过以下4种方法来更新ui界面。对应的函数功能说...

  • 第三周

    Handler Handler 的功能 Handler 最常用的功能就是更新 UI。因为 Android 只能在主...

  • 关于Android在非UI线程更新UI的问题。

    为了解决在Android非UI线程更新UI这个问题,Android提供了一些方法,从其他线程访问UI线程。 Act...

  • Android更新UI的两种方法 - handler和runOn

    Android更新UI的两种方法——handler和runOnUiThread() 在Android开发过程中,常...

  • Android异步处理技术

    Handler是Android的异步消息处理机制,常用于更新UI操作。 一、Handler、Looper、Mess...

  • Android多线程

    1.沿用java的子线程创建 2.在子线程中不能更新UI,那么在Android中更新UI的方法 runOnUiTh...

  • Android 对UI操作的工具类UIUtils

    Android中对UI操作的工具类,大概还有不少常用方法没有记录进去,以后用到一些再更新就好哒~网上那么多资料来着...

  • Android Handler探索

    在日常开发中,我们常用Handler来在子线程更新主线程UI,这是因为Android系统不允许我们在子线程更新UI...

  • Android更新UI的方法

    一.Activity的 runOnUiThread 二.Handler sendEmptyMessage()/se...

  • UI常用方法(持续更新...)

    退出APP到桌面 写在父类控制器的方法(个人习惯) 控制器之间的操作 scrollview: tableview:...

网友评论

      本文标题:Android更新UI的常用方法

      本文链接:https://www.haomeiwen.com/subject/xrseqxtx.html