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

 更新时间:2016年08月15日 09:07:51   作者:loving863  
这篇文章主要介绍了Android闹铃服务AlarmManager用法,结合实例形式深入分析了闹铃服务AlarmManager的功能、原理、定义与使用方法,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • 深度剖析Android Binder IPC机制

    深度剖析Android Binder IPC机制

    Android系统的成功离不开其强大的IPC(Inter-Process Communication)机制,其中最引人注目的就是Binder,本文将深入探讨Binder的技术原理,解释其工作方式以及相关的关键概念
    2023-10-10
  • Android自定义view实现圆的扩散效果

    Android自定义view实现圆的扩散效果

    这篇文章主要为大家详细介绍了Android自定义view实现圆的扩散效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android AlertDialog六种创建方式案例详解

    Android AlertDialog六种创建方式案例详解

    这篇文章主要介绍了Android AlertDialog六种创建方式案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • android 解析json数据格式的方法

    android 解析json数据格式的方法

    这篇文章主要介绍了android 解析json数据格式的方法,有需要的朋友可以参考一下
    2014-01-01
  • Android Shader应用开发之霓虹闪烁文字效果

    Android Shader应用开发之霓虹闪烁文字效果

    这篇文章主要为大家详细介绍了Android Shader应用开发之霓虹闪烁文字效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android  调用系统应用的方法总结

    Android 调用系统应用的方法总结

    这篇文章主要介绍了Android 调用系统应用的方法总结的相关资料,这里提供调用录像,录音,拍照等功能,需要的朋友可以参考下
    2017-08-08
  • Android仿腾讯视频实现悬浮窗效果

    Android仿腾讯视频实现悬浮窗效果

    对view比较熟悉的同学们应该发现了,其实我们的悬浮窗就是一个view,我们只需要把view添加到windowManager上就可以了。那么,下面通过本文给大家分享Android仿腾讯视频实现悬浮窗效果,一起看看吧
    2021-06-06
  • Android实现可滑动的自定义日历控件

    Android实现可滑动的自定义日历控件

    这篇文章主要为大家详细介绍了Android实现可滑动的自定义日历控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android闹钟启动时间设置无效问题的解决方法

    Android闹钟启动时间设置无效问题的解决方法

    这篇文章主要为大家详细介绍了Android闹钟启动时间设置无效问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android开发使用json实现服务器与客户端数据的交互功能示例

    Android开发使用json实现服务器与客户端数据的交互功能示例

    这篇文章主要介绍了Android开发使用json实现服务器与客户端数据的交互功能,结合具体实例形式分析了Android使用json格式数据在服务器与客户端传递实现数据库查询功能的步骤与相关操作技巧,需要的朋友可以参考下
    2017-10-10

最新评论