`
zhoujianghai
  • 浏览: 434341 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

在工作线程中创建Toast

阅读更多

在工作线程中创建Toast,代码如下:

 

private void showToast() {
		new Thread() {
			public void run() {
				
				Looper.prepare();
				Toast.makeText(ActivityA.this, "来自工作线程", 

Toast.LENGTH_SHORT).show();
				Looper.loop();
			}
		}.start();
	}
 

 

 

如果不加Looper.prepare();则会出现以下异常:

java.lang.RuntimeException: Can't create handler inside thread that has not called 

Looper.prepare()
 

 

所以 在工作线程中创建Toast 须创建Looper对象。Looper.prepare();会创建当前线程的Looper对象和对应的MessageQueue(消息队列)

 

 

请参看Toast.java源码。

 Toast.java 包含 这句代码:final Handler mHandler = new Handler();   

mHandler是Toast的一个final类型的成员变量,在Handler的构造方法中有如下代码:

 

if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called 

Looper.prepare()");
        }
 

 

创建了Toast对象后,调用show()方法,把mShow这个Runnable添加到工作线程的消息队列中,

 

消息队列是在创建Looper对象的时候创建好的。

如代码:

 

final Runnable mShow = new Runnable() {
            public void run() {
                handleShow();
            }
        };

public void show() {
            if (localLOGV) Log.v(TAG, "SHOW: " + this);
            mHandler.post(mShow);
        }

/**
     * 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. 
     *  
     */
    public final boolean post(Runnable r)
    {
       return  sendMessageDelayed(getPostMessage(r), 0);
    }
 

此时调用Looper.loop(),从消息队列中取出消息,并执行。

主要源码:

 /**

     *  Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static final void loop() {
        Looper me = myLooper();
        MessageQueue queue = me.mQueue;
        while (true) {
            Message msg = queue.next(); // might block
            ......
                msg.target.dispatchMessage(msg);
               ......
                msg.recycle();
            }
        }
    }
 

此时会调用:mShow的handleShow();

源码:

 public void handleShow() {

            if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
                    + " mNextView=" + mNextView);
            if (mView != mNextView) {
                // remove the old view if necessary
                handleHide();
                mView = mNextView;
                mWM = WindowManagerImpl.getDefault();
                final int gravity = mGravity;
                mParams.gravity = gravity;
                if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == 

Gravity.FILL_HORIZONTAL) {
                    mParams.horizontalWeight = 1.0f;
                }
                if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 

Gravity.FILL_VERTICAL) {
                    mParams.verticalWeight = 1.0f;
                }
                mParams.x = mX;
                mParams.y = mY;
                mParams.verticalMargin = mVerticalMargin;
                mParams.horizontalMargin = mHorizontalMargin;
                if (mView.getParent() != null) {
                    if (localLOGV) Log.v(
                            TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                }
                if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
                mWM.addView(mView, mParams);
            }
        }
 

mWM.addView(mView, mParams);把Toast对应的view添加到当前Window中。

举一反三:

基于以上原理,可以实现这样的效果,拖动GridView里的ImageView元素,改变元素在GridView的位置。

分享到:
评论

相关推荐

    Android高级编程.pdf

    8.3.2 在工作(worker)线程中使用Toast 8.4 Notification简介 8.4.1 Notification Manager简介 8.4.2 创建Notification 8.4.3 触发Notification 8.4.4 向Earthquake Monitor中添加Notiflcation 8.4.5 高级...

    《Android高级编程》

    8.2.1 创建新的线程 8.2.2 为GUI操作同步线程 8.2.3 将Earthquake Service移动到后台线程 8.3 创建一个Toast 8.3.1 定制Toast 8.3.2 在工作(worker)线程中使用Toast 8.4 Notification简介 8.4.1 Notification ...

    android开发实践二

    监听系统广播usb连接,wifi连接,动态展示当前的usb连接状态和wifi连接状态,wifi相关参数等详细信息。(这个项目是在开发实践一的基础上实现的) ...(4)线程创建的方式 (5)Android 用LinkedList实现队列

    Android高级编程--源代码

    8.3.2 在工作(worker)线程中使用Toast 265 8.4 Notification简介 266 8.4.1 Notification Manager简介 267 8.4.2 创建Notification 267 8.4.3 触发Notification 268 8.4.4 向Earthquake Monitor中添加...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    8.3.3 Toast的Window创建过程 311 第9章 四大组件的工作过程 316 9.1 四大组件的运行状态 316 9.2 Activity的工作过程 318 9.3 Service的工作过程 336 9.3.1 Service的启动过程 336 9.3.2 Service的绑定过程 ...

    android开发秘籍

    3.6.1 秘诀23:使用toast 在屏幕上显示简短消息 61 3.6.2 秘诀24:使用alert 对话框 61 3.6.3 秘诀25:在状态栏中显示通知 62 第4 章 用户界面布局 65 4.1 资源目录及其基本属性 65 4.2 view 和viewgroup 67 ...

    Android开发艺术探索

     8.3.3 Toast的Window创建过程 / 311  第9章 四大组件的工作过程 / 316  9.1 四大组件的运行状态 / 316  9.2 Activity的工作过程 / 318  9.3 Service的工作过程 / 336  9.3.1 Service的启动过程 / 336  9.3.2...

    Android基于CountDownTimer实现倒计时功能

    将后台线程的创建和Handler队列封装成一个方便的类调用。 查看了一下官方文档,这个类及其简单,只有四个方法,上面都涉及到了onTick,onFinsh、cancel和start。其中前面两个是抽象方法,所以要重写一下。 下面是...

    精通ANDROID 3(中文版)1/2

    13.3.2 在工作线程与主线程之间通信  13.3.3 线程行为概述  13.4 处理程序示例驱动程序类  13.4.1 驱动程序活动文件  13.4.2 布局文件  13.4.3 菜单文件  13.4.4 描述文件  13.5 组件和进程寿命  ...

    精通Android 3 (中文版)2/2

    13.3.2 在工作线程与主线程之间通信  13.3.3 线程行为概述  13.4 处理程序示例驱动程序类  13.4.1 驱动程序活动文件  13.4.2 布局文件  13.4.3 菜单文件  13.4.4 描述文件  13.5 组件和进程寿命  ...

    android开发艺术探索高清完整版PDF

    / 301 8.2.3 Window的更新过程 / 303 8.3 Window的创建过程 / 304 8.3.1 Activity的Window创建过程 / 304 8.3.2 Dialog的Window创建过程 / 308 8.3.3 Toast的Window创建过程 / 311 第9章 四大组件的工作过程 ...

    Android开发案例驱动教程 配套代码

    3.1.1 在Eclipse中创建项目 15 3.1.2 编写程序项目代码 17 3.1.3 运行HelloAndroid 18 3.1.4 Android工程目录 19 3.1.5 AndroidManifest.xml文件 21 3.2 Android中的组件介绍 22 3.3 使用Android SDK帮助 23 ...

    Android蓝牙通信聊天实现发送和接受功能

    首先创建线程 实际上就是创建BluetoothChatService() (蓝牙聊天服务类) 这个时候把handler 传过去 这样就可以操作UI 界面了,在线程中不断轮询读取蓝牙消息,当主界面点击发送按钮时 调用BluetoothChatService 的...

    疯狂Android讲义源码

     2.1.3 在代码中控制UI界面 41  2.1.4 使用XML布局文件和Java  代码混合控制UI界面 42  2.1.5 开发自定义View 43  2.2 布局管理器 46  2.2.1 线性布局 47  2.2.2 表格布局 49  2.2.3 帧布局 52  2.2.4 相对...

    疯狂Android讲义.part2

    2.1.3 在代码中控制UI界面 41 2.1.4 使用XML布局文件和Java 代码混合控制UI界面 42 2.1.5 开发自定义View 43 2.2 布局管理器 46 2.2.1 线性布局 47 2.2.2 表格布局 49 2.2.3 帧布局 52 2.2.4 相对布局 55 2.2.5 绝对...

Global site tag (gtag.js) - Google Analytics