itgoyo / AndroidSummary

12 stars 4 forks source link

AndroidStudio自定义代码块Live Tamplates #64

Open itgoyo opened 6 years ago

itgoyo commented 6 years ago

有时候为了方便输入一些常用的代码块,所以设置一些关键字之后比较方便的输出想要的结果,所以使用这个功能,提高编码效率 Setting-Live Tamplates-+-第一个框框就是输入的关键字,第二个框框是定义说明

itgoyo commented 6 years ago
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * author: itgoyo
 * github: https://github.com/itgoyo
 * time: 2018/10/10 10:55.
 * desc: Log统一管理工具
 * email: itgoyo@gmail.com
 */

public class LogUtils {

    private LogUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean isDebug = AppConfig.IS_DEBUG;// 是否需要打印bug,可以在application的onCreate函数里面初始化
    private static final String TAG = "itgoyo";

    // 下面四个是默认tag的函数
    public static void i(String msg) {
        if (isDebug)
            Log.i(TAG, msg);
    }

    public static void d(String msg) {
        if (isDebug)
            Log.d(TAG, msg);
    }

    public static void e(String msg) {
        if (isDebug)
            Log.e(TAG, msg);
    }

    public static void v(String msg) {
        if (isDebug)
            Log.v(TAG, msg);
    }

    public static void w(String msg) {
        if (isDebug)
            Log.w(TAG, msg);
    }

    // 下面是传入自定义tag的函数
    public static void i(String tag, String msg) {
        if (isDebug)
            Log.i(tag, msg);
    }

    public static void d(String tag, String msg) {
        if (isDebug)
            Log.d(tag, msg);
    }

    public static void e(String tag, String msg) {
        if (isDebug)
            Log.e(tag, msg);
    }

    public static void v(String tag, String msg) {
        if (isDebug)
            Log.v(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (isDebug)
            Log.w(tag, msg);
    }

    /**
     * 错误
     *
     * @param clazz
     * @param msg
     */
    public static void e(Class<?> clazz, String msg) {
        if (isDebug) {
            Log.e(clazz.getSimpleName(), msg);
        }
    }

    /**
     * 信息
     *
     * @param clazz
     * @param msg
     */
    public static void i(Class<?> clazz, String msg) {
        if (isDebug) {
            Log.i(clazz.getSimpleName(), msg);
        }
    }

    /**
     * 警告
     *
     * @param clazz
     * @param msg
     */
    public static void w(Class<?> clazz, String msg) {
        if (isDebug) {
            Log.w(clazz.getSimpleName(), msg);
        }
    }

    /**
     * 写入文件
     */
    public static void writeLog(String log) {
        if (!isDebug) {
            return;
        }
        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/talk915.txt";
        FileWriter writer = null;
        try {
            File file = new File(path);

            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new FileWriter(file, true);
            writer.write(log);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}