Android封装Toast工具类
/**
* Created by Chen Chuanwen on 2016/12/19.
* Toast工具类
*/
public class ToastUtils {
private static Handler handler = new Handler(Looper.getMainLooper());
private static Toast toast = null;
private static Object synObj = new Object();
/**
* Toast发送消息,默认Toast.LENGTH_SHORT
*
* @param act
* @param msg
*/
public static void showMessage(final Context act, final String msg) {
showMessage(act, msg, Toast.LENGTH_SHORT);
}
/**
* Toast发送消息,默认Toast.LENGTH_LONG
*
* @param act
* @param msg
*/
public static void showMessageLong(final Context act, final String msg) {
showMessage(act, msg, Toast.LENGTH_LONG);
}
/**
* Toast发送消息
*
* @param act
* @param msg
* @param len
*/
private static void showMessage(final Context act, final String msg,
final int len) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {
if (toast != null) {
toast.setText(msg);
toast.setDuration(len);
} else {
toast = Toast.makeText(act, msg, len);
}
toast.show();
}
}
});
}
}).start();
}
}