Andorid定时器封装类

public class TimerUtil {
private static final String TAG = "TimerUtil"; private static Handler mTimeHandler = null;
private static boolean mTimerMark = false;
private static Runnable mTimerRunnable = null;
private static int mUpdateTime = 0; private TimerUtil() {
}; /**
* 定时器开始定时
*
* @param doThing 定时器处理事情
* @param updateTime 定时器时间
* @since V1.0
*/
public static void startTime(Runnable doThing, int updateTime) {
if (null == doThing || updateTime < 0) {
return;
} if (null == mTimeHandler) {
mTimeHandler = new Handler();
mTimerRunnable = doThing;
mUpdateTime = updateTime;
mTimerMark = true;
} if (mTimerMark) {
mTimeHandler.postDelayed(mTimerRunnable, 0);
} else {
mTimeHandler.postDelayed(null, 0);
}
} /**
* 定时器开始定时
*
* @param doThing 定时器处理事情
* @param updateTime 定时器时间
* @param mode 定时器模式
* @since V1.0
*/
public static void startTime(Runnable doThing, int updateTime, boolean mode) {
if (null == doThing || updateTime < 0) {
return;
} if (null == mTimeHandler) {
mTimeHandler = new Handler();
mTimerRunnable = doThing;
mUpdateTime = updateTime;
mTimerMark = true;
} if (mTimerMark) {
if (mode) {
mTimeHandler.postDelayed(mTimerRunnable, 0);
} else {
mTimeHandler.postDelayed(mTimerRunnable, updateTime);
}
} else {
mTimeHandler.postDelayed(null, 0);
}
} /**
* 这里对方法做描述
*
* @since V1.0
*/
public static void updataTime() {
if (mTimerMark) {
mTimeHandler.postDelayed(mTimerRunnable, mUpdateTime);
} else {
mTimeHandler.postDelayed(null, 0);
}
} /**
* 这里对方法做描述
*
* @since V1.0
*/
public static void stopTime() {
mTimerMark = false;
if (null != mTimeHandler) {
mTimeHandler.removeCallbacks(mTimerRunnable);
mTimeHandler = null;
}
} /**
* time格式 String型的日 如:昨天、今天、8月24日 15:30
* @param ltime
* @param context
* @return
* @since V1.0
*/
public synchronized static String getTime_FormatTime_MMDD(long ltime, Context context) {
String time = ""; Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTimeInMillis(ltime); Calendar targetCalendar = Calendar.getInstance();
targetCalendar.set(Calendar.HOUR_OF_DAY, 0);
targetCalendar.set(Calendar.MINUTE, 0); if (dateCalendar.after(targetCalendar)) {
time = context.getString(R.string.today) +" "+ time;
return time;
} else {
targetCalendar.add(Calendar.DATE, -1);
if (dateCalendar.after(targetCalendar)) {
time = context.getString(R.string.yesterday) +" "+ time;
return time;
}
} String otherSDF = context.getString(R.string.mmdd);
Date date = dateCalendar.getTime();
SimpleDateFormat sfd = new SimpleDateFormat(otherSDF);
time = sfd.format(date);
CLog.d(TAG, "getTime_FormatTime_MMDD time:"+time);
return time;
} /**
* time格式 String型的日 如:昨天、今天、8月24日 15:30
* @param ltime
* @param context
* @return
* @since V1.0
*/
public synchronized static String getTime_FormatTime_MMDDHHMM(long ltime, Context context) {
String time = ""; Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTimeInMillis(ltime); int month = dateCalendar.get(Calendar.MONTH) + 1;
int day = dateCalendar.get(Calendar.DAY_OF_MONTH);
int hour = dateCalendar.get(Calendar.HOUR_OF_DAY);
int minute = dateCalendar.get(Calendar.MINUTE); time = String.format("%02d:%02d", hour, minute); Calendar targetCalendar = Calendar.getInstance();
targetCalendar.set(Calendar.HOUR_OF_DAY, 0);
targetCalendar.set(Calendar.MINUTE, 0);
if (dateCalendar.after(targetCalendar)) {
time = context.getString(R.string.today) +" "+ time;
return time;
} else {
targetCalendar.add(Calendar.DATE, -1);
if (dateCalendar.after(targetCalendar)) {
time = context.getString(R.string.yesterday) +" "+ time;
return time;
}
} time = String.format(Locale.ENGLISH,"%02d-%02d %02d:%02d", month, day, hour, minute);
CLog.d(TAG, "getTime_FormatTime_MMDDHHMM time:"+time);
return time;
} /**
* 这里对方法做描述
*
* @return time格式 nnnn-mm-dd hh:mm:ss
* @since V1.0
*/
public synchronized static String getTime_nnnnyydd(long t) {
if (t == 0) {
return "";
} Calendar cale = Calendar.getInstance();
cale.setTimeInMillis(t);
int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH) + 1;
int day = cale.get(Calendar.DAY_OF_MONTH);
int hour = cale.get(Calendar.HOUR_OF_DAY);
int minute = cale.get(Calendar.MINUTE);
int second = cale.get(Calendar.SECOND); String time = String.format(Locale.ENGLISH, "%d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
CLog.d(TAG, "getTime_nnnnyydd time:"+time);
return time;
} /**
* 这里对方法做描述
*
* @return time格式 nnnn-mm-dd hh:mm:ss
* @since V1.0
*/
public synchronized static String getTime_nnnnyydd(Calendar cale) {
if (cale == null) {
return "";
} int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH) + 1;
int day = cale.get(Calendar.DAY_OF_MONTH);
int hour = cale.get(Calendar.HOUR_OF_DAY);
int minute = cale.get(Calendar.MINUTE);
int second = cale.get(Calendar.SECOND);
String time = String.format(Locale.ENGLISH,"%d-%02d-%02d %02d:%02d:%02d", year,month,day, hour, minute, second);
CLog.d(TAG, "getTime_nnnnyydd time:" + time);
return time;
}
}

