PendingIntent and AlarmManager Introduction

2010-05-05  来源:本站原创  分类:Mobile  人气:200 

Transfer from: http://dev.chinamobile.com/cmdn/bbs/thread-9526-1-1.html

SUMMARY: Based on the OPhone built-in alarm function briefly to allow developers to friends understanding OPhone platform based on client / server programming model, and how to use OPhone alarm wake-up function provided by the system. At the same time, the paper also PendingIntent do some brief, and an example program to demonstrate how PendingIntent the alarm clock alarm services, applications and systems linked. (Author: Yang Ai'in)
1, alarm clock alarm clock application used as the everyday one of the basic application, its importance is self-evident. Alarm system in OPhone services not only to the alarm clock application service, the most important thing is to use the services provided by the wake-up alarm capability to do timer. Even if this application is not running or not start the case, as long as its registered alarm, then the time the alarm Dao Hou, OPhone system Ke Yi automatically start the application, which is called the alarm, "wake up" feature.
In OPhone system, the underlying system provides two types of clock, clock software clock and hard and soft clock is what we often say Timer, hardware clock is the RTC. System under normal operation, Timer to provide time services and alarms, while in the system to sleep, the time of service and alarms to be responsible by the RTC. For the upper applications, we do not need to worry about is the timer or RTC to provide services for us, because OPhone System Framework layer to the underlying details of the package and do provide unified API. This API is called his AlarmManager. In OPhone system is interesting that there is a corresponding AlarmManage AlarmManagerServie service program, the service program is to provide the alarm service is true, it is mainly down to maintain the application for registration and timely to set various alarm will trigger the alarm to the downtown Bell Equipment (in OPhone system, linux device called the realization of "/ dev / alarm"), and has been monitoring the alarm device, once the alarm is triggered or alarm events, AlarmManagerServie service program will traverse the alarm list to find the appropriate registration and issue radio alarm. The service program at system startup when the system service routine system_service start and initialize the alarm device (/ dev / alarm). Of course, in JAVA and Linux Alarm AlarmManagerService layer driver interface layer between the well package, that is JNI.
AlarmManager applications and services will be separated, the so application developers do not care about specific services, but directly through AlarmManager to use this service. This may be the client / service model of the benefits of it. AlarmManager and AlarmManagerServie to between communicate by Binder, is a many to one relationship between them. In OPhone system, AlarmManage provides three interfaces 5 types of alarm services:
3 API Call Interface:
void cancel (PendingIntent operation)
/ / Cancel the registered alarm and parameter matching
void set (int type, long triggerAtTime, PendingIntent operation)
/ / Register a new alarm
void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
/ / Register a repeat type of alarm
void setTimeZone (String timeZone)
/ / Set time zone

5 kinds of alarm types:
public static final int ELAPSED_REALTIME
/ / When the system goes into sleep state, this type of alarm will not wake the system. Wake up until the system before the next pass it, the alarm time is used in relative time is after start time from the system, including sleep time, you can call SystemClock.elapsedRealtime () access. System value is 3 (0x00000003).

public static final int ELAPSED_REALTIME_WAKEUP
/ / Can wake the system, use the same ELAPSED_REALTIME, the system value is 2 (0x00000002).

public static final int RTC
/ / When the system goes into sleep state, this type of alarm will not wake the system. Until the system is awakened before the next pass it, the alarm time is used in absolute time, time is UTC time used, by calling System.currentTimeMillis () access. System value is 1 (0x00000001).

public static final int RTC_WAKEUP
/ / Can wake the system, use the type with the RTC, the system value is 0 (0x00000000).

Public static final int POWER_OFF_WAKEUP
/ / Can wake the system, it is a shutdown alarm, that device is turned off can also wake up the system, so we call it off the alarm. RTC to use the same type, the system value of 4 (0x00000004).
In the first section, we have already mentioned OPhone system AlarmManagerService provides two set the alarm API, they are:
void set (int type, long triggerAtTime, PendingIntent operation)
void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)

In OPhone 1.5 version adds another API.
The following program demonstrates how to set an alarm.

