欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)每天定時(shí)提醒功能

 更新時(shí)間:2017年04月10日 11:46:10   作者:F-凡  
本文主要介紹了Android每天定時(shí)提醒功能、定時(shí)功能、鬧鐘的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧

這個(gè)是設(shè)置定時(shí)提醒的功能,即設(shè)置幾點(diǎn)幾分后提醒,用的是給系統(tǒng)設(shè)置個(gè)時(shí)間點(diǎn),當(dāng)系統(tǒng)時(shí)間到達(dá)設(shè)置的時(shí)間點(diǎn)的時(shí)候就會(huì)給我們發(fā)送一個(gè)廣播,然后達(dá)到時(shí)間提醒功能

網(wǎng)上找了很多,遇到了很多坑,經(jīng)過(guò)摸索出來(lái)的,比如下面設(shè)置重復(fù)時(shí)間的第二個(gè)參數(shù),網(wǎng)上有很多說(shuō)是執(zhí)行提醒延時(shí)多少毫秒執(zhí)行,我用的刷了MIUI的三星手機(jī)測(cè)試怎么都不對(duì),經(jīng)過(guò)摸索測(cè)試才發(fā)現(xiàn),原來(lái)不是,原來(lái)那個(gè)參數(shù)是設(shè)定的時(shí)間點(diǎn)的毫秒值!好了,不多說(shuō),看代碼:

/**
 * 開(kāi)啟提醒
 */
 private void startRemind(){
   //得到日歷實(shí)例,主要是為了下面的獲取時(shí)間
   mCalendar = Calendar.getInstance();
   mCalendar.setTimeInMillis(System.currentTimeMillis());
   //獲取當(dāng)前毫秒值
   long systemTime = System.currentTimeMillis();
   //是設(shè)置日歷的時(shí)間,主要是讓日歷的年月日和當(dāng)前同步
   mCalendar.setTimeInMillis(System.currentTimeMillis());
   // 這里時(shí)區(qū)需要設(shè)置一下,不然可能個(gè)別手機(jī)會(huì)有8個(gè)小時(shí)的時(shí)間差
   mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
   //設(shè)置在幾點(diǎn)提醒 設(shè)置的為13點(diǎn)
   mCalendar.set(Calendar.HOUR_OF_DAY, 13);
   //設(shè)置在幾分提醒 設(shè)置的為25分
   mCalendar.set(Calendar.MINUTE, 25);
   //下面這兩個(gè)看字面意思也知道
   mCalendar.set(Calendar.SECOND, 0);
   mCalendar.set(Calendar.MILLISECOND, 0);
   //上面設(shè)置的就是13點(diǎn)25分的時(shí)間點(diǎn)
   //獲取上面設(shè)置的13點(diǎn)25分的毫秒值
   long selectTime = mCalendar.getTimeInMillis();

   // 如果當(dāng)前時(shí)間大于設(shè)置的時(shí)間,那么就從第二天的設(shè)定時(shí)間開(kāi)始
   if(systemTime > selectTime) {
     mCalendar.add(Calendar.DAY_OF_MONTH, 1);
   }
  //AlarmReceiver.class為廣播接受者
   Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
   PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
   //得到AlarmManager實(shí)例
   AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  //**********注意!!下面的兩個(gè)根據(jù)實(shí)際需求任選其一即可*********
   /**
   * 單次提醒
   * mCalendar.getTimeInMillis() 上面設(shè)置的13點(diǎn)25分的時(shí)間點(diǎn)毫秒值
   */
   am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pi);

   /**
   * 重復(fù)提醒
   * 第一個(gè)參數(shù)是警報(bào)類型;下面有介紹
   * 第二個(gè)參數(shù)網(wǎng)上說(shuō)法不一,很多都是說(shuō)的是延遲多少毫秒執(zhí)行這個(gè)鬧鐘,但是我用的刷了MIUI的三星手機(jī)的實(shí)際效果是與單次提醒的參數(shù)一樣,即設(shè)置的13點(diǎn)25分的時(shí)間點(diǎn)毫秒值
   * 第三個(gè)參數(shù)是重復(fù)周期,也就是下次提醒的間隔 毫秒值 我這里是一天后提醒
   */
   am.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), (1000 * 60 * 60 * 24), pi);
 }
/**
 * 關(guān)閉提醒
 */
 private void stopRemind(){
   Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
   PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
       intent, 0);
   AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
   //取消警報(bào)
   am.cancel(pi);
   Toast.makeText(this, "關(guān)閉了提醒", Toast.LENGTH_SHORT).show();
 }

上面第一個(gè)參數(shù)的詳解:

AlarmManager.RTC,硬件鬧鐘,不喚醒手機(jī)(也可能是其它設(shè)備)休眠;當(dāng)手機(jī)休眠時(shí)不發(fā)射鬧鐘。

AlarmManager.RTC_WAKEUP,硬件鬧鐘,當(dāng)鬧鐘發(fā)躰時(shí)喚醒手機(jī)休眠;

AlarmManager.ELAPSED_REALTIME,真實(shí)時(shí)間流逝鬧鐘,不喚醒手機(jī)休眠;當(dāng)手機(jī)休眠時(shí)不發(fā)射鬧鐘。

AlarmManager.ELAPSED_REALTIME_WAKEUP,真實(shí)時(shí)間流逝鬧鐘,當(dāng)鬧鐘發(fā)躰時(shí)喚醒手機(jī)休眠;

RTC鬧鐘和ELAPSED_REALTIME最大的差別就是前者可以通過(guò)修改手機(jī)時(shí)間觸發(fā)鬧鐘事件,后者要通過(guò)真實(shí)時(shí)間的流逝,即使在休眠狀態(tài),時(shí)間也會(huì)被計(jì)算。

然后寫廣播接受者:

public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    //當(dāng)系統(tǒng)到我們?cè)O(shè)定的時(shí)間點(diǎn)的時(shí)候會(huì)發(fā)送廣播,執(zhí)行這里
  }
}

最后別忘了給廣播接受者配置清單文件:

<receiver
  android:name=".receiver.AlarmReceiver"
  android:process=":remote"
  />

使用方法就直接調(diào)用啟動(dòng)提醒或關(guān)閉提醒就行

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論