package com.neusoft.util;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import java.lang.ref.WeakReference;

public class ToastUtil {
    static WeakReference<Toast> reference = null;

    private static Toast getToast() {
        return reference == null ? null : reference.get();
    }

    public static void showToast(Context context, CharSequence text) {
        showToast(context, text, Toast.LENGTH_SHORT);
    }

    public static void showToast(Context context, CharSequence text,
            int duration) {
        showToast(context, text, duration, Gravity.BOTTOM);
    }

    public static void showToast(Context context, CharSequence text,
            int duration, int gravity) {

        Toast toast = getToast();

        if (toast == null)
            toast = Toast.makeText(context, text, duration);
        else {
            toast.cancel();
            toast = Toast.makeText(context, text, duration);
        }

        toast.setGravity(gravity, toast.getXOffset(), toast.getYOffset());
        reference = new WeakReference<Toast>(toast);

        toast.show();

    }

    public static void showToast(Context context, int resId) {
        showToast(context, resId, Toast.LENGTH_SHORT);
    }

    public static void showToast(Context context, int resId, int duration) {
        showToast(context, context.getResources().getString(resId), duration,
                Gravity.BOTTOM);
    }

    public static void showToast(Context context, View view) {

        showToast(context, view, Toast.LENGTH_SHORT, Gravity.BOTTOM);
    }

    public static void showToast(Context context, View view, int duration) {

        showToast(context, view, duration, Gravity.BOTTOM);
    }

    public static void showToast(Context context, View view, int duration,
            int gravity) {

        Toast toast = getToast();

        if (toast == null)
            toast = new Toast(context);
        else {
            toast.cancel();
            toast = new Toast(context);
        }

        toast.setGravity(gravity, toast.getXOffset(), toast.getYOffset());
        reference = new WeakReference<Toast>(toast);
        toast.setView(view);
        toast.show();
    }
}

@@ -9,15 +9,21 @@
 com.
-neusoft
+stupid.method
 .uti
@@ -58,16 +58,69 @@
 ontext;%0A
+import android.os.Handler;%0Aimport android.os.Looper;%0A
 import a
@@ -233,16 +233,48 @@
 rence;%0A%0A
+/**%0A * @author wangx%0A * %0A * **/%0A
 public c
@@ -321,30 +321,82 @@
 e%3CToast%3E ref
-erence
+Toast = null;%0A    static WeakReference%3CHandler%3E refHandler
  = null;%0A%0A  
@@ -449,22 +449,21 @@
 turn ref
-erence
+Toast
  == null
@@ -475,22 +475,21 @@
 ll : ref
-erence
+Toast
 .get();%0A
@@ -951,33 +951,33 @@
   if (toast 
-=
+!
 = null)%0A
             
@@ -968,84 +968,8 @@
 ll)%0A
-            toast = Toast.makeText(context, text, duration);%0A        else %7B%0A
     
@@ -984,36 +984,32 @@
 toast.cancel();%0A
-    
         toast = 
@@ -1048,26 +1048,16 @@
 ration);
-%0A        %7D
 %0A%0A      
@@ -1128,38 +1128,37 @@
 ());%0A        ref
-erence
+Toast
  = new WeakRefer
@@ -1186,27 +1186,50 @@
         
-toast.show(
+showToastOnUiThread(context, toast
 );%0A%0A    
@@ -2222,22 +2222,21 @@
      ref
-erence
+Toast
  = new W
@@ -2304,21 +2304,727 @@
     
-toast.show();
+showToastOnUiThread(context, toast);%0A    %7D%0A%0A    /**%0A     * %E5%A6%82%E6%9E%9C%E5%A4%84%E4%BA%8EUI%E7%BA%BF%E7%A8%8B%E9%87%8C,%E5%88%99%E7%9B%B4%E6%8E%A5%E6%98%BE%E7%A4%BA,%E5%90%A6%E5%88%99 %E6%94%BE%E5%88%B0%EF%BC%B5%EF%BC%A9%E7%BA%BF%E7%A8%8B%E9%87%8C%E6%98%BE%E7%A4%BA%0A     */%0A    public static void showToastOnUiThread(Context context, final Toast toast) %7B%0A        if (Looper.getMainLooper().getThread().getId() == Thread%0A                .currentThread().getId())%0A            toast.show();%0A        else %7B%0A            Handler handler = refHandler.get();%0A            if (handler == null)%0A                refHandler = new WeakReference%3CHandler%3E(handler = new Handler(%0A                        context.getMainLooper()));%0A            handler.post(new Runnable() %7B%0A%0A                @Override%0A                public void run() %7B%0A                    toast.show();%0A                %7D%0A            %7D);%0A%0A        %7D
 %0A   

Update ToastUtil.java