/ When setting the alarm is triggered, Ophone system will broadcast an Intent, of course, we need to create a new alarm
/ / Register an alarm when the event sink object AlarmReceiver, the receiving object can also be in the / / AndroidManifest.xml in the release can also be registered dynamically in code.

PendingIntent
In the previous chapter, we register the alarm in the event ﹑ send alarm when there was an important parameter PendingIntent. Intent can be said that PendingIntent further package, he not only includes a description of Intent Intent behavior is the implementation (which may not strictly defined), if an order Intent than Zuocheng then, PendingIntent more like an order under people, because it has to be responsible for issuing the order to go, to be responsible for orders sent after processing, such as sending successful acceptance of orders for goods to be ready to send after the failure to re-send or cancel the order and other operations. Developers can call getActivity (Context, int, Intent, int), getBroadcast (Context, int, Intent, int), getService (Context, int, Intent, int) function to get a PendingIntent instance.

public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)

PendingIntent obtained by the function will play a broadcast function, as called Context.sendBroadcast () function is the same. When the system through its intent to send a broadcast to be used when the form, and the intent would be to include the appropriate intent of the receiving object, course, this object we can create PendingIntent the time specified, you can also CATEGORY ACTION and so describes getting OPhone behavior management system to automatically locate the object.
Example code is as follows:

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 obtained by the function can start a new activity, just call Context.startActivity (Intent) as. But worth noting is to find the new Activity no longer exists in the Activity when the current process. Our intent must use Intent.FLAG_ACTIVITY_NEW_TASK.
Example code is as follows:

/ / 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 obtained by the function can start a new Service, as called Context.startService () the same.
Example code is as follows:

/ / 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);

