ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:108.36KB ,
资源ID:10782648      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10782648.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(androidalarm.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

androidalarm.docx

1、androidalarmPendingInent 与 AlarmManager 2009-11-22 23:13:51 来源: 网友评论 0 条 概要:本文通过对OPhone内置闹铃功能的简单介绍,来让开发者朋友们了解基于OPhone平台下客户/服务模式的编程模型,以及如何使用OPhone系统提供的闹铃唤醒功能。与此同时,本文还对PendingIntent做一些简单的介绍,并通过实例程序来演示如何通过PendingIntent将闹钟应用程序和系统闹铃服务联系起来。(作者:Yang Aiin)一、闹铃功能 闹钟应用程序作为人们日常常用的基本应用程序之一,其重要性不言而喻。在OPhone系统中闹铃

2、服务功能不仅仅对闹钟应用程序服务,最重要的是可以利用该闹铃服务功能提供的唤醒能力来做定时器。这样即便应用程序没有运行或者是没有启动的情况下,只要其注册过闹铃,那么该闹铃到时间后,OPhone系统可以自动将该应用程序启动,这就是所谓的闹铃“唤醒“功能。 在OPhone系统中,底层系统提供了两种类型的时钟,软时钟与硬时钟,软时钟就是我们常说的Timer,硬时钟就是RTC。系统在正常运行的情况下,Timer工作提供时间服务和闹铃提醒,而在系统进入睡眠状态后,时间服务和闹铃提醒由RTC来负责。对于上层应用来说,我们并不需要关心是timer还是RTC为我们提供服务,因为OPhone系统的Framewor

3、k层把底层细节做了封装并统一提供API。这个API他的名字就叫AlarmManager。在OPhone系统中有意思的是对应AlarmManage有一个AlarmManagerServie服务程序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在OPhone系统中,linux实现的设备名为”/dev/alarm”),并且一直监听闹铃设备,一旦有闹铃触发或者是闹铃事件发生,AlarmManagerServie服务程序就会遍历闹铃列表找到相应的注册闹铃并发出广播。该服务程序在系统启动时被系统服务程序system_service启动并初始化

4、闹铃设备(/dev/alarm)。当然,在JAVA层的AlarmManagerService与Linux Alarm驱动程序接口之间还有一层封装,那就是JNI。 AlarmManager将应用与服务分割开来后,使得应用程序开发者不用关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许就是客户/服务模式的好处吧。AlarmManager与AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关系。在OPhone系统中,AlarmManage提供了3个接口5种类型的闹铃服务: 3个API调用接口: void cancel(PendingInte

5、nt 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 /当系统

6、进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。 public static final int ELAPSED_REALTIME_WAKEUP /能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。 public static final int RTC /当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间

7、是绝对时间,所用时间是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 SDK来获取更多AlarmManage

8、r的相关细节,他所在的包是:Android.app.AlarmManager 随着OPhone系统版本的不断的升级,这些接口和闹铃类型也许会有些调整,但其基本使用方法将不会改变 二、闹钟设置与提醒 我们首先通过一个直观的UI来感受一下OPhone系统内嵌的闹钟程序是如何响应用户设置和自动提醒的的:(如下4个图所示)(图一)(图二)(图三) (图四) 上面4图直观的告诉了开发者如何使用OPhone内嵌的闹钟应用程序,但开发者可能更关心的是通过代码如何实现这些功能。比如说怎么设置一个闹铃,该设置哪种类型的闹铃,以及如何获得闹铃时间已到并提醒用户。依据这几个问题,我们在下面的章节中逐步来介绍。2.1

9、 设置闹铃 在第一小节中,我们已经提到过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,当然我们需要在创

10、建一个新的闹铃 /时注册一个闹铃事件接收对象AlarmReceiver ,该接收对象也可以通过在/AndroidManifest.xml中发布,也可以在代码中动态注册。 import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.SystemClock; import android.os.Bundle; import java.util.Calendar; /创建一个PendingIntent Intent intent

11、= new Intent(ALARM_ALERT_ACTION); intent.putExtra(ID, id); intent.putExtra(TIME, atTimeInMillis); PendingIntent sender = PendingIntent.getBroadcast( context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); /获得AlarmMnager并注册一个新闹铃, /一次性闹铃的设置 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVIC

12、E); am.set(AlarmManager.POWER_OFF_WAKEUP, atTimeInMillis, sender); /重复性闹铃的设置 / We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 15*1000; / Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating

