android學習筆記之AlarmManager

來源:互聯網
上載者:User

轉自:http://yuanzhifei89.iteye.com/blog/1131523

AlarmManager的作用文檔中的解釋是:在特定的時刻為我們廣播一個指定的Intent。簡單的說就是我們設定一個時間,然後在該時間到來時,AlarmManager為我們廣播一個我們設定的Intent。 

對應AlarmManager更深層的瞭解可以參考: 
http://jinguo.iteye.com/blog/799778 
android提供了四種類型的鬧鐘: 
❑ ELAPSED_REALTIME 
在指定的延時過後,發送廣播,但不喚醒裝置。 
❑ ELAPSED_REALTIME_WAKEUP 
在指定的示範後,發送廣播,並喚醒裝置 
延時是要把系統啟動的時間SystemClock.elapsedRealtime()算進去的,具體用法看代碼。 
❑ RTC 
在指定的時刻,發送廣播,但不喚醒裝置 
❑ RTC_WAKEUP 
在指定的時刻,發送廣播,並喚醒裝置 

AlarmManager提供的方法: 
❑ void set(int type, long triggerAtTime, PendingIntent operation) 
設定一個鬧鐘 
❑ void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) 
設定一個會重複的鬧鐘 
❑ void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) 
設定一個重複鬧鐘的不精確版本,它相對而言更節能(power-efficient)一些,因為系統可能會將幾個差不多的鬧鐘合并為一個來執行,減少裝置的喚醒次數。 
內建的幾個interval為: 
INTERVAL_FIFTEEN_MINUTES 
INTERVAL_HALF_HOUR 
INTERVAL_HOUR 
INTERVAL_HALF_DAY 
INTERVAL_DAY 
如果你將其設為DAY,那麼可能這一天中的所有鬧鐘都會被合并掉。 
❑ void cancel(PendingIntent operation) 
取消一個設定的鬧鐘 
❑ void setTimeZone(String timeZone) 
設定系統的預設時區。需要android.permission.SET_TIME_ZONE許可權 

Java代碼  

  1. // 首先建立Receiver  
  2. public class AlarmReceiver extends BroadcastReceiver {  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();  
  6.     }  
  7. }  
Xml代碼  
  1. // manifest中申明,並不需要intent-filter,我們是明確指定發到哪個receiver的  
  2. <receiver android:name="yuan.receivers.AlarmReceiver" />  


PendingIntent:簡單的說就是在Intent上在加個指定的動作。Intent的話,我們還需要在執行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個動作包含在內了,如PendingIntent.getBroadcast就包含了sendBroadcast的動作。 
5s後發送指定廣播 
Java代碼  

  1. AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  2. Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);  
  3. int requestCode = 0;  
  4. PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),  
  5.         requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  6. // 5秒後發送廣播,只發送一次  
  7. int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;  
  8. alarmMgr.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendIntent);  

5s後發送指定廣播,然後每個10秒重複發送廣播 
Java代碼  

  1. AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  2. Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);  
  3. int requestCode = 0;  
  4. PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),  
  5.         requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  6. // 5秒後發送廣播,然後每個10秒重複發廣播。廣播都是直接發到AlarmReceiver的  
  7. int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;  
  8. int interval = 10 * 1000;  
  9. alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, interval, pendIntent);  

取消一個鬧鐘 
Java代碼  

  1. AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  2. Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);  
  3. PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),  
  4.         0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  5. // 與上面的intent匹配(filterEquals(intent))的鬧鐘會被取消  
  6. alarmMgr.cancel(pendIntent);  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.