android广播事件处理broadcast receive -闹钟实例(运用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个接口:

  1. // 取消已经注册的与参数匹配的闹铃
  2. void cancel(PendingIntent operation)

 

  1.  
  2. //注册一个新的闹铃
  3. void set(int type, long triggerAtTime, PendingIntent operation)
  4. //注册一个重复类型的闹铃
  5. void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
  6. //设置时区
  7. void setTimeZone(String timeZone)

 

 

 

5个闹铃类型

 

 

 

 

public  static   final   int  ELAPSED_REALTIME

 

  1. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠
  2. 时间,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3 (0x00000003)。
  3. public static final int ELAPSED_REALTIME_WAKEUP
  4. //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) 。
  5. public static final int RTC
  6.  
  7. //当系统进入睡眠状态时,这种类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用
  8. System.currentTimeMillis()获得。系统值是1 (0x00000001) 。
  9. public static final int RTC_WAKEUP
  10. //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。
  11. Public static final int POWER_OFF_WAKEUP
  12.  
  13. //能唤醒系统,它是一种关机闹铃,就是说设备在关机状态下也可以唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为
  14. 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等描述让系统自动找到该行为处理对象。

 

 

 

 

 
  1. Intent intent = new Intent(AlarmController. this , OneShotAlarm. class );
  2. 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
  1. 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.

  1. mAlarmSender = PendingIntent.getService(AlarmService.this 0 new Intent(AlarmService. this , AlarmService_Service. class ), 0 );

 

实例:

/Chapter08_Broadcast_AlarmManager/src/com/amaker/ch08/app/MainActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.ch08.app;  
  4.  
  5. import com.amaker.ch08.app.R;  
  6.  
  7. import android.app.Activity;  
  8. import android.app.AlarmManager;  
  9. import android.app.PendingIntent;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15.  
  16. /**  
  17.  *   
  18.  * 测试AlarmManager  
  19.  */  
  20. public class MainActivity extends Activity {  
  21.     // 声明Button  
  22.     private Button setBtn, cancelBtn;  
  23.     // 定义广播Action  
  24.     private static final String BC_ACTION = "com.amaker.ch08.app.action.BC_ACTION";  
  25.  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         // 设置当前布局视图  
  30.         setContentView(R.layout.main);  
  31.           
  32.         // 实例化Button  
  33.         setBtn = (Button) findViewById(R.id.Button01);  
  34.         cancelBtn = (Button) findViewById(R.id.Button02);  
  35.           
  36.         // 获得AlarmManager实例  
  37.         final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);  
  38.           
  39.         // 实例化Intent  
  40.         Intent intent = new Intent();  
  41.         // 设置Intent action属性  
  42.         intent.setAction(BC_ACTION);  
  43.         intent.putExtra("msg", "你该去开会啦!");  
  44.         // 实例化PendingIntent  
  45.         final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,  
  46.                 intent, 0);  
  47.         // 获得系统时间  
  48.         final long time = System.currentTimeMillis();  
  49.  
  50.         // 设置按钮单击事件  
  51.         setBtn.setOnClickListener(new OnClickListener() {  
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 // 重复提示,从当前时间开始,间隔5秒  
  55.                 am.setRepeating(AlarmManager.RTC_WAKEUP, time,  
  56.  * 1000, pi);  
  57.             }  
  58.         });  
  59.           
  60.         // 设置按钮单击事件  
  61.         cancelBtn.setOnClickListener(new OnClickListener() {  
  62.             @Override  
  63.             public void onClick(View v) {  
  64.                 am.cancel(pi);  
  65.             }  
  66.         });  
  67.     }  