13、(AlarmManager.POWER_OFF_WAKEUP, firstTime, 15*1000, sender); /在AndroidManifest.xml中注册 /在应用程序执行过程中注册 AlarmReceiver mAlarmReceiver; IntentFilter filter = new IntentFilter(); filter.addAction(“com.android.alarmclock.ALARM_ALERT”); context.registerReceiver(mAlarmReceiver, filter); public class AlarmRece

14、iver extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) 2.2 闹铃提醒 在OPhone系统中,当应用程序所设置的闹铃时间到了后,OPhone系统中的AlarmManagerService就会从系统底层获取一个闹铃事件并从自己维护的队列中取出其匹配的闹铃,然后通过其应用注册的PendingIntent把该闹铃事件发送回给应用。PendingIntent.send(); 当我们的应用收到该Intent后就会启动相应的Activity来提醒用户闹铃时间到。 程序代码如

15、下:package com.android.alarmclock; import android.content.Context; import android.content.Intent; import android.content.BroadcastReceiver; import android.os.Handler; import android.os.PowerManager; import android.os.SystemClock; public class AlarmReceiver extends BroadcastReceiver Override public vo

16、id onReceive(Context context, Intent intent) long now = System.currentTimeMillis(); int id = intent.getIntExtra(Alarms.ID, 0); long setFor = intent.getLongExtra(Alarms.TIME, 0); Intent fireAlarm = new Intent(context, AlarmAlert.class); fireAlarm.putExtra(Alarms.ID, id); fireAlarm.setFlags(Intent.FLA

17、G_ACTIVITY_NEW_TASK); /启动一个新的UI对象来提醒 context.startActivity(fireAlarm); 三、PendingIntent 在前面的章节中,我们在注册闹铃发送闹铃事件的时候,有过一个重要的参数PendingIntent。这个PendingIntent可以说是Intent的进一步封装,他既包含了Intent的描述又是Intent行为的执行(这种定义也许不太严格),如果将Intent比作成一个订单的话,PendingIntent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送失败后要

18、重发还是取消订单等操作。开发者可以通过调用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将会扮演一个广播的功能,就像调用

19、 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让OPhone系统自动找到该行为处理对象。 实例代码如下:Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmContro

20、ller.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 ac

21、tivity 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

22、.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); 四、PendingInent与 service 在OPhone系统编程中,一个完整OPhone应用程序可

23、以有4个需要创建的模块,他们分别是: Activity ,Broadcast intent Receiver,Service,Content Provider。Service作为一个OPhone应用程序组成部分,通常运行在系统后台,他与用户之间没有交互。像其他应用程序对象一样运行在所属进程的主线程中。那么这就意味着它有可能进入长时间的运行等待而导致应用得不到用户的相应。所以在开发者设计程序的时候就要考虑,如果一个Service要做一些长时间的数据处理时(比如播放MP3,或者是网络下载),就需要把该工作切换到自己的线程空间来执行。 实例代码如下:import android.app.Notifi

24、cation; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.widget.Toast; /定义一个 Service 对象

25、 public class AlarmService_Service extends Service NotificationManager mNM; public void onCreate() /创建一个线程来运行Runnable Thread thr = new Thread(null, mTask, AlarmService_Service); thr.start(); public void onDestroy() Runnable mTask = new Runnable() public void run() / 通常我们就可以在这里设计长时间运行的功能, long endTim

26、e = System.currentTimeMillis() + 15*1000; while (System.currentTimeMillis() endTime) synchronized (mBinder) try mBinder.wait(endTime - System.currentTimeMillis(); catch (Exception e) / 停止Service AlarmService_Service.this.stopSelf(); ; /在编写Service代码时,可以不实现onStart,onStop等函数,但一定要实现onBind函数 public IBinder onBind(Intent intent) return mBinder; /* 通过该对象可以与客户端通信 */ private final IBinder mBinder = new Binder() Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException return super.onTransact(code, data, reply, flags);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1