Skip to content

Instantly share code, notes, and snippets.

@Haldir65
Created February 21, 2016 12:07
Show Gist options
  • Save Haldir65/08fbcef5c6140245bb8a to your computer and use it in GitHub Desktop.
Save Haldir65/08fbcef5c6140245bb8a to your computer and use it in GitHub Desktop.
常用Utils类
import java.text.SimpleDateFormat;
import java.util.Date;
import android.util.Log;
public class LogUtil {
private static final boolean DEBUG = true;
public static void d(String TAG, String method, String msg) {
Log.d(TAG, "[" + method + "]" + msg);
}
public static void d(String TAG, String msg){
if (DEBUG) {
Log.d(TAG, "[" + getFileLineMethod() + "]" + msg);
}
}
public static void d(String msg){
if (DEBUG) {
Log.d(_FILE_(), "[" + getLineMethod() + "]" + msg);
}
}
public static void e(String msg){
if (DEBUG) {
Log.e(_FILE_(), getLineMethod() + msg);
}
}
public static void e(String TAG, String msg){
if (DEBUG) {
Log.e(TAG, getLineMethod() + msg);
}
}
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
StringBuffer toStringBuffer = new StringBuffer("[")
.append(traceElement.getFileName()).append(" | ")
.append(traceElement.getLineNumber()).append(" | ")
.append(traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
public static String getLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
StringBuffer toStringBuffer = new StringBuffer("[")
.append(traceElement.getLineNumber()).append(" | ")
.append(traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
return traceElement.getFileName();
}
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}
}
public class ToastUtil {
private static Toast toast;
public static void showTextLong(Context context, String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
} else {
toast.setText(text);
toast.setDuration(Toast.LENGTH_LONG);
}
toast.show();
}
public static void showTextShort(Context context, String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
}
public static void cancelToast() {
if (toast != null) {
toast.cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment