android要實(shí)現定時(shí)的功能那肯定就要用到鬧鈴相關(guān)的技術(shù),
那么android鬧鈴實(shí)現是基于 AlarmManager 這個(gè)類(lèi)的,首先我們來(lái)看一下它的幾個(gè)主要的方法。
打開(kāi)AlarmManager的源碼,如下圖顯示:
兩個(gè)核心的方法 :
- private final IAlarmManager mService;
- public void set(int type, long triggerAtMillis, PendingIntent operation) {
- try {
- mService.set(type, triggerAtMillis, operation);
- } catch (RemoteException ex) {
- }
- }
和
- public void setRepeating(int type, long triggerAtMillis,
- long intervalMillis, PendingIntent operation) {
- try {
- mService.setRepeating(type, triggerAtMillis, intervalMillis, operation);
- } catch (RemoteException ex) {
- }
- }
第一個(gè)方法主要功能是注冊一個(gè)比較簡(jiǎn)單的鬧鈴,第二個(gè)方法是注冊一個(gè)重復的鬧鈴,這里重復的意思是指:設置5s, 那么每隔5s會(huì )執行一次 。
我們看到這里具體的實(shí)現都是基于IAlarmManager的,而IAlarmManager是一個(gè)aidl(Android Interface definition language),具體的就不詳細介紹了,大家有興趣的可以自己研究一下。
下面我來(lái)看下set(int type, long triggerAtMillis, PendingIntent operation)方法是如何調用的:
- // 進(jìn)行鬧鈴注冊
- Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
- PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
-
- // 過(guò)10s 執行這個(gè)鬧鈴
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- calendar.add(Calendar.SECOND, 10);
-
- AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
- manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
看一下AlarmReceiver 的源碼:
- /**
- *
- * @ClassName: AlarmReceiver
- * @Description: 鬧鈴時(shí)間到了會(huì )進(jìn)入這個(gè)廣播,這個(gè)時(shí)候可以做一些該做的業(yè)務(wù)。
- * @author HuHood
- * @date 2013-11-25 下午4:44:30
- *
- */
- public class AlarmReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context, "鬧鈴響了, 可以做點(diǎn)事情了~~", Toast.LENGTH_LONG).show();
- }
-
- }
還有別忘了,AndroidManifest.xml中需要加入:
- <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" android:process=":remote">
運行之后,過(guò)10s 彈出 "鬧鈴響了,可以做點(diǎn)事情了~~", 說(shuō)明成功了。
ok,這個(gè)結果肯定不是我們想要的,我們想要的功能是每天定時(shí)提醒的功能,那么需要基于
setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation)
這個(gè)方法來(lái)實(shí)現,代碼如下:
- Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
- PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
-
- long firstTime = SystemClock.elapsedRealtime(); // 開(kāi)機之后到現在的運行時(shí)間(包括睡眠時(shí)間)
- long systemTime = System.currentTimeMillis();
-
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- // 這里時(shí)區需要設置一下,不然會(huì )有8個(gè)小時(shí)的時(shí)間差
- calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
- calendar.set(Calendar.MINUTE, mMinute);
- calendar.set(Calendar.HOUR_OF_DAY, mHour);
- calendar.set(Calendar.SECOND, 0);
- calendar.set(Calendar.MILLISECOND, 0);
- // 選擇的定時(shí)時(shí)間
- long selectTime = calendar.getTimeInMillis();
- // 如果當前時(shí)間大于設置的時(shí)間,那么就從第二天的設定時(shí)間開(kāi)始
- if(systemTime > selectTime) {
- Toast.makeText(MainActivity.this,"設置的時(shí)間小于當前時(shí)間", Toast.LENGTH_SHORT).show();
- calendar.add(Calendar.DAY_OF_MONTH, 1);
- selectTime = calendar.getTimeInMillis();
- }
- // 計算現在時(shí)間到設定時(shí)間的時(shí)間差
- long time = selectTime - systemTime;
- firstTime += time;
- // 進(jìn)行鬧鈴注冊
- AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
- manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
- firstTime, DAY, sender);
- Log.i(TAG,"time ==== " + time +", selectTime ===== "
- + selectTime + ", systemTime ==== " + systemTime +", firstTime === " + firstTime);
- Toast.makeText(MainActivity.this,"設置重復鬧鈴成功! ", Toast.LENGTH_LONG).show();
以上的思路大致是這樣的,首先根據設置的時(shí)間,算出當前時(shí)間離設置時(shí)間的時(shí)間差,加上這個(gè)時(shí)間差,我們就知道第一次提醒的時(shí)間,然后再設定重復的時(shí)間間隔,我們這里設置1天就可以了。
現在解釋一下setRepeating中第一個(gè)參數:
AlarmManager.RTC,硬件鬧鐘,不喚醒手機(也可能是其它設備)休眠;當手機休眠時(shí)不發(fā)射鬧鐘。
AlarmManager.RTC_WAKEUP,硬件鬧鐘,當鬧鐘發(fā)躰時(shí)喚醒手機休眠;
AlarmManager.ELAPSED_REALTIME,真實(shí)時(shí)間流逝鬧鐘,不喚醒手機休眠;當手機休眠時(shí)不發(fā)射鬧鐘。
AlarmManager.ELAPSED_REALTIME_WAKEUP,真實(shí)時(shí)間流逝鬧鐘,當鬧鐘發(fā)躰時(shí)喚醒手機休眠;
RTC鬧鐘和ELAPSED_REALTIME最大的差別就是前者可以通過(guò)修改手機時(shí)間觸發(fā)鬧鐘事件,后者要通過(guò)真實(shí)時(shí)間的流逝,即使在休眠狀態(tài),時(shí)間也會(huì )被計算。
以上摘自:http://hualang.iteye.com/blog/1304054
還有,我們這里是調用廣播的形式來(lái)實(shí)現的,也可以用service的方式來(lái)進(jìn)行實(shí)現,只要把PendingIntent改動(dòng)一下即可,相關(guān)代碼:
PendingIntent.getService(MainActivity.this, 0,new Intent(MainActivity.this,AlarmService.class), 0);
其他的均相同。
完整Demo下載:
http://download.csdn.net/detail/jakend/6612033
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。