Android 定时器的更多相关文章

  1. Android定时器,推荐ScheduledThreadPoolExecutor

    Android定时器,推荐ScheduledThreadPoolExecutor 官方网址:http://developer.android.com/reference/java/util/Timer ...

  2. android 定时器AlarmManager

    1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...

  3. android 定时器的实现

    在Android上常用的定时器有两种,一种是Java.util.Timer,一种就是系统的AlarmService了. 实验1:使用Java.util.Timer. 在onStart()创创建Time ...

  4. android 定时器的使用

    1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...

  5. Android 定时器TimerTask 简单使用

    Android平台中需要反复按周期执行方法可以使用Java上自带的TimerTask类,TimerTask相对于Thread来说对于资源 消耗的更低,除了使用Android自带的AlarmManage ...

  6. Android 定时器实现的几种方式和removeCallbacks失效问题详解

    实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + Runnable的方式 Handler handler = new Handler(); Runnable runna ...

  7. Android定时器功能实现方法

    在Android开发中,定时器一般有以下3种实现方法: 1.采用Handler与线程的sleep(long)方法 2.采用Handler的postDelayed(Runnable, long)方法 3 ...

  8. 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/9011193 实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + ...

  9. android 定时器总结

    1:handler实现定时器的功能 Handler handler=new Handler(); //立即执行Runnable对象   public final boolean post(Runnab ...

  10. 【转】 Android定时器

    转载自:http://www.android-study.com/pingtaikaifa/508.html 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sl ...

随机推荐

  1. Bitbucket Repository size limits

    Repository size limits By Justen Stepka, Product Manager on May 30, 2014 In order to improve and mai ...

  2. IOS适配

    • Default.png(图片尺寸为320x480):显示在非Retina-3.5英寸屏幕上(iPhone3G\iPhone3GS,屏幕分辨率为320x480) • Default@2x.png(图 ...

  3. Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了

    作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...

  4. 实现微信公众号自动登陆自己的Web App

    测试: 基本的思路是通过公众号OAuth API获取用户微信的openid.第一次使用的时候让用户登录,然后在数据库里把openid和自己应用的userid对应起来.以后获得用户的openid之后就可 ...

  5. 永久删除 tadb.exe

    系统安装qq手机管家等手机管理软件之后,在开机时会自动运行tadb.exe, 这个垃圾进程会让开发android程序时默认的 adb.exe启动不了,这样就没法用手机调试. 这并不是说tadb.exe ...

  6. MySQL存储引擎选型

    一.MySQL的存储引擎 完整的引擎说明还是看官方文档:http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html 这里介绍一些主要的引擎 ...

  7. 解放双手——Android自动化测试

    解放程序猿宝贵的右手(或者是左手) http://blog.csdn.net/eclipsexys/article/details/45622813 --Android自动化测试技巧 Google大神 ...

  8. Windows下Nginx的安装与配置(转)

    一.首先去官网下载 nginx1.0.11的Windows版本,官网下载:http://nginx.org/download/nginx-1.0.11.zip 下载到软件包后,解压 nginx-ngi ...

  9. 用node开发repl应用

    前言 每次看到一些库npm -g install xx然后,执行xx就可以跑起来,这不就是一个shell工具了吗,那么我不就可以不用学习shell语法,直接用js写命令行脚本了吗! 什么是REPL应用 ...

  10. android TypedValue.applyDimension()的作用

    这个方法是转变为标准尺寸的一个函数,例如 int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, cont ...