Android在非UI线程中显示Toast

来源:互联网 发布:小米note网络制式查询 编辑:程序博客网 时间:2024/05/20 14:28
  1. [java] view plaincopyprint?
    1. public void showToast(String msg){  
    2.         Looper.prepare();  
    3.         Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();  
    4.         Looper.loop();  
    5.     }  
    public void showToast(String msg){Looper.prepare();Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();Looper.loop();}

    只需要加上那两句就能在非UI线程中显示Toast

    [java] view plaincopyprint?
    1. Toast里面的show()  
    2.     public void show() {  
    3.       ...  
    4.         service.enqueueToast(pkg, tn, mDuration);   //把这个toast插入到一个队列里面  
    5.         ...  
    6.     }  
    Toast里面的show()    public void show() {      ...        service.enqueueToast(pkg, tn, mDuration);   //把这个toast插入到一个队列里面        ...    }

    [java] view plaincopyprint?
    1. Looper  
    2. public static final void prepare() {  
    3.         if (sThreadLocal.get() != null) {  
    4.             throw new RuntimeException("Only one Looper may be created per thread");  
    5.         }  
    6.        sThreadLocal.set(new Looper());  //在当前线程中创建一个Looper  
    7.     }  
    8.   
    9. private Looper() {  
    10.         mQueue = new MessageQueue();  //关键在这,创建Looper都干了什么。 其实是创建了消息队列  
    11.         mRun = true;  
    12.         mThread = Thread.currentThread();  
    13.     }  
    Looperpublic static final void prepare() {        if (sThreadLocal.get() != null) {            throw new RuntimeException("Only one Looper may be created per thread");        }       sThreadLocal.set(new Looper());  //在当前线程中创建一个Looper    }private Looper() {        mQueue = new MessageQueue();  //关键在这,创建Looper都干了什么。 其实是创建了消息队列        mRun = true;        mThread = Thread.currentThread();    }

 

 

 

 

 

[java] view plaincopyprint?
  1. public int onStartCommand(Intent intent, int flags, int startId) {  
  2.         // TODO Auto-generated method stub  
  3.           
  4.         System.out.println("OnStartCommand");  
  5.         handler = new Handler(Looper.getMainLooper());  
  6.         handler.post(new Runnable(){  
  7.   
  8.             public void run() {  
  9.                 // TODO Auto-generated method stub  
  10.                 System.out.println("in run");  
  11.                 Toast toast = Toast.makeText(getApplicationContext(),"onStartCommand",Toast.LENGTH_SHORT);  
  12.                 toast.setGravity(Gravity.TOP,0220);  
  13.                 toast.show();  
  14.             }  
  15.               
  16.         });  
  17.         return super.onStartCommand(intent, flags, startId);  
  18.     }  
public int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("OnStartCommand");handler = new Handler(Looper.getMainLooper());handler.post(new Runnable(){public void run() {// TODO Auto-generated method stubSystem.out.println("in run");Toast toast = Toast.makeText(getApplicationContext(),"onStartCommand",Toast.LENGTH_SHORT);toast.setGravity(Gravity.TOP,0, 220);toast.show();}});return super.onStartCommand(intent, flags, startId);}

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击