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. 给同为.NET开发者普及一点Oracle数据库经验

    前段时间,因为要给自己开发的搜易站内搜索引擎增加Oracle数据库的支持,所以学习了下Oracle的基础知识,发现使用方式跟MYSQL,MSSQL等数据库的思维有很大的不同,总结一下几点不同,希望给初 ...

  2. What is /proc/slabinfo?

    /proc/slabinfo gives information about memory usage on the slab level. Linux kernels uses slab pools ...

  3. Utopian Tree in Java

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during th ...

  4. javascript 搜索二叉树

    function Tree() { this.root = null; } Tree.prototype = { constructor: Tree, addItem: function(value) ...

  5. How to set colors of HTML tables

    There is a simple way to set the color of tables: use the bgcolor attribute of tag <table>.   ...

  6. MySQL中导入 导出CSV

    来自:http://blog.csdn.net/sara_yhl/article/details/6850107 导出 select * from test_info into outfile '/t ...

  7. Source Insight 使用

    1.括号配对高亮:“在前括号左侧,后括号左侧” 双击鼠标左键,可以选定匹配括号和其中内容(<>,(),L{R},[]之间) 2.让{ 和 } 不缩进:Options -> Docum ...

  8. JIT

    http://www.cppblog.com/vczh/category/9583.html

  9. WIN8 下Cisco VPN连接 出现vpn 422 failed to enable virtual adapter错误

    今天在家用VPN软件连接,出现了“vpn 422 failed to enable virtual adapter”的错误,系统安装的是Win8专业版32位,百度了半天又很多方法解决不了,后来发现了一 ...

  10. vxworks下网络编程一:网络字节序问题

    inet_addr("192.168.1.1");//返回网络字节序整型ip地址inet_ntoa(saddr);//将包含网络字节序整型ip地址的in_addr对象转换成本地ch ...