/Chapter08_Broadcast_AlarmManager/src/com/amaker/ch08/app/MyReceiver.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.ch08.app;  
  4.  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.widget.Toast;  
  9.  
  10. public class MyReceiver extends BroadcastReceiver {  
  11.     @Override  
  12.     public void onReceive(Context context, Intent intent) {  
  13.         // 获得提示信息  
  14.         String msg = intent.getStringExtra("msg");  
  15.         // 显示提示信息  
  16.         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();  
  17.     }  

/Chapter08_Broadcast_AlarmManager/res/layout/main.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9. <Button   
  10.     android:text="设置闹钟"   
  11.     android:id="@+id/Button01"   
  12.     android:layout_width="wrap_content"   
  13.     android:layout_height="wrap_content"></Button> 
  14. <Button   
  15.     android:text="取消闹钟"   
  16.     android:id="@+id/Button02"   
  17.     android:layout_width="wrap_content"   
  18.     android:layout_height="wrap_content"></Button> 
  19. </LinearLayout> 

/Chapter08_Broadcast_AlarmManager/AndroidManifest.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  5.       package="com.amaker.ch08.app" 
  6.       android:versionCode="1" 
  7.       android:versionName="1.0"> 
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".MainActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.           
  17.         <receiver android:name="MyReceiver"> 
  18.             <intent-filter> 
  19.                 <action android:name="com.amaker.ch08.app.action.BC_ACTION"/> 
  20.             </intent-filter> 
  21.         </receiver> 
  22.  
  23.     </application> 
  24.     <uses-sdk android:minSdkVersion="3" /> 
  25.  
  26. </manifest> 

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079377



相关文章
|
4月前
|
Java 关系型数据库 数据库
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
36 0
|
7月前
|
编解码 Android开发 开发者
Android平台RTMP多实例推送的几种情况探讨
好多开发者提到,如何实现Android平台,多实例推送,多实例推送,有几种理解: 1. 多路编码,多个实例分别推送到不同的RTMP URL(如Android采集板卡同时接2路出去); 2. 同一路编码,多个实例分别推送到不同的RTMP URL(如推送到内网、外网不同的RTMP服务器); 3. 部分路编码、部分路对接编码后的H.264/AAC数据,多个实例分别推送到不同的RTMP URL(混合推)。
|
3月前
|
Shell Android开发 数据安全/隐私保护
安卓逆向 -- Frida环境搭建(HOOK实例)
安卓逆向 -- Frida环境搭建(HOOK实例)
40 0
|
4月前
|
XML Java Android开发
Android Studio App开发之监听系统广播Broadcast的讲解及实战(包括接收分钟到达广播、网络变更广播、定时管理器等 附源码)
Android Studio App开发之监听系统广播Broadcast的讲解及实战(包括接收分钟到达广播、网络变更广播、定时管理器等 附源码)
81 0
|
4月前
|
XML 安全 Java
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
39 0
|
6月前
|
Shell Android开发 数据安全/隐私保护
安卓逆向 -- Frida环境搭建(HOOK实例)
安卓逆向 -- Frida环境搭建(HOOK实例)
90 0
|
8月前
|
Android开发
Android 中使用AlarmManager设置闹钟详解
Android 中使用AlarmManager设置闹钟详解
199 0
|
10月前
|
Android开发
Android:四大组件之 Broadcast(广播)
Broadcast 是一种广泛运用的、在应用程序之间传输信息的机制,Android 中的广播与传统意义上的电台广播类似,一个广播可以有任意个接收者,当然也可能不被任何应用程序所接收。广播机制是一个典型的发布-订阅模式,也就是观察者模式。
70 0
Android:四大组件之 Broadcast(广播)
|
消息中间件 安全 API
Android 面试(三):用广播 BroadcastReceiver 更新 UI 界面真的好吗?
这是 面试系列 的第三期。本期我们将来探讨一下 Android 四大组件的重要组成部分:广播 BroadcastReceiver。 往期内容传递:Android 面试:说说 Android 的四种启动模式Android 面试:如何理解 Activity 的生命周期 前言 BroadcastReceiver 作为 Android 四大组件之一,应用场景可谓非常之多。
1787 0