ToastUtil.java
2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.cnlive.gundam.user.util;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.cnlive.gundam.R;
import org.jivesoftware.smack.util.StringUtils;
/**
* Created by xiansong on 2016/12/12.
*/
public class ToastUtil {
private static Toast toast;
private static long back_pressed;
private static String show_msg = "";
private static Toast current_toast;
private static final int TOAST_LAYOUT_ID = R.layout.test_layout_toast;
public static void hideToast() {
if (current_toast != null) current_toast.cancel();
}
public static Toast show(Context context, String msg) {
// if (!ShockwaveApplication.appOnline)
// return;
hide();
if (null == context || StringUtils.isEmpty(msg))
return null;
if (back_pressed + 2000 < System.currentTimeMillis() || !show_msg.equals(msg)) {
hideToast();
show_msg = msg;
toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(TOAST_LAYOUT_ID, null, false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
TextView textView = (TextView) view.findViewById(R.id.text);
imageView.setVisibility(View.GONE);
textView.setText(msg);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
current_toast = toast;
}
back_pressed = System.currentTimeMillis();
return toast;
}
public static boolean checkNetwork(Context context) {
if (context == null) return false;
if (NetworkUtil.isConnectInternet(context.getApplicationContext())) {
return true;
} else {
show(context.getApplicationContext(), context.getString(R.string.net_connect_toast));
return false;
}
}
public static void showToast(Context context, String text) {
show(context,text);
}
public static void hide() {
if (toast != null) toast.cancel();
}
}