Android基于AlarmManager實(shí)現(xiàn)用戶在線心跳功能示例
本文實(shí)例講述了Android基于AlarmManager實(shí)現(xiàn)用戶在線心跳功能。分享給大家供大家參考,具體如下:
在做即時(shí)通信或者其他檢測(cè)是否在線等操作時(shí)要用到心跳。比較常用的是AlarmManager全局定時(shí)器 去實(shí)現(xiàn)。
AlarmManager的使用機(jī)制有的稱呼為全局定時(shí)器,有的稱呼為鬧鐘。其實(shí)它的作用和Timer有點(diǎn)相似。都有兩種相似的用法:(1)在指定時(shí)長(zhǎng)后執(zhí)行某項(xiàng)操作(2)周期性的執(zhí)行某項(xiàng)操作
AlarmManager對(duì)象配合Intent使用,可以定時(shí)的開啟一個(gè)Activity,發(fā)送一個(gè)BroadCast,或者開啟一個(gè)Service.
下面的代碼詳細(xì)的介紹了兩種定時(shí)方式的使用:
(1)在指定時(shí)長(zhǎng)后執(zhí)行某項(xiàng)操作
//操作:發(fā)送一個(gè)廣播,廣播接收后Toast提示定時(shí)操作完成 Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("short");
PendingIntent sender= PendingIntent.getBroadcast(Main.this, 0, intent, 0);
//設(shè)定一個(gè)五秒后的時(shí)間
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//或者以下面方式簡(jiǎn)化
//alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
Toast.makeText(Main.this, "五秒后alarm開啟", Toast.LENGTH_LONG).show();
注意:receiver記得在manifest.xml注冊(cè)
public static class alarmreceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("short")){
Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "repeating alarm", Toast.LENGTH_LONG).show();
}
}
}
(2)周期性的執(zhí)行某項(xiàng)操作
Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent
.getBroadcast(Main.this, 0, intent, 0);
//開始時(shí)間
long firstime=SystemClock.elapsedRealtime();
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); //5秒一個(gè)周期,不停的發(fā)送廣播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 5*1000, sender);
AlarmManager的setRepeating()相當(dāng)于Timer的Schedule(task,delay,peroid);有點(diǎn)差異的地方是Timer這個(gè)方法是指定延遲多長(zhǎng)時(shí)間以后開始周期性的執(zhí)行task;
AlarmManager的取消:(其中需要注意的是取消的Intent必須與啟動(dòng)Intent保持絕對(duì)一致才能支持取消AlarmManager)
Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent.getBroadcast(Main.this, 0, intent, 0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android10 隱藏SystemUI鎖屏下的多用戶圖標(biāo)的示例代碼
- Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
- Android實(shí)現(xiàn)用戶圓形頭像和模糊背景
- Android實(shí)現(xiàn)簡(jiǎn)單用戶注冊(cè)案例
- Android啟動(dòng)頁(yè)用戶相關(guān)政策彈框的實(shí)現(xiàn)代碼
- 詳解Android Studio實(shí)現(xiàn)用戶登陸界面demo(xml實(shí)現(xiàn))
- android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- Android權(quán)限如何禁止以及友好提示用戶開通必要權(quán)限詳解
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android模擬用戶點(diǎn)擊的實(shí)現(xiàn)方法
- Android EditText 監(jiān)聽用戶輸入完成的實(shí)例
- Android 用戶Session管理的設(shè)計(jì)方案
- Android 多用戶詳情
相關(guān)文章
Android中ScrollView嵌套GridView的解決辦法
前些日子在開發(fā)中用到了需要ScrollView嵌套GridView的情況,由于這兩款控件都自帶滾動(dòng)條,當(dāng)他們碰到一起的時(shí)候便會(huì)出問(wèn)題,即GridView會(huì)顯示不全。下面小編給大家分享下解決方案,需要的朋友可以參考下2017-04-04
Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了添加數(shù)據(jù)到Intent的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Android中實(shí)現(xiàn)TCP和UDP傳輸實(shí)例
這篇文章主要介紹了Android中實(shí)現(xiàn)TCP和UDP傳輸實(shí)例,本文給出了TCP服務(wù)器端代碼、TCP客戶端代碼、UDP服務(wù)器端代碼、UDP客戶端代碼等代碼實(shí)例,需要的朋友可以參考下2015-03-03
Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android開發(fā) -- 控件的顯示與隱藏 setVisibility View.VISIBLE View.INVISI
本文簡(jiǎn)單介紹在Android開發(fā)中控件的顯示與隱藏幾種常見的屬性,給大家一個(gè)參考,希望對(duì)大家學(xué)習(xí)有所幫助。2016-06-06
Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android多線程斷點(diǎn)續(xù)傳下載實(shí)現(xiàn)代碼
這篇文章主要介紹了Android多線程斷點(diǎn)續(xù)傳下載實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

