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

Android中AlarmManager基本用法分析

 更新時(shí)間:2016年08月15日 09:50:01   作者:llyofdream  
這篇文章主要介紹了Android中AlarmManager基本用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了AlarmManager的基本類型、方法及簡(jiǎn)單使用示例,需要的朋友可以參考下

本文實(shí)例講述了Android中AlarmManager基本用法。分享給大家供大家參考,具體如下:

AlarmManager的作用文檔中的解釋是:在特定的時(shí)刻為我們廣播一個(gè)指定的Intent。簡(jiǎn)單的說(shuō)就是我們?cè)O(shè)定一個(gè)時(shí)間,然后在該時(shí)間到來(lái)時(shí),AlarmManager為我們廣播一個(gè)我們?cè)O(shè)定的Intent。

對(duì)應(yīng)AlarmManager更深層的了解可以參考:

http://chabaoo.cn/article/90491.htm

android提供了四種類型的鬧鐘:

① ELAPSED_REALTIME

在指定的延時(shí)過(guò)后,發(fā)送廣播,但不喚醒設(shè)備。

② ELAPSED_REALTIME_WAKEUP

在指定的演示后,發(fā)送廣播,并喚醒設(shè)備
延時(shí)是要把系統(tǒng)啟動(dòng)的時(shí)間SystemClock.elapsedRealtime()算進(jìn)去的,具體用法看代碼。

③ RTC

在指定的時(shí)刻,發(fā)送廣播,但不喚醒設(shè)備

④ RTC_WAKEUP

在指定的時(shí)刻,發(fā)送廣播,并喚醒設(shè)備

AlarmManager提供的方法:

1. void set(int type, long triggerAtTime, PendingIntent operation)
設(shè)置一個(gè)鬧鐘

2. void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
設(shè)置一個(gè)會(huì)重復(fù)的鬧鐘

3. void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
設(shè)置一個(gè)重復(fù)鬧鐘的不精確版本,它相對(duì)而言更節(jié)能(power-efficient)一些,因?yàn)橄到y(tǒng)可能會(huì)將幾個(gè)差不多的鬧鐘合并為一個(gè)來(lái)執(zhí)行,減少設(shè)備的喚醒次數(shù)。

內(nèi)置的幾個(gè)interval為:

INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY

如果你將其設(shè)為DAY,那么可能這一天中的所有鬧鐘都會(huì)被合并掉。

void cancel(PendingIntent operation)

取消一個(gè)設(shè)置的鬧鐘

void setTimeZone(String timeZone)

設(shè)置系統(tǒng)的默認(rèn)時(shí)區(qū)。需要android.permission.SET_TIME_ZONE權(quán)限

// 首先創(chuàng)建Receiver
public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();
  }
}

// manifest中申明,并不需要intent-filter,我們是明確指定發(fā)到哪個(gè)receiver的
<receiver android:name="yuan.receivers.AlarmReceiver" />

PendingIntent:簡(jiǎn)單的說(shuō)就是在Intent上在加個(gè)指定的動(dòng)作。Intent的話,我們還需要在執(zhí)行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個(gè)動(dòng)作包含在內(nèi)了,如PendingIntent.getBroadcast就包含了sendBroadcast的動(dòng)作。

5s后發(fā)送指定廣播

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
int requestCode = 0;
PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
    requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 5秒后發(fā)送廣播,只發(fā)送一次
int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;
alarmMgr.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendIntent);

5s后發(fā)送指定廣播,然后每個(gè)10秒重復(fù)發(fā)送廣播

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
int requestCode = 0;
PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
    requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 5秒后發(fā)送廣播,然后每個(gè)10秒重復(fù)發(fā)廣播。廣播都是直接發(fā)到AlarmReceiver的
int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;
int interval = 10 * 1000;
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, interval, pendIntent);

取消一個(gè)鬧鐘

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(),
    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 與上面的intent匹配(filterEquals(intent))的鬧鐘會(huì)被取消
alarmMgr.cancel(pendIntent);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論