亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android鬧鐘機(jī)制實(shí)現(xiàn)定時任務(wù)功能

 更新時間:2020年06月19日 09:56:16   作者:ckdroid  
這篇文章主要為大家詳細(xì)介紹了Android鬧鐘機(jī)制實(shí)現(xiàn)定時任務(wù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android的鬧鐘實(shí)現(xiàn)機(jī)制, 需要調(diào)用AlarmManager.set()將鬧鈴時間記錄到系統(tǒng)中,當(dāng)鬧鈴時間到后,系統(tǒng)會給應(yīng)用程序發(fā)送廣播,我們只需要去注冊廣播接收器就可以了。

本文分三部分講解如何實(shí)現(xiàn)鬧鐘:

目錄:

1. 設(shè)置鬧鈴時間;
2. 接收鬧鈴事件廣播;
3. 重開機(jī)后重新計(jì)算并設(shè)置鬧鈴時間;

1. 設(shè)置鬧鈴時間(毫秒)

private void setAlarmTime(Context context, long triggerAtMillis) { 
  AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
  Intent intent = new Intent("android.alarm.demo.action"); 
  PendingIntent sender = PendingIntent.getBroadcast( 
    context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
  //鬧鈴間隔, 這里設(shè)為1分鐘鬧一次,在第2步我們將每隔1分鐘收到一次廣播 
  //int interval = 60 * 1000; 
  //am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender); 
  am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, sender); 
 } 

第二個參數(shù)它大致分為兩種類型 一種是相對時間 一種是絕對時間。

所以,根據(jù)使用的類型不同 triggerAtTime設(shè)置也有所不同。

如果使用ELAPSED_REALTIME_WAKEUP類型 應(yīng)該調(diào)用SystemClock.elapsedRealtime()獲取相對時間在加上你設(shè)定的延遲時間。

2. 接收鬧鈴事件廣播

public class AlarmReceiver extends BroadcastReceiver { 
 public void onReceive(Context context, Intent intent) { 
  if ("android.alarm.demo.action".equals(intent.getAction())) { 
   // 第1步中設(shè)置的鬧鈴時間到,這里可以彈出鬧鈴提示并播放響鈴 
   Toast.makeText(context, "hello alarm", Toast.LENGTH_LONG).show(); 
   System.out.println("hello alarm"); 
   // 可以繼續(xù)設(shè)置下一次鬧鈴時間; 
   return; 
  } 
 } 
} 

當(dāng)然,Receiver是需要在Manifest.xml中注冊的:

<receiver android:name="AlarmReceiver"> 
   <intent-filter> 
    <action android:name="android.alarm.demo.action" /> 
   </intent-filter> 
  </receiver> 

3. 重開機(jī)后重新計(jì)算并設(shè)置鬧鈴時間

當(dāng)然要有一個BootReceiver:

public class BootReceiver extends BroadcastReceiver { 
 public void onReceive(Context context, Intent intent) { 
  String action = intent.getAction(); 
  if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 
   //重新計(jì)算鬧鈴時間,并調(diào)第一步的方法設(shè)置鬧鈴時間及鬧鈴間隔時間 
  } 
 } 
} 

當(dāng)然,也需要注冊:

<receiver android:name="BootReceiver"> 
 <intent-filter> 
  <action android:name="android.intent.action.BOOT_COMPLETED" /> 
 </intent-filter> 
 </receiver> 

我在設(shè)置時鐘的時候遇到一點(diǎn)問題
我開始的代碼是這樣寫的

alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender); 

我的本意是設(shè)定五秒后啟動鬧鐘 但是每次都是我設(shè)置完鬧鐘之后立馬就啟動了。
后來我發(fā)現(xiàn)問題出在第二個參數(shù)上 我對他的理解是錯誤的

我之前以為它是“延遲”時間,而實(shí)際它是“啟動”時間。
要理解這個參數(shù)還要看type這個參數(shù)

public static final int ELAPSED_REALTIME 
 //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統(tǒng)啟動后開始計(jì)時的,包括睡眠時間,可以通過調(diào)用SystemClock.elapsedRealtime()獲得。系統(tǒng)值是3 (0x00000003)。 

public static final int ELAPSED_REALTIME_WAKEUP 
 //能喚醒系統(tǒng),用法同ELAPSED_REALTIME,系統(tǒng)值是2 (0x00000002) 。 

public static final int RTC 
 //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時,這種類型的鬧鈴不會喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調(diào)用 System.currentTimeMillis()獲得。系統(tǒng)值是1 (0x00000001) 。 
 
public static final int RTC_WAKEUP 
 //能喚醒系統(tǒng),用法同RTC類型,系統(tǒng)值為 0 (0x00000000) 。 

它大致分為兩種類型 一種是相對時間 一種是絕對時間。

所以,根據(jù)使用的類型不同 triggerAtTime設(shè)置也有所不同。

如果使用ELAPSED_REALTIME_WAKEUP類型 應(yīng)該調(diào)用SystemClock.elapsedRealtime()獲取相對時間在加上你設(shè)定的延遲時間。

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender); 

如果使用RTC_WAKEUP類型 應(yīng)該調(diào)用System.currentTimeMillis()獲取從1970.1.1號以來的時間在加上你設(shè)定的延遲時間

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender); 

setRepeating方法有4個參數(shù),這些參數(shù)的含義如下:

type:表示警報(bào)類型,一般可以取的值是AlarmManager.RTC和AlarmManager.RTC_WAKEUP。如果將type參數(shù)值設(shè)為AlarmManager.RTC,表示是一個正常的定時器,如果將type參數(shù)值設(shè)為AlarmManager.RTC_WAKEUP,除了有定時器的功能外,還會發(fā)出警報(bào)聲(例如,響鈴、震動)。

triggerAtTime:第1次運(yùn)行時要等待的時間,也就是執(zhí)行延遲時間,單位是毫秒。

interval:表示執(zhí)行的時間間隔,單位是毫秒。

operation:一個PendingIntent對象,表示到時間后要執(zhí)行的操作。PendingIntent與Intent類似,可以封裝Activity、BroadcastReceiver和Service。但與Intent不同的是,PendingIntent可以脫離應(yīng)用程序而存在。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論