Android代碼中自己寫的一些工具類(整理)

這些都是在項目中會用到的一些代碼;當做備忘

整理者:Insomnia

因爲開發的時候,經常有些代碼會重複使用,故將其記錄下開。以免遺忘

看碼:

1.Log日誌管理類


import android.util.Log;

/**
 * Log 日誌工具類
 */
public class Logger {
    public static int LOG_LEVEL = 6;
    public static int ERROR = 1;
    public static int WARNING = 2;
    public static int INFO = 3;
    public static int DEBUG = 4;
    public static int VERBOSE = 5;

    public static void e(String tag, String message) {
        if (LOG_LEVEL > ERROR) {
            Log.e(tag, message);
        }
    }

    public static void w(String tag, String message) {
        if (LOG_LEVEL > WARNING) {
            Log.w(tag, message);
        }
    }

    public static void i(String tag, String message) {
        if (LOG_LEVEL > INFO) {
            Log.i(tag, message);
        }
    }

    public static void d(String tag, String message) {
        if (LOG_LEVEL > DEBUG) {
            Log.d(tag, message);
        }
    }

    public static void v(String tag, String message) {
        if (LOG_LEVEL > VERBOSE) {
            Log.v(tag, message);
        }
    }
}

2.日期判斷工具類

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 對日期做出判斷
     * @param ordertime
     * @return
     */
    public static int compare_date(String ordertime) {
        Date date = null;
        DateFormat df = null;
        try {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = df.parse(ordertime);
            String nowTime = df.format(new Date());
            String orderNextTime = orderNextTime(Calendar.getInstance(), date);
            Date dt2 = df.parse(nowTime);
            Date dt3 = df.parse(orderNextTime);
            if (dt2.getTime() <= dt3.getTime()) {
                return 1;  //正常
            } else {
                return 0;  // 超期
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 回退一天時間
     * @param ca
     * @param date
     * @return
     */
    private static String orderNextTime(Calendar ca, Date date) {
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, -2 * 12);
        String sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ca.getTime());
        return sb.toString();
    }

    /**
     * 獲得以前的時間
     * @param ca
     * @param insertTime
     * @return
     */
    public static String getOldTime(Calendar ca, String insertTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(insertTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, -12 * 3 * 2);
        String sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ca.getTime());
        return sb.toString();
    }


    /**
     * 獲取當前日期
     * @return 當前日期(年-月-日 小時:分鐘:秒)
     */
    public static String getNowTime() {
        DateFormat df = null;
        String nowTime = null;
        try {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            nowTime = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowTime;

    }

    // 只獲取年月日
    public static String getYMD() {
        DateFormat df = null;
        String nowTime = null;
        try {
            df = new SimpleDateFormat("yyyy-MM-dd");
            nowTime = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowTime;

    }

    /**
     * 將String日期轉換爲Date格式的
     * @param str 日期
     * @return date格式的時間
     */
    public static Date getFormatTime(String str) {
        DateFormat df = null;
        Date date = null;
        try {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }

}

3.判斷網絡狀態工具類


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetCheck {
    /**
     * 判斷是否有網絡
     * @param context 
     * @return true:有網絡;false:無網絡
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            Log.i("NetWorkState", "Unavailabel");
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        Log.i("NetWorkState", "Availabel");
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

4.倒計時工具類

package com.weijia.community.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.widget.TextView;

/*定義一個倒計時的內部類*/
public class MyCount extends CountDownTimer {
    private TextView button;
    private IsFinishListener isFinishListener;
    private Context context;
    private long millisUntilFinished1;
    private boolean isFromTask = false;

    public MyCount(long millisInFuture, long countDownInterval, TextView getCode, Context context, boolean isFromTask) {
        super(millisInFuture, countDownInterval);
        this.button = getCode;
        this.context = context;
        this.isFromTask = isFromTask;
    }

    @Override
    public void onFinish() {
        this.isFinishListener.FinishChange();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        millisUntilFinished1 = millisUntilFinished;
        button.setTextColor(Color.parseColor("#A4A4A4"));

        button.setClickable(false);
        if (this.isFromTask == false) {
            button.setText(millisUntilFinished / 1000 + "秒後重新獲取");
        } else {
            button.setText("剩餘" + millisUntilFinished / 1000 + "秒");
        }
    }

    // 實現該接口,使用全部被選擇
    public interface IsFinishListener {

        void FinishChange();
    }

    public void setIsFinishListener(IsFinishListener isFinishListener) {
        this.isFinishListener = isFinishListener;
    }

}

5.dp轉px,px轉dp

//dp-->px
public int dp2px(Context context, float dp) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (dp * scale + 0.5f); 
} 

//px-->dp
public int px2dp(Context context, float px) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (px / scale + 0.5f); 
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章