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. zz Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位)

    Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位) Posted on 2015年01月28日 by 虾虾 22 Comments   最新的Windows 10 MSD ...

  2. Android 中的AIDL,Parcelable和远程服务

    Android 中的AIDL,Parcelable和远程服务      早期在学习期间便接触到AIDL,当时对此的运用也是一撇而过.只到近日在项目中接触到AIDL,才开始仔细深入.AIDL的作用    ...

  3. 奇怪吸引子---ShimizuMorioka

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  4. Eclipse中web项目缓存路径

    eclipse运行web项目后, 默认保存到 workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps ecli ...

  5. PHP查看SSL证书信息

    <? $str = file_get_contents('2.cer'); print_r(openssl_x509_parse($str)); ?> 证书需要使用base64编码的方式c ...

  6. U深度利用iso文件制作U盘启动盘

    利用U盘装win10系统: 工具:U深度装机版   文件:win10.iso 步骤1:下载U深度装机版安装 步骤2:打开U深度,制作U盘启动盘,注意选择iso模式,如下图所示 接下来下一步即可,工具会 ...

  7. android:style.xml

    <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Andr ...

  8. MVC使用基架添加控制器出现的错误:无法检索XXX的元数据

    环境 vs2012 框架 mvc3 数据库  sqlservercompact4.0 出现的错误如下: “ ---------------------------Microsoft Visual St ...

  9. ab 测试模块高并发

    转载:http://gekie.iteye.com/blog/1704235 作为程序员,写好一个模块后,不知道这个模块在高并发的情况下能不能平稳过渡,这里所说的平稳过渡是指,在高并发的情况下还能正常 ...

  10. memcached 源码阅读笔记

    阅读 memcached 最好有 libevent 基础, memcached 是基于 libevent 构建起来的. 通由 libevent 提供的事件驱动机制触发 memcached 中的 IO ...