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

