- 浏览: 1056425 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
转自:http://dev.chinamobile.com/cmdn/bbs/thread-9526-1-1.html
概要:本文通过对OPhone内置闹铃功能的简单介绍,来让开发者朋友们了解基于OPhone平台下客户/服务 模式的编程模型,以及如何使用OPhone系统提供的闹铃唤醒功能。与此同时,本文还对PendingIntent做一些简单的介绍,并通过实例程序来演 示如何通过PendingIntent将闹钟应用程序和系统闹铃服务联系起来。(作者:Yang Ai'in)
一、闹铃功能
闹钟应用程序作为人们日常常用的基本应用程序之一,其重要性不言而喻。在OPhone系统中闹铃服务功能不仅仅对闹钟应用程序服务,最重要的是可以利用该 闹铃服务功能提供的唤醒能力来做定时器。这样即便应用程序没有运行或者是没有启动的情况下,只要其注册过闹铃,那么该闹铃到时间后,OPhone系统可以 自动将该应用程序启动,这就是所谓的闹铃“唤醒“功能。
在OPhone系统中,底层系统提供了两种类型的时钟,软时钟与硬时钟,软时钟就是我们常说的Timer,硬时钟就是RTC。系统在正常运行的情况 下,Timer工作提供时间服务和闹铃提醒,而在系统进入睡眠状态后,时间服务和闹铃提醒由RTC来负责。对于上层应用来说,我们并不需要关心是 timer还是RTC为我们提供服务,因为OPhone系统的Framework层把底层细节做了封装并统一提供API。这个API他的名字就叫 AlarmManager。在OPhone系统中有意思的是对应AlarmManage有一个AlarmManagerServie服务程序,该服务程序 才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在OPhone系统中,linux实现的设备名 为”/dev/alarm”),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列 表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化闹铃设备(/dev/alarm)。当 然,在JAVA层的AlarmManagerService与Linux Alarm驱动程序接口之间还有一层封装,那就是JNI。
AlarmManager将应用与服务分割开来后,使得应用程序开发者不用关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许 就是客户/服务模式的好处吧。AlarmManager与AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关 系。在OPhone系统中,AlarmManage提供了3个接口5种类型的闹铃服务:
3个API调用接口:
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)。
在第一小节中,我们已经提到过OPhone系统AlarmManagerService提供了两个设置闹铃的API,他们分别是:
void set(int type, long triggerAtTime, PendingIntent operation)
void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
在OPhone 1.5版本中又增加了一个API。
下面的程序演示了如何设置一个闹铃。
/当设置的闹铃触发时,Ophone系统会广播一个Intent,当然我们需要在创建一个新的闹铃
//时注册一个闹铃事件接收对象AlarmReceiver ,该接收对象也可以通过在//AndroidManifest.xml中发布,也可以在代码中动态注册。
PendingIntent
在前面的章节中,我们在注册闹铃﹑发送闹铃事件的时候,有过一个重要的参数PendingIntent。这个PendingIntent可以说是 Intent的进一步封装,他既包含了Intent的描述又是Intent行为的执行(这种定义也许不太严格),如果将Intent比作成一个订单的 话,PendingIntent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送 失败后要重发还是取消订单等操作。开发者可以通过调用getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)函数来得到一个PendingIntent实例。
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
通过该函数获得的PendingIntent将会扮演一个广播的功能,就像调用 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让OPhone系统自动找到该行为处理对象。
实例代码如下:
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
Public static PendingIntent getActivity(Context, int, Intent, int)
通过该函数获得的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);
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags)
通过该函数获得的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);
概要:本文通过对OPhone内置闹铃功能的简单介绍,来让开发者朋友们了解基于OPhone平台下客户/服务 模式的编程模型,以及如何使用OPhone系统提供的闹铃唤醒功能。与此同时,本文还对PendingIntent做一些简单的介绍,并通过实例程序来演 示如何通过PendingIntent将闹钟应用程序和系统闹铃服务联系起来。(作者:Yang Ai'in)
一、闹铃功能
闹钟应用程序作为人们日常常用的基本应用程序之一,其重要性不言而喻。在OPhone系统中闹铃服务功能不仅仅对闹钟应用程序服务,最重要的是可以利用该 闹铃服务功能提供的唤醒能力来做定时器。这样即便应用程序没有运行或者是没有启动的情况下,只要其注册过闹铃,那么该闹铃到时间后,OPhone系统可以 自动将该应用程序启动,这就是所谓的闹铃“唤醒“功能。
在OPhone系统中,底层系统提供了两种类型的时钟,软时钟与硬时钟,软时钟就是我们常说的Timer,硬时钟就是RTC。系统在正常运行的情况 下,Timer工作提供时间服务和闹铃提醒,而在系统进入睡眠状态后,时间服务和闹铃提醒由RTC来负责。对于上层应用来说,我们并不需要关心是 timer还是RTC为我们提供服务,因为OPhone系统的Framework层把底层细节做了封装并统一提供API。这个API他的名字就叫 AlarmManager。在OPhone系统中有意思的是对应AlarmManage有一个AlarmManagerServie服务程序,该服务程序 才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在OPhone系统中,linux实现的设备名 为”/dev/alarm”),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列 表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化闹铃设备(/dev/alarm)。当 然,在JAVA层的AlarmManagerService与Linux Alarm驱动程序接口之间还有一层封装,那就是JNI。
AlarmManager将应用与服务分割开来后,使得应用程序开发者不用关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许 就是客户/服务模式的好处吧。AlarmManager与AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关 系。在OPhone系统中,AlarmManage提供了3个接口5种类型的闹铃服务:
3个API调用接口:
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)。
在第一小节中,我们已经提到过OPhone系统AlarmManagerService提供了两个设置闹铃的API,他们分别是:
void set(int type, long triggerAtTime, PendingIntent operation)
void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
在OPhone 1.5版本中又增加了一个API。
下面的程序演示了如何设置一个闹铃。
/当设置的闹铃触发时,Ophone系统会广播一个Intent,当然我们需要在创建一个新的闹铃
//时注册一个闹铃事件接收对象AlarmReceiver ,该接收对象也可以通过在//AndroidManifest.xml中发布,也可以在代码中动态注册。
PendingIntent
在前面的章节中,我们在注册闹铃﹑发送闹铃事件的时候,有过一个重要的参数PendingIntent。这个PendingIntent可以说是 Intent的进一步封装,他既包含了Intent的描述又是Intent行为的执行(这种定义也许不太严格),如果将Intent比作成一个订单的 话,PendingIntent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送 失败后要重发还是取消订单等操作。开发者可以通过调用getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)函数来得到一个PendingIntent实例。
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
通过该函数获得的PendingIntent将会扮演一个广播的功能,就像调用 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让OPhone系统自动找到该行为处理对象。
实例代码如下:
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
Public static PendingIntent getActivity(Context, int, Intent, int)
通过该函数获得的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);
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags)
通过该函数获得的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);
发表评论
-
打印调用堆栈
2019-11-15 15:48 446平常我们遇到不清楚代码逻辑的,可以通过打印调用堆栈来理清楚,如 ... -
你知道Log.isLoggable
2018-11-23 14:15 850我们可以通过Log.isLoggable来动态开关log的输出 ... -
android:allowUndo
2018-04-25 16:51 754Android 在Android 23增加了UndoManag ... -
mipmap-xxx
2015-12-10 11:35 1064最近在看AOSP,发现mipmaps, 百度 了一下,发现有各 ... -
《Android.Programming.Pushing.the.Limits].Erik.Hellman》记录1
2015-10-29 10:56 553最近在看《Android.Programming.Pushin ... -
System.currentTimeMillis() uptimeMillis elapsedRealtime 区别
2015-10-28 20:02 1280转自http://blog.csdn.net/wutianyi ... -
GPS的开关设置
2015-09-29 18:36 1989//modify by hyxu 2015-9-30 to s ... -
DialogFragment
2015-09-25 13:56 1012public class YesNoDialog extend ... -
ANDROID L——RecyclerView,CardView导入和使用
2015-07-23 09:51 921转自http://blog.csdn.net/a3969019 ... -
IntentService 和ResultReceiver
2015-07-22 20:00 789转自[url] http://javatechig.com/a ... -
Android media媒体库分析之:分类别统计媒体文件大小
2015-07-21 20:07 527转自http://www.linuxidc.com/Linux ... -
java.lang.IllegalArgumentException: Service Intent must be explicit
2015-07-21 20:03 1280转自:http://www.2cto.com/kf/20150 ... -
Context 和Application Context
2015-02-11 15:14 865http://possiblemobile.com/2013/ ... -
ContentProviderOperation.Builder 中withValue和withValueBackReference的区别
2015-02-10 14:01 2178关于ContentProviderOperation.Buil ... -
AndroidManifest.xml的Service元素 android:process设置
2013-05-30 17:02 11462转自:http://galin.blog.sohu ... -
android中打包含有Activity以及资源文件的jar包在工程中调用
2013-05-28 15:00 1294转自:http://www.cnblogs.com/vaiya ... -
Android杂谈--内存泄露(1)--contentView缓存使用与ListView优化
2012-11-01 09:29 2802转自:http://www.cnblogs.com/louli ... -
Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
2012-10-31 14:32 1857转自:http://www.eoeandroid.com/th ... -
Animation
2012-10-30 13:41 1110转自:http://hi.baidu.com/wendaoer ... -
Android onTouchEvent和onInterceptTouchEvent
2012-10-24 15:05 1263ViewGroup里的onInterceptTouchEven ...
相关推荐
实现可定时响起的闹钟----PendingIntent 与 AlarmManager 的运用
NULL 博文链接:https://technicalsearch.iteye.com/blog/2007636
根据本讲学习内容,学会运用AlarmManager和PendingIntent实现定时提醒功能。 (1) 提交的作业包中含有应用程序运行效果截图。
本地通知非常适合具有基于时间的行为的应用程序,例如日历和待办事项列表应用程序。 在 iOS 允许的有限时间内在后台运行的应用程序也可能会发现本地通知很有用。 例如,依赖服务器获取消息或数据的应用程序可以在...
本篇通过AlarmManager和PendingIntent实现定时提醒功能,界面运用Button、TextView、Switch进行布局 同时这也是中国大学慕课移动终端应用开发的网课作业18,我会持续更新我的作业,如果有需要关注一下吧 说明 具体的...
AlarmManager简介 AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service...PendingIntent sender = PendingIntent.getBroadcast(cont
简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent。 项目功能简介: AlarmService模拟后台任务,定时发起广播 AlarmReceive启动...
android-闹钟 使用 AlarmManager、BroadcastReceiver 和 PendingIntent 的重复 Android 警报的简单示例
本文实例讲述了Android编程实现PendingIntent控制多个闹钟的方法。分享给大家供大家参考,具体如下: 要用 android.app.PendingIntent.getBroadcast(Context context, int requestCode, Intent intent)来实现控制多...
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); } }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show(); } }
AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); // create a PendingIntent that will perform a broadcast PendingIntent pi= PendingIntent.getBroadcast(MainActivity.this, 0, new ...
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); // 开始时间 long firstime = SystemClock.elapsedRealtime(); // 60秒一个周期,不停的发送广播 // am....
Android 创建备忘录的例子,可自定义备忘录内容,自定义时间和自定义提醒闹钟的类型、铃声等。相关代码: c.setTimeInMillis(System.currentTimeMillis());//设置当前时间 c.set(Calendar.HOUR_OF_DAY, ...
Android闹钟alarmManager+PendingIntent的使用方法,供大家参考,具体内容如下 这次给大家分享两种闹钟: 第一种是当到达闹钟时间后执行一次相应的操作 alarmManager.set(AlarmManager.RTC_WAKEUP,c....
Android 使用... am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//设置闹钟 Toast.makeText(Sample_12_7.this, "闹钟设置成功", Toast.LENGTH_LONG).show();//提示用户 } }
使用AlarmManager.setRepeating();方法,利用PendingIntent来做。这就是思路。 使用数据库和使用SharedPreferences
6.10 实现可定时响起的闹钟——PendingIntent与AlarmManager的运用 6.11 黑名单来电自动静音——PhoneStateListener与AudioManager 6.12 手机翻背面即静音震动——SensorListener及AudioManager整合应用 6.13 指定...
6.10 实现可定时响起的闹钟——PendingIntent与AlarmManager的运用 6.11 黑名单来电自动静音——PhoneStateListener与AudioManager 6.12 手机翻背面即静音震动——SensorListener及AudioManager整合应用 6.13 指定...
6.10 实现可定时响起的闹钟——PendingIntent与AlarmManager的运用 6.11 黑名单来电自动静音——PhoneStateListener与AudioManager 6.12 手机翻背面即静音震动——SensorListener及AudioManager整合应用 6.13 指定...