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 ...
随机推荐
- 未能加载文件或程序集“System.Data.SQLite.DLL”或它的某一个依赖项
今天在部署code到测试环境的时候 出现了未能加载文件或程序集"System.Data.SQLite.DLL"或它的某一个依赖项 这个错误,其实错误的的原因有很多,1.典型的是是版 ...
- Reservoir Sampling 蓄水池抽样算法,经典抽样
随机读取数据,如何保证真随机是不可能的,因为计算机的随机函数是伪随机的. 但是在不考虑计算机随机函数的情况下,如何保证数据的随机采样呢? 1.系统提供的shuffle函数 C++/Java都提供有sh ...
- Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和Index文件 (FST详细解析)
我们来看最复杂的部分,就是Term Dictionary和Term Index文件,Term Dictionary文件的后缀名为tim,Term Index文件的后缀名是tip,格式如图所示. Ter ...
- Ejabberd V.S Openfire
ejabberd Openfire homepage https://www.ejabberd.im/ http://www.igniterealtime.org/projects/openfire/ ...
- A little tutorial on CodeFluent Entities with ASP.NET MVC4
/* Author: Jiangong SUN */ CodeFluent Entities is a model-first development tool which creates non-s ...
- [Android实例] 有关spinner 的item问题 谁能给解答下??
[Android实例] 有关spinner 的item问题 (更多Android问题解决,Android开发讨论 请访问:http://www.eoeandroid.com/forum.php)
- <a>标签href属性与onclick事件
a标签主要用来实现页面跳转,可以通过href属性实现,也可以在onclick事件里实现. <a onclick="window.location.href='www.cnblogs.c ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- ECShop 添加文章时作者默认为当前登录用户
打开admin\article.php文件 查找代码 $article['is_open'] = 1; 在下边添加代码 $article['author'] = $_SESSION['admin_na ...
- Java对象创建阶段的代码调用顺序
在创建阶段系统通过下面的几个步骤来完成对象的创建过程 为对象分配存储空间 开始构造对象 从超类到子类对static成员进行初始化 超类成员变量按顺序初始化,递归调用超类的构造方法 子类成员变量按顺序初 ...