`
baobaoupup
  • 浏览: 470375 次
文章分类
社区版块
存档分类
最新评论

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. packagecom.example.android.apis.app;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.content.BroadcastReceiver;
  5. importandroid.widget.Toast;
  6. //Needthefollowingimporttogetaccesstotheappresources,sincethis
  7. //classisinasub-package.
  8. importcom.example.android.apis.R;
  9. /**
  10. *Thisisanexampleofimplementan{@linkBroadcastReceiver}foranalarmthat
  11. *shouldoccuronce.
  12. *<p>
  13. *Whenthealarmgoesoff,weshowa<i>Toast</i>,aquickmessage.
  14. */
  15. publicclassOneShotAlarmextendsBroadcastReceiver
  16. {
  17. @Override
  18. publicvoidonReceive(Contextcontext,Intentintent)
  19. {
  20. Toast.makeText(context,R.string.one_shot_received,Toast.LENGTH_SHORT).show();
  21. }
  22. }

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

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

3.在Activity中启动定时发送

  1. //Whenthealarmgoesoff,wewanttobroadcastanIntenttoour
  2. //BroadcastReceiver.HerewemakeanIntentwithanexplicitclass
  3. //nametohaveourownreceiver(whichhasbeenpublishedin
  4. //AndroidManifest.xml)instantiatedandcalled,andthencreatean
  5. //IntentSendertohavetheintentexecutedasabroadcast.
  6. Intentintent=newIntent(AlarmController.this,OneShotAlarm.class);
  7. PendingIntentsender=PendingIntent.getBroadcast(AlarmController.this,
  8. 0,intent,0);
  9. //Wewantthealarmtogooff30secondsfromnow.
  10. Calendarcalendar=Calendar.getInstance();
  11. calendar.setTimeInMillis(System.currentTimeMillis());
  12. calendar.add(Calendar.SECOND,30);
  13. //Schedulethealarm!
  14. AlarmManageram=(AlarmManager)getSystemService(ALARM_SERVICE);
  15. am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender);

三、自定义广播

1.发送广播

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

2.在AndroidManifest.xml中注册此Receiver

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

3.编辑Receiver

  1. packageandroid.example;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.media.MediaPlayer;
  6. importandroid.util.Log;
  7. publicclassMyBroadRecieverextendsBroadcastReceiver{
  8. //如果接收的事件发生
  9. @Override
  10. publicvoidonReceive(Contextcontext,Intentintent){
  11. //对比Action决定输出什么信息
  12. if(intent.getAction().equals("myAction")){
  13. Log.v("MyBroadReciever","onReceive");
  14. }
  15. }
  16. }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics