[Java]View Plaincopyprint?
- Public void showtoast (String msg) {
- Looper.prepare ();
- Toast.maketext (Getapplicationcontext (), MSG, Toast.length_short). Show ();
- Looper.loop ();
- }
public void Showtoast (String msg) {looper.prepare (); Toast.maketext (Getapplicationcontext (), MSG, Toast.length_short). Show (); Looper.loop ();}
Just add those two sentences to show toast in a non-UI thread
[Java]View Plaincopyprint?
- The show () inside the toast
- public void Show () {
- ...
- Service.enqueuetoast (pkg, TN, mduration); //Put this toast into a queue
- ...
- }
Inside the toast Show () public Void Show () { ... Service.enqueuetoast (Pkg, TN, mduration); Insert this toast into a queue ... }
[Java]View Plaincopyprint?
- Looper
- Public static final void prepare () {
- if (sthreadlocal.get () = null) {
- throw new runtimeexception ("Only one Looper could be Created per thread ");
- }
- Sthreadlocal.set (new Looper ()); //Create a looper in the current thread
- }
- Private Looper () {
- Mqueue = New MessageQueue (); //Key here, create what looper have done. is actually creating a message queue
- Mrun = true ;
- Mthread = Thread.CurrentThread ();
- }
Looperpublic static final void prepare () { if (sthreadlocal.get () = null) { throw new RuntimeException ("only one Looper may created per thread "); Sthreadlocal.set (New Looper ()); Creates a Looper }private Looper () { mqueue = new MessageQueue () in the current thread; The key here is to create what looper have done. In fact, the message queue is created Mrun = true; Mthread = Thread.CurrentThread (); }
Summary: The prerequisites for Toast display:
1:toast display needs to appear in a thread's message queue .... Very discreet.
Reproduced in: http://blog.csdn.net/xiaanming/article/details/8904645
Android displays toast in a non-UI thread