1、Broadcast Receiver简介
2、Broadcast Receiver接收定时发送的广播
3、自定义广播
一、Broadcast Receiver简介
Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么Broadcast Receiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
可以使用BroadcastReceiver使应用程序代码能够响应外部事件,如电话呼入、数据网络可用等。尽管BroadcastReceiver可以使用NotificationManager来提醒用户一些感兴趣的事件的发生,但是它并不显示用户界面。
BroadcastReceiver在AndroidManifest.xml中完成注册,也可以在代码中通过Context.registerReceiver()方法完成注册。
应用程序也可以通过Context.sendBroadcast()将自身的intent 广播给其他应用程序。
二、Broadcast Receiver接收定时发送的广播
做一个例子,功能是接收定时发送的广播。
1、建立OneShotAlarm.java 内容如下:
- package com.example.android.apis.app;
- import android.content.Context;
- import android.content.Intent;
- import android.content.BroadcastReceiver;
- import android.widget.Toast;
- // Need the following import to get access to the app resources, since this
- // class is in a sub-package.
- import com.example.android.apis.R;
- /**
- * This is an example of implement an {@link BroadcastReceiver} for an alarm that
- * should occur once.
- * <p>
- * When the alarm goes off, we show a <i>Toast</i>, a quick message.
- */
- public class OneShotAlarm extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show();
- }
- }
2、在AndroidManifest.xml中注册此Receiver :
- <receiver android:name=".app.OneShotAlarm" android:process=":remote" />
3.在Activity中启动定时发送
- // When the alarm goes off, we want to broadcast an Intent to our
- // BroadcastReceiver. Here we make an Intent with an explicit class
- // name to have our own receiver (which has been published in
- // AndroidManifest.xml) instantiated and called, and then create an
- // IntentSender to have the intent executed as a broadcast.
- Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
- PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
- 0, intent, 0);
- // We want the alarm to go off 30 seconds from now.
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- calendar.add(Calendar.SECOND, 30);
- // Schedule the alarm!
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
三、自定义广播
1.发送广播
- //定义一个intent
- Intent intent = new Intent().setAction(
- "myAction").putExtra("myTag",
- "myData");
- //广播出去
- sendBroadcast(intent);
2.在AndroidManifest.xml中注册此Receiver
- <receiver android:name="MyBroadReciever">
- <intent -filter="">
- <action android:name="myAction">
- </action></intent>
- </receiver>
3.编辑Receiver
- package android.example;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.util.Log;
- public class MyBroadReciever extends BroadcastReceiver {
- //如果接收的事件发生
- @Override
- public void onReceive(Context context, Intent intent) {
- //对比Action决定输出什么信息
- if(intent.getAction().equals("myAction")){
- Log.v("MyBroadReciever", "onReceive");
- }
- }
- }