Android 定时器
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 定时器的更多相关文章
- Android定时器,推荐ScheduledThreadPoolExecutor
Android定时器,推荐ScheduledThreadPoolExecutor 官方网址:http://developer.android.com/reference/java/util/Timer ...
- android 定时器AlarmManager
1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...
- android 定时器的实现
在Android上常用的定时器有两种,一种是Java.util.Timer,一种就是系统的AlarmService了. 实验1:使用Java.util.Timer. 在onStart()创创建Time ...
- android 定时器的使用
1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...
- Android 定时器TimerTask 简单使用
Android平台中需要反复按周期执行方法可以使用Java上自带的TimerTask类,TimerTask相对于Thread来说对于资源 消耗的更低,除了使用Android自带的AlarmManage ...
- Android 定时器实现的几种方式和removeCallbacks失效问题详解
实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + Runnable的方式 Handler handler = new Handler(); Runnable runna ...
- Android定时器功能实现方法
在Android开发中,定时器一般有以下3种实现方法: 1.采用Handler与线程的sleep(long)方法 2.采用Handler的postDelayed(Runnable, long)方法 3 ...
- 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错
原文网址:http://blog.csdn.net/xiaanming/article/details/9011193 实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + ...
- android 定时器总结
1:handler实现定时器的功能 Handler handler=new Handler(); //立即执行Runnable对象 public final boolean post(Runnab ...
- 【转】 Android定时器
转载自:http://www.android-study.com/pingtaikaifa/508.html 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sl ...
随机推荐
- 采用TL026等构成的宽带ALC放大器电路图
Building a Differential Amplifier An op-amp with no feedback is already a differential amplifier, am ...
- SqlServer2008R2用Windows身份登录18456错误解决
// 重装系统后发现SqlServer2008R2使用Windows身份验证不能进行连接,如下图: 以前经常会碰到SqlServer 身份验证连接失败,Windows身份验证的连接失败还是第一次,试了 ...
- Android webView打不开网页的解决办法
在我们开发过程中,有可能会遇到webview有些网页打不开的问题.这可能是设置的不对,下面就是解决办法. 进行如下设置吧,大多数情况都能解决! displayWebview.getSettings() ...
- Scala 深入浅出实战经典 第63讲:Scala中隐式类代码实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 前端测试回顾及我们为什么选择Karma
前端测试,或者UI测试一直是业界一大难题.最近Q.js使用Karma作为测试任务管理工具,本文在回顾前端测试方案的同时,也分析下为什么Q.js选用Karma而不是其他测试框架. 像素级全站对比 曾今有 ...
- 高并发WCF配置
在使用WCF做服务接口时,TCP模式肯定比Http效率高,Binary/MTOM格式的绑定也会Text格式的绑定高效. 两个endpoint,一个用来调试:ms-mex的binding是用来方便WCF ...
- Android Screen Orientation Change (Screen Rotation) Example
原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#
- sql 查询 – left join on
1. 问题引入 主要是为了查询在一个表中出现,而不在另一个表中出现的数据,具体来说: 如下图所示, 有A.B两个表,其中B表的Aid字段参照A表的主键id,为了查询在A表中出现,却没有被B表引 ...
- WebDriver测试EXT控件(基于C#)
WebDriver测试EXT控件(基于C#)http://www.docin.com/p-748096409.html
- 【Android】如何实现ButterKnife
如何实现ButterKnife ButterKnife的原理简述 示例代码ButterKnifeProcedure Pluggable Annotation Processing 注解处理器 Java ...