相关文章
  • PendingIntent and AlarmManager Introduction 2010-05-05

    Transfer from: http://dev.chinamobile.com/cmdn/bbs/thread-9526-1-1.html SUMMARY: Based on the OPhone built-in alarm function briefly to allow developers to friends understanding OPhone platform based on client / server programming model, and how to u

  • 关于android中PendingIntent.getBroadcase的注册广播 2013-01-28

    使用语句 [java] view plain copy PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags) 获得PendingIntent,浏览了各类文章,大多数说了这种方法,但是基本上也就是止步于此,可是还有最重要的没有谈及,如何区别多个已注册的PendingIntent呢,看了一下PendingIntent.getBroadca

  • Android 系统定时管理器AlarmManager的使用 2014-11-23

    1.什么是AlarmManager? 按照Alarm+Manager的拼写可知,这是一个定时管理器,不能称为定时器,或者闹钟之类的,这样让人感觉他的重要性并不是那么高,功能那么简单. 作为管理器而言,首先是要有至少一个被管理者,否则就多此一举了.官方定义是,它是一种系统级别的提示服务,因此也就说他是一种可后台运行的 全局Service(不要担心创建了多余的Service,AlarmManager本身是一种已经被创建的Service). 2.在java中timer也是一个定时器,那么与Alarm的

  • alarm manager to create and abolish 2010-08-03

    Again mark the creation of what alamr manager public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ac01 app = ac01.getApp(); app.btEvent("from AlarmReceiver"); } } // Add alarm

  • Android Wear计时器开发 2014-09-28

    这篇文章主要介绍了Android Wear计时器开发,需要的朋友可以参考下 记得在2013年12月的时候,有系列文章是介绍怎么开发一个智能手表的App,让用户可以在足球比赛中记录停表时间.随着Android Wear的问世,在可穿戴设备中开发一款这样的App确实是个很不错的想法,但是按照目前对于Android Wear的架构了解来说,似乎有些困难.所以本系列文章我们就重写这个应用,带领大家进入Android Wear的世界. 本文不会长篇大论地讲解我们要开发的这款App的用途,因为我们在之前的系

  • 成为Android高手必须掌握的8项基本要求 2012-08-22

    (一)成为Android高手必须掌握的8项基本要求 [1] Android操作系统概述 1. Android系统架构. 2. Android利用设计理念. 3. Android 开源知识. 4. Android 参考网站与权威信息. [2] Android SDK及其开发环境搭建 1. Android SDK的版本发布. 2. ADT插件的安装和更新. 3. Android利用程序架构. 4. 建立第一个Android项目(HelloAndroid!). 5. 通过Eclipse导入一个开源项目

  • Android实现app长时间未操作时自动退出app 2015-02-01

    这里要考虑3个问题,第一个是锁屏问题,第二个是app被切换至后台的问题,第三个是屏幕锁定和解除时app在后台时的问题 一,监听屏幕解锁,锁定 public class ScreenObserver { private static String TAG = "ScreenObserver"; private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateList

  • Android实现App自动重启 2015-02-10

    Android实现App自动重启 使用PendingIntent和AlarmManager Intent restartIntent = new Intent(MainActivity.this, MainActivity.class); int pendingId = 1; PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, pendingId, restartIntent, PendingInt

  • Alarm Clock AlarmManager PendingIntent 2010-06-21

    Based on the android built-in alarm function in brief, to allow developers to friends understanding OPhone platform based on client / server programming model, and how to use OPhone alarm wake-up function provided by the system. At the same time, the

  • PendingIntent and Intent 2010-03-29

    Notification n = new Notification(R.drawable.face_1, "Service Start ", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0); n.setLatestEventInfo(this, " Th

  • In-depth study of AlarmManager android 2010-11-01

    Corresponds to a AlarmManagerServie AlarmManage service program, the service program is to provide an alarm service are true, it is mainly to maintain the application up and down the various types of alarm settings in a timely manner will trigger the

  • PendingIntent and understanding of the difference between Intent 2010-11-03

    AlarmManager which previously encountered in the learning PendingIntent, I am very confused, PendingIntent and Intent is the difference between what? I searched online, but saw many people still do not understand the explanation, see the PendingInten

  • Android-Intent and the relationship between PendingIntent 2010-11-05

    The relationship between Intent and PendingIntent Intent and PendingIntent relations ever more confused and spent some time analyzing the JavaDoc Android finally a little experience, to share with you here: Intent was a intent, a description of you w

  • The relationship between Intent and PendingIntent 2010-12-04

    Intent and PendingIntent relationship, when the beginner is confused when using PendingIntent, will appear strange problems, such as not passing data, the data can not be updated, so I set the length of the public at home, plus some of my personal pr

  • Android PendingIntent and Intent 2010-12-20

    Java code Notification n = new Notification (R.drawable.face_1, "Service started", System.currentTimeMillis ()); PendingIntent contentIntent = PendingIntent.getActivity (this, 0, new Intent (this, TServiceHolder. Class), 0); n.setLatestEventInfo

  • Android开发之AlarmManager的用法详解 2014-10-29

    这篇文章主要介绍了Android开发之AlarmManager的用法,是Android应用开发中非常实用的技能,需要的朋友可以参考下 Android中的AlarmManager实质上是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver). 一.概述: 该类提供一种访问系统闹钟服务的方式,允许你去设置在将来的某个时间点去执行你的应用程序.当你的闹钟响起(时间到)时,在它上面注册的

  • PendingIntent和Intent的区别 2012-04-10

    Java代码 Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0); n.setLatestEventInfo(this, "任

  • AlarmManager类的应用(实现闹钟功能) 2013-03-15

    1.AlarmManager,顾名思义,就是"提醒",是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用,实现闹钟等提示功能 2.AlarmManager的常用方法有三个: (1)set(int type,long startTime,PendingIntent pi): 该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作. (2)set

  • AlarmManager研究 2013-08-02

    AlarmManager研究 侯 亮 1.概述 在Android系统中,闹钟和唤醒功能都是由Alarm Manager Service控制并管理的.我们所熟悉的RTC闹钟以及定时器都和它有莫大的关系.为了便于称呼,我常常也把这个service简称为ALMS. 另外,ALMS还提供了一个AlarmManager辅助类.在实际的代码中,应用程序一般都是通过这个辅助类来和ALMS打交道的.就代码而言,辅助类只不过是把一些逻辑语义传递给ALMS服务端而已,具体怎么做则完全要看ALMS的实现代码了. AL

  • pendingIntent 2014-03-25

    pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件.打开一个广播组件和打开一个服务组件. 参数有4个,比较重要的事第三个