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); 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值