Android基本之Broadcast Receiver

 

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 内容如下:

  1. package com.example.android.apis.app;  
  2.   
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.content.BroadcastReceiver;  
  6. import android.widget.Toast;  
  7.   
  8. // Need the following import to get access to the app resources, since this   
  9. // class is in a sub-package.   
  10. import com.example.android.apis.R;  
  11.   
  12. /** 
  13.  * This is an example of implement an {@link BroadcastReceiver} for an alarm that 
  14.  * should occur once. 
  15.  * <p> 
  16.  * When the alarm goes off, we show a <i>Toast</i>, a quick message. 
  17.  */  
  18. public class OneShotAlarm extends BroadcastReceiver  
  19. {  
  20.     @Override  
  21.     public void onReceive(Context context, Intent intent)  
  22.     {  
  23.         Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show();  
  24.     }  
  25. }  

2、在AndroidManifest.xml中注册此Receiver :

  1. <receiver android:name=".app.OneShotAlarm" android:process=":remote" />  

3.在Activity中启动定时发送

  1. // When the alarm goes off, we want to broadcast an Intent to our   
  2.             // BroadcastReceiver.  Here we make an Intent with an explicit class   
  3.             // name to have our own receiver (which has been published in   
  4.             // AndroidManifest.xml) instantiated and called, and then create an   
  5.             // IntentSender to have the intent executed as a broadcast.   
  6.             Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);  
  7.             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,  
  8.                     0, intent, 0);  
  9.   
  10.             // We want the alarm to go off 30 seconds from now.   
  11.             Calendar calendar = Calendar.getInstance();  
  12.             calendar.setTimeInMillis(System.currentTimeMillis());  
  13.             calendar.add(Calendar.SECOND, 30);  
  14.   
  15.             // Schedule the alarm!   
  16.             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  17.             am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);  

三、自定义广播

1.发送广播

 

 

  1. //定义一个intent   
  2.                 Intent intent = new Intent().setAction(  
  3.                         "myAction").putExtra("myTag",  
  4.                         "myData");  
  5.                 //广播出去   
  6.                 sendBroadcast(intent);  

2.在AndroidManifest.xml中注册此Receiver

  1. <receiver android:name="MyBroadReciever">  
  2.           <intent -filter="">  
  3.                 <action android:name="myAction">  
  4.             </action></intent>  
  5.     </receiver>  

3.编辑Receiver

  1. package android.example;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.media.MediaPlayer;  
  7. import android.util.Log;  
  8.   
  9. public class MyBroadReciever extends BroadcastReceiver {  
  10.   
  11.     //如果接收的事件发生   
  12.     @Override  
  13.     public void onReceive(Context context, Intent intent) {  
  14.         //对比Action决定输出什么信息   
  15.         if(intent.getAction().equals("myAction")){  
  16.             Log.v("MyBroadReciever""onReceive");  
  17.         }  
  18.     }  
  19. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值