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í)的開(kāi)啟一個(gè)Activity,發(fā)送一個(gè)BroadCast,或者開(kāi)啟一個(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開(kāi)啟", 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); //開(kāi)始時(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í)間以后開(kāi)始周期性的執(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開(kāi)發(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)限如何禁止以及友好提示用戶開(kāi)通必要權(quán)限詳解
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android模擬用戶點(diǎn)擊的實(shí)現(xiàn)方法
- Android EditText 監(jiān)聽(tīng)用戶輸入完成的實(shí)例
- Android 用戶Session管理的設(shè)計(jì)方案
- Android 多用戶詳情
相關(guān)文章
android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例
這篇文章主要介紹了android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例,大家參考使用2013-11-11Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例的相關(guān)資料,這里介紹了Webview加載網(wǎng)頁(yè)的方法及帶進(jìn)度的Drawable文件view_progress_webview的實(shí)現(xiàn),需要的朋友可以參考下2017-07-07Unity3D游戲引擎實(shí)現(xiàn)在Android中打開(kāi)WebView的實(shí)例
這篇文章主要介紹了Unity3D游戲引擎在Android中打開(kāi)WebView的實(shí)例,需要的朋友可以參考下2014-07-07Android?ViewPager你可能不知道的刷新操作分享
這篇文章主要為大家詳細(xì)介紹了Android中ViewPager你可能不知道的刷新操作,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-05-05