- 浏览: 1592711 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (289)
- java 语法基础 (51)
- spring (8)
- mvc struct /Ant --build.xml (8)
- SOA (0)
- oracle 9i/10g (23)
- sql server 2000-2005 (3)
- 数据库基础知识 (6)
- 设计模式与软件架构 (10)
- Hibernate 持久化 (9)
- J2SE/J2EE/J2ME/AJAX 技术 (8)
- JSF 技术 (3)
- JAVA 图形化 (0)
- JMS (40)
- Eclipse 3.2 IDE 开发技巧 (13)
- 项目处理方法集合 (2)
- html/jsp/javascript (2)
- Unix/Linux (9)
- j2me/ARM/windriver/嵌入式 (4)
- 电信科学 (8)
- jsp (1)
- c/c++ (1)
- LZW压缩算法(java) (2)
- Android (77)
- 版本管理git/svn (2)
最新评论
-
huihai:
有demo吗?
NamingStrategy实现动态表名映射 -
cangbaotu:
推荐给大家一些有用的爬虫源码:https://github.c ...
网络爬虫(源代码参考) -
tuspark:
除了.classpath文件以外,.project文件也应该了 ...
Eclipse .classpath文件浅谈 -
tuspark:
造成eclipse自动关闭的原因有很多,这里有很多介绍:ecl ...
eclipse 自动关闭 解决方案 -
DEMONU:
网上都是这些,这种文章。。。
ActiveMQ中的消息持久性
本文通过对android内置闹铃功能的简单介绍,来让开发者朋友们了解基于OPhone平台下客户/服务模式的编程模型,以及如何使用OPhone 系统提供的闹铃唤醒功能。与此同时,本文还对PendingIntent做一些简单的介绍,并通过实例程序来演示如何通过PendingIntent将闹 钟应用程序和系统闹铃服务联系起来。
一、闹铃功能
闹钟应用程序作为人 们日常常用的基本应用程序之一,其重要性不言而喻。在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调用接口:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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种闹铃类型:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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 SDK来获取更多AlarmManager的相关细节,他所在的包是:Android.app.AlarmManager
随着OPhone系统版本的不断的升级,这些接口和闹铃类型也许会有些调整,但其基本使用方法将不会改变
二、闹钟设置与提醒
我们首先通过一个直观的UI来感受一下OPhone系统内嵌的闹钟程序是如何响应用户设置和自动提醒的的:(如下4个图所示)
上面4图直观的告诉了开发者如何使用OPhone内嵌的闹钟应用程序,但开发者可能更关心的是通过代码如何实现这些功能。比如说怎么设置一个闹铃,该设置哪种类型的闹铃,以及如何获得闹铃时间已到并提醒用户。依据这几个问题,我们在下面的章节中逐步来介绍。
2.1 设置闹铃
在第一小节中,我们已经提到过OPhone系统AlarmManagerService提供了两个设置闹铃的API,他们分别是:
void setRepeating( int type, long triggerAtTime, long interval, PendingIntent operation)
在OPhone 1.5版本中又增加了一个API。
下面的程序演示了如何设置一个闹铃。
// 时注册一个闹铃事件接收对象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 = 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_SERVICE);
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(AlarmManager.POWER_OFF_WAKEUP,
firstTime, 15 * 1000 , sender);
< receiver android:name = " AlarmReceiver " >
< intent - filter >
< action android:name = " com.android.alarmclock.ALARM_ALERT " />
</ intent - filter >
</ receiver >
AlarmReceiver mAlarmReceiver;
IntentFilter filter = new IntentFilter();
filter .addAction(“com.android.alarmclock.ALARM_ALERT”);
context.registerReceiver(mAlarmReceiver, filter );
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
在OPhone系统中,当应用程序所设置的闹铃时间到了后,OPhone系统中的 AlarmManagerService就会从系统底层获取一个闹铃事件并从自己维护的队列中取出其匹配的闹铃,然后通过其应用注册的 PendingIntent把该闹铃事件发送回给应用。
当我们的应用收到该Intent后就会启动相应的Activity来提醒用户闹铃时间到。
程序代码如下:
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 void 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.FLAG_ACTIVITY_NEW_TASK);
// 启动一个新的UI对象来提醒
context.startActivity(fireAlarm);
}
}
在前面的章节中,我们在注册闹铃﹑发送闹铃事件的时候,有过一个重要的参数 PendingIntent。这个PendingIntent可以说是 Intent的进一步封装,他既包含了Intent的描述又是Intent行为的执行(这种定义也许不太严格),如果将Intent比作成一个订单的 话,PendingIntent更像是一个下订单的人,因为它既要负责将订单发出去,也要负责订单发送后的处理,比如发送成功后要准备验收订单货物,发送 失败后要重发还是取消订单等操作。开发者可以通过调用getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)函数来得到一个PendingIntent实例。
通过该函数获得的PendingIntent将会扮演一个广播的功能,就像调用 Context.sendBroadcast()函数一样。当系统通过它要发送一个intent时要采用广播的形式,并且在该intent中会包含相应的 intent接收对象,当然这个对象我们可以在创建PendingIntent的时候指定,也可以通过ACTION 和CATEGORY等描述让OPhone系统自动找到该行为处理对象。
实例代码如下:
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0 , intent, 0 );
通过该函数获得的PendingIntent可以直接启动新的activity, 就像调用 Context.startActivity(Intent)一样.不过值得注意的是要想这个新的Activity不再是当前进程存在的Activity 时。我们在intent中必须使用Intent.FLAG_ACTIVITY_NEW_TASK.
实例代码如下:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 ,
new Intent(this, AlarmService.class), 0 );
通过该函数获得的PengdingIntent可以直接启动新的Service,就像调用Context.startService()一样。
实例代码如下:
// with the alarm manager.
mAlarmSender = PendingIntent.getService(AlarmService.this,
0 , new Intent(AlarmService.this, AlarmService_Service.class), 0 );
四、PendingInent与 service
在OPhone系统编程中,一个完整OPhone应用程序可以有4个需要创建的模块,他们分别是:
Activity ,Broadcast intent Receiver,Service,Content Provider。Service作为一个OPhone应用程序组成部分,通常运行在系统后台,他与用户之间没有交互。像其他应用程序对象一样运行在所属 进程的主线程中。那么这就意味着它有可能进入长时间的运行等待而导致应用得不到用户的相应。所以在开发者设计程序的时候就要考虑,如果一个Service 要做一些长时间的数据处理时(比如播放MP3 ,或者是网络 下载),就需要把该工作切换到自己的线程空间来执行。
实例代码如下:
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 对象
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 endTime = 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);
}
};
}
小结: 本 篇文章主要介绍了如何使用AlarmManager的定时唤醒功能,以及各种闹铃的含义与API使用实例,希望对读者朋友们在OPhone应用编程中,对 AlarmManager的正确使用起到抛砖引玉的作用。同时我们还引入了一个重要的概念 PendingIntent,通过对PendingIntent参数的解析,相信读者朋友们对PendingIntent的使用有了一个基本的认识。
发表评论
-
APN(default、mms、supl、dun、hipri接入点类型的区别)
2013-10-10 19:31 45895设置APN上网时,大家可能经常遇到这个问题:为什么有时要填写 ... -
AMR音频编码器概述及文件格式分析
2013-10-10 19:29 3963全称Adaptive Multi-Rate,自适应多速率编码 ... -
pad 强制加载 Hdpi资源 (2.3 dpi < 240)
2012-07-16 16:47 0pad 强制加载 Hdpi资源 (2.3 dpi < ... -
android 设计模式-享元模式
2011-12-16 23:01 2197享元(FlyWeight)模式,原意是“轻量级”模式,它用来解 ... -
statusbar 2.3
2011-11-08 16:04 1290以前我的文章分析过Stat ... -
android 输入法默认设置
2011-07-07 14:00 7201设置默认输入法 在 frameworks\base\co ... -
Android Media Scanner Process
2011-06-06 22:58 3412下面是系统图 Medi ... -
handler与多线程消息处理
2011-06-04 13:42 8287在Android下面也有多线程的概念,在C/C++中,子线程可 ... -
android light
2011-03-24 16:15 3417背光设置是在:设置->声音和显示->亮度,通过进度 ... -
PowerManagerService sensor
2011-03-22 20:06 2676默认分类 2010-12-24 14:34:55 阅读144 ... -
android 单例
2011-02-15 09:26 42331. Framework层的代码: A ... -
Android 开发之 Services 服务
2010-11-02 18:21 4246Service Service ... -
深入学习android之AlarmManager
2010-11-01 16:50 34770对应AlarmManage有一个Alarm ... -
eclipse+android+ddms+adt
2010-09-14 10:30 8467用eclipse + ADT作为android ... -
Menus
2010-09-05 19:01 1793Android Menus 文章分类:移动开发 1.O ... -
Android开机自启动应用开发
2010-08-28 10:07 4597目前需要开发一个开机自启动的GTD应用程序来提醒用户的 ... -
Android2.1_Launcher学习笔记
2010-08-11 13:40 3882文章分类:移动开发 好么,2.0的源码没看几天,2.1的 ... -
Browsing Android Source in Eclipse
2010-08-09 16:00 3442Google’s Android SDK includes ... -
android 编译
2010-08-07 16:31 25381 我的系统是Ubuntu 8.04 * 2 系统上 ... -
Ubuntu linux 右键添加"以管理员身份打开","在终端中打开"
2010-08-02 15:04 4671Ubuntu linux 右键添加"以管理员身份打开 ...
相关推荐
3. 将之前创建的`PendingIntent`作为参数传递给`AlarmManager`,这样当闹钟触发时,`PendingIntent`会启动服务。 4. 在服务启动后,根据业务需求执行相应的操作,例如执行某些计算、更新数据或显示通知。 5. 如果...
在实际应用中,我们首先创建一个`PendingIntent`,然后将其传递给`AlarmManager`的`set()`或`setRepeating()`方法,以设置闹钟。例如,如果我们希望创建一个定时提醒,我们可以这样做: ```java // 创建一个...
4. **取消闹钟**:如果用户取消了闹钟,可以通过`AlarmManager`的`cancel()`方法和`PendingIntent`来取消已经设置的闹钟。 ```java alarmManager.cancel(pendingIntent); ``` 5. **更新闹钟**:如果用户修改了闹钟...
3. **取消闹钟**:在不再需要闹钟时,记得调用`AlarmManager.cancel(PendingIntent)`取消相应的闹钟。 4. **测试**:由于模拟器和设备的限制,测试闹钟时可能需要在真实设备上进行。 通过以上内容,你应该已经掌握...
本篇通过AlarmManager和PendingIntent实现定时提醒功能,界面运用Button、TextView、Switch进行布局 同时这也是中国大学慕课移动终端应用开发的网课作业18,我会持续更新我的作业,如果有需要关注一下吧 说明 具体的...
7. **启动Alarm**:最后,将创建好的PendingIntent传入`AlarmManager` 的`set()` 方法,完成闹钟的设置。 ### 注意事项 - **权限**:使用`AlarmManager` 需要声明`...
如果需要取消已经设置的闹钟,可以使用`AlarmManager`的`cancel()`方法,传入相同的`PendingIntent`: ```java public void cancelAlarm(Context context) { Intent alarmIntent = new Intent(context, ...
6. **取消闹钟**:如果需要取消已经设置的闹钟,可以调用`AlarmManager`的`cancel`方法,并传入之前创建的`PendingIntent`。 ```java alarmManager.cancel(pendingIntent); ``` 以上就是Android中使用`...
`AlarmManager`是Android中强大的定时任务管理工具,通过结合`PendingIntent`和`BroadcastReceiver`,我们可以实现重启后依然生效的多闹钟功能。但务必注意权限管理和电池效率,以提供良好的用户体验。在实际开发中...
这个"小闹钟"程序是基于`AlarmManager`构建的,非常适合初学者学习如何在Android环境中实现定时任务。下面我们将深入探讨`AlarmManager`的工作原理及其在项目中的应用。 `AlarmManager`是Android系统服务之一,它...
4. 可能还包含取消闹钟的代码,通过`AlarmManager`的`cancel()`方法和对应的`PendingIntent`。 实际开发中,要注意以下几点: 1. 由于电源管理和系统优化,`AlarmManager`可能不会完全精确。对于要求严格时效性的...
这种设计使得我们可以放心地将PendingIntent传递给第三方应用,如通知系统或闹钟服务。 PendingIntent的创建通常通过以下方法: 1. `getActivity(Context context, int requestCode, Intent intent, int flags)`: ...
`PendingIntent`与`BroadcastReceiver`关联,当`AlarmManager`触发时,它会通过`PendingIntent`激活`BroadcastReceiver`。 在设定闹钟时,开发者会使用`AlarmManager`的`set()`或`setRepeating()`方法。例如,设定...
在Android开发中,实现整点报时和闹钟功能是一个常见的需求,这通常涉及到系统级的服务——AlarmManager。AlarmManager是Android系统提供的一个用于安排在特定时间执行任务的服务,它可以触发BroadcastReceiver,...
`AlarmManager`通过向系统注册闹钟事件,当达到设定的时间点时,会触发一个`PendingIntent`,这个`PendingIntent`通常会启动一个广播接收器(BroadcastReceiver)来处理相应的任务,比如显示通知、更新UI或者执行...
本教程将深入探讨`AlarmManager`的使用,以及如何通过它来实现一个实用的闹钟功能。 ### 1. `AlarmManager`介绍 `AlarmManager`是Android系统服务,它提供了调度任务的能力,可以在指定的绝对时间或相对时间触发一...
总之,`AlarmManager`是Xamarin.Android中实现定时任务的关键组件,结合`BroadcastReceiver`和`PendingIntent`,开发者可以轻松地创建各种定时功能,例如闹钟、定时提醒或后台数据同步。理解和熟练运用这些概念对于...
3. 设置AlarmManager:在需要触发广播的时间点,通过`AlarmManager`的`set()`或`setExact()`方法设置闹钟,传入BroadcastReceiver对应的Intent和触发时间。 4. 发送Intent:使用PendingIntent与BroadcastReceiver...
3. **设置AlarmManager**:在需要触发定时任务的地方,获取AlarmManager服务并设置闹钟。 ```java AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent alarmIntent = new Intent...
在Android中,设置和取消闹钟主要依赖于AlarmManager、PendingIntent和BroadcastReceiver这三个组件。通过合理使用它们,开发者可以创建出符合用户需求的各种闹钟功能。在`TextDemo5`项目中,你可以根据以上步骤实现...