網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android實(shí)現(xiàn)定時任務(wù)功能的具體代碼,供大家參考,具體內(nèi)容如下
1、采用Handle與線程的sleep(long)方法
(1)、定義一個Handler類,用于處理接受到的Message。
Handler handler = new Handler() { ? ? ? public void handleMessage(Message msg) { ? ? ? ? ? // 要做的事情 ? ? ? ? ? super.handleMessage(msg); ? ? ? } ? };
(2)、新建一個實(shí)現(xiàn)Runnable接口的線程類,如下:
public class MyThread implements Runnable { ? ? ? @Override ? ? ? public void run() { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? while (true) { ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? Thread.sleep(10000);// 線程暫停10秒,單位毫秒 ? ? ? ? ? ? ? ? ? Message message = new Message(); ? ? ? ? ? ? ? ? ? message.what = 1; ? ? ? ? ? ? ? ? ? handler.sendMessage(message);// 發(fā)送消息 ? ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? } ? }
(3)、在需要啟動線程的地方加入下面語句:
new Thread(new MyThread()).start(); ?
2、采用Handler的postDelayed(Runnable, long)方法
(1)、定義一個Handler類
Handler handler=new Handler(); ? ? Runnable runnable=new Runnable() { ? ? ? @Override ? ? ? public void run() { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? //要做的事情 ? ? ? ? ? handler.postDelayed(this, 2000); ? ? ? } ? };
(2)、啟動
handler.postDelayed(runnable, 2000);
3、用Handler與timer及TimerTask結(jié)合的方法
(1)、定義定時器、定時器任務(wù)及Handler句柄
private final Timer timer = new Timer(); ? private TimerTask task; ? Handler handler = new Handler() { ? ? ? @Override ? ? ? public void handleMessage(Message msg) { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? // 要做的事情 ? ? ? ? ? super.handleMessage(msg); ? ? ? } ? };
(2)、初始化計(jì)時器任務(wù)
task = new TimerTask() { ? ? ? @Override ? ? ? public void run() { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? Message message = new Message(); ? ? ? ? ? message.what = 1; ? ? ? ? ? handler.sendMessage(message); ? ? ? } ? };
(3)、啟動和關(guān)閉定時器
timer.schedule(task, 2000, 3000); ?? timer.cancel();
4、采用AlarmManger實(shí)現(xiàn)長期精確的定時任務(wù)
(1)、服務(wù)類:
public class HorizonService extends Service { ? ? @Override ? ? public IBinder onBind(Intent intent) { ? ? ? ? return null; ? ? } ? ? @Override ? ? public int onStartCommand(Intent intent, int flags, int startId) { ? ? ? ? new Thread(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? Log.d("TAG", "打印時間: " + new Date(). ? ? ? ? ? ? ? ? ? ? ? ? toString()); ? ? ? ? ? ? } ? ? ? ? }).start(); ? ? ? ? AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); ? ? ? ? int five = 5000; // 這是5s ? ? ? ? long triggerAtTime = SystemClock.elapsedRealtime() + five; ? ? ? ? Intent i = new Intent(this, AlarmReceiver.class); ? ? ? ? PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0); ? ? ? ? manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); ? ? ? ? return super.onStartCommand(intent, flags, startId); ? ? } }
(2)、廣播接受器
public class AlarmReceiver extends BroadcastReceiver { ? ? @Override ? ? public void onReceive(Context context, Intent intent) { ? ? ? ? Intent i = new Intent(context, HorizonService.class); ? ? ? ? context.startService(i); ? ? } }
(3)、啟動定時任務(wù):
Intent intent = new Intent(this,HorizonService.class); startService(intent);
原文鏈接:https://blog.csdn.net/weixin_42600398/article/details/104325308
相關(guān)推薦
- 2022-06-08 基于Springboot實(shí)現(xiàn)專業(yè)認(rèn)證材料管理系統(tǒng)
- 2023-05-12 Python時間戳轉(zhuǎn)換為字符串與字符串轉(zhuǎn)換為時間戳_python
- 2022-06-30 python神經(jīng)網(wǎng)絡(luò)Xception模型復(fù)現(xiàn)詳解_python
- 2022-10-13 redux持久化之redux-persist結(jié)合immutable使用問題_React
- 2022-10-07 Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫操作指南_C 語言
- 2022-04-09 SpringBoot默認(rèn)日志框架(slf4j)的使用以及配置文件
- 2023-03-03 C++?高精度乘法運(yùn)算的實(shí)現(xiàn)_C 語言
- 2022-10-10 React路由組件三種傳參方式分析講解_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支