当前位置: 移动技术网 > 移动技术>移动开发>Android > Android闹铃服务AlarmManager用法深入分析

Android闹铃服务AlarmManager用法深入分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android闹铃服务alarmmanager用法。分享给大家供大家参考,具体如下:

对应alarmmanage有一个alarmmanagerservie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,linux实现的设备名 为"/dev/alarm"),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,alarmmanagerservie服务程序就会遍历闹铃列 表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化闹铃设备(/dev/alarm)。当 然,在java层的alarmmanagerservice与linux alarm驱动程序接口之间还有一层封装,那就是jni。

alarmmanager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过alarmmanager来使用这种服务。这也许就是客户/服务模式的好处吧。alarmmanager与 alarmmanagerservie之间是通过binder来通信的,他们之间是多对一的关系。

在android系统中,alarmmanage提供了3个接口5种类型的闹铃服务。

3个接口:

// 取消已经注册的与参数匹配的闹铃
void cancel(pendingintent operation)
//注册一个新的闹铃
void set( int type, long triggerattime, pendingintent operation)
//注册一个重复类型的闹铃
void setrepeating( int type, long triggerattime, long interval, pendingintent operation)
//设置时区
void settimezone(string timezone)

java代码:

// 取消已经注册的与参数匹配的闹铃
void cancel(pendingintent operation)
 //注册一个新的闹铃
void set(int type, long triggerattime, pendingintent operation)
 //注册一个重复类型的闹铃
void setrepeating(int type, long triggerattime, long interval, pendingintent operation)
 //设置时区
void settimezone(string timezone)

5个闹铃类型

public static final int elapsed_realtime
// 当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时 间,可以通过调用systemclock.elapsedrealtime()获得。系统值是3 (0x00000003)。
public static final int elapsed_realtime_wakeup
//能唤醒系统,用法同elapsed_realtime,系统值是2 (0x00000002) 。
public static final int rtc
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是utc时间,可以通过调用 system.currenttimemillis()获得。系统值是1 (0x00000001) 。
public static final int rtc_wakeup
//能唤醒系统,用法同rtc类型,系统值为 0 (0x00000000) 。
public static final int power_off_wakeup
//能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同rtc类型,系统值为4(0x00000004)。

java代码 :

public static final int elapsed_realtime
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠
时间,可以通过调用systemclock.elapsedrealtime()获得。系统值是3 (0x00000003)。
public static final int elapsed_realtime_wakeup
//能唤醒系统,用法同elapsed_realtime,系统值是2 (0x00000002) 。
public static final int rtc
//当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是utc时间,可以通过调用
 system.currenttimemillis()获得。系统值是1 (0x00000001) 。
public static final int rtc_wakeup
//能唤醒系统,用法同rtc类型,系统值为 0 (0x00000000) 。
public static final int power_off_wakeup
//能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同rtc类型,系统值为
4(0x00000004)。

注意一个重要的参数pendingintent。这个pendingintent可以说是 intent的进一步封装,他既包含了intent的描述又是intent行为的执行(这种定义也许不太严格),如果将intent比作成一个订单的 话,pendingintent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送 失败后要重发还是取消订单等操作。开发者可以通过调用
getactivity(context, int, intent, int)
getbroadcast(context, int, intent, int)
getservice(context, int, intent, int)

三种不同方式来得到一个pendingintent实例。

getbroadcast——通过该函数获得的pendingintent将会 扮演一个广播的功能,就像调用 context.sendbroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建pendingintent的时候指定,也可以通过action 和category等描述让系统自动找到该行为处理对象。

intent intent = new intent(alarmcontroller. this , oneshotalarm. class );
pendingintent sender = pendingintent.getbroadcast(alarmcontroller.this , 0 , intent, 0 );

java代码:

intent intent = new intent(alarmcontroller.this, oneshotalarm.class);
pendingintent sender = pendingintent.getbroadcast(alarmcontroller.this, 0, intent, 0);

getactivity——通过该函数获得的pendingintent可以直 接启动新的activity, 就像调用 context.startactivity(intent)一样.不过值得注意的是要想这个新的activity不再是当前进程存在的activity 时。我们在intent中必须使用intent.flag_activity_new_task.

// the pendingintent to launch our activity if the user selects this notification
pendingintent contentintent = pendingintent.getactivity(this , 0 , new intent( this , alarmservice. class ), 0 );

java代码:

// the pendingintent to launch our activity if the user selects this notification
pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, alarmservice.class), 0);

getservice——通过该函数获得的pengdingintent可以直接启动新的service,就像调用context.startservice()一样。

// create an intentsender that will launch our service, to be scheduled
// with the alarm manager.
malarmsender = pendingintent.getservice(alarmservice.this ,
 0 , new intent(alarmservice. this , alarmservice_service. class ), 0 );

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网