标签:main toast port 如何 log oid new t nal import
import android.content.Context; import android.os.Handler; import android.os.Looper; import android.widget.Toast; /** * Created by zhengping on 2017/1/15,11:26. */ public class ToastUtils { //sHandler维护的是主线程的消息队列 private static Handler sHandler = new Handler(Looper.getMainLooper()); //无论如何,Toast都是在主线程中被调用的 public static void showToast(final Context context,final String msg) { //判断是否是主线程 if(Looper.myLooper() == Looper.getMainLooper()) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } else { //handler.sendMessage(msg) 把一个消息发送到了消息队列,这个消息队列属于主线程的呢还是子线程的呢,取决于handler维护的是哪一个线程的消息队列 // 将runnable丢到主线程的消息队列 sHandler.post(new Runnable() { @Override public void run() { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }); } /*new Thread(new Runnable() { @Override public void run() { Looper.prepare(); //handler维护的是子线程的消息队列 Handler handler = new Handler(); Looper.loop(); } }).start();*/ } }
标签:main toast port 如何 log oid new t nal import
原文地址:http://www.cnblogs.com/loaderman/p/6435200.html