上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送。

设置完相关属性的时候,广播就会依照有序的方式进行发送:

发送顺序:

先发送第二条广播;

再发送第一条广播;

最后发送第三条广播。

代码例如以下:

布局文件:

activity_main(一个Button):

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <Button
  12. android:id="@+id/button1"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentTop="true"
  16. android:layout_centerHorizontal="true"
  17. android:text="点击发送有序广播" />
  18.  
  19. </RelativeLayout>

MainActivity:

  1. package com.android_broadcasereceiverdemo2;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends Activity {
  10. private Button button;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15.  
  16. button = (Button) findViewById(R.id.button1);
  17. button.setOnClickListener(new View.OnClickListener() {
  18.  
  19. @Override
  20. public void onClick(View v) {
  21. Intent intent = new Intent();
  22. intent.setAction("myaction");
  23. intent.putExtra("name", "梅西");
  24. sendOrderedBroadcast(intent, null);
  25. }
  26. });
  27. }
  28. }

FirstBroadcast:

  1. package com.android_broadcasereceiverdemo2;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. public class FirstBroadcast extends BroadcastReceiver {
  9.  
  10. @Override
  11. public void onReceive(Context context, Intent intent) {
  12.  
  13. String name = intent.getStringExtra("name");
  14. Toast.makeText(context, "第一条广播已发送..."+name, Toast.LENGTH_SHORT).show();
  15. // abortBroadcast();//当加上这条代码的时候,广播发送到此结束,即第三条广播不会再收到。
  16. }
  17.  
  18. }

SecondBroadcast:

  1. package com.android_broadcasereceiverdemo2;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. public class SecondBroadcast extends BroadcastReceiver {
  9.  
  10. @Override
  11. public void onReceive(Context context, Intent intent) {
  12.  
  13. String name = intent.getStringExtra("name");
  14. Toast.makeText(context, "第二条广播已发送..."+name, Toast.LENGTH_SHORT).show();
  15. }
  16.  
  17. }

ThirdBroadcast:

  1. package com.android_broadcasereceiverdemo2;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. public class ThirdBroadcast extends BroadcastReceiver {
  9.  
  10. @Override
  11. public void onReceive(Context context, Intent intent) {
  12.  
  13. String name = intent.getStringExtra("name");
  14. Toast.makeText(context, "第三条广播已发送..."+name, Toast.LENGTH_SHORT).show();
  15. }
  16.  
  17. }

AndroidManifest.xml(非常关键):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android_broadcasereceiverdemo2"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="11"
  9. android:targetSdkVersion="18" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.android_broadcasereceiverdemo2.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21.  
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25.  
  26. <receiver android:name=".FirstBroadcast" >
  27. <!-- 有序广播中,priority数值越大,优先级越大,也就越先发送!! -->
  28. <intent-filter android:priority="99">
  29. <action android:name="myaction" />
  30. </intent-filter>
  31. </receiver>
  32.  
  33. <receiver android:name=".SecondBroadcast" >
  34. <intent-filter android:priority="100">
  35. <action android:name="myaction" />
  36. </intent-filter>
  37. </receiver>
  38.  
  39. <receiver android:name=".ThirdBroadcast" >
  40. <intent-filter android:priority="98">
  41. <action android:name="myaction" />
  42. </intent-filter>
  43. </receiver>
  44. </application>
  45.  
  46. </manifest>

须要源代码的读者能够到我的资源中下载。

Android BroadcastReceiver实例Demo(有序广播的发送)的更多相关文章

  1. Android ListFragment实例Demo(自己定义适配器)

    上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...

  2. Android ExpandableListView实例Demo

    前几篇文章介绍了Listview.但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview.基本思想与Listview大 ...

  3. Android广播的发送与接收

    Android广播的发送与接收 效果图 广播发送 广播分为有序广播和无序广播 有序广播与无序广播的区别 无序广播:只要是广播接收者指定了接收的事件类型,就可以接收到发送出来的广播消息.不能修改消息. ...

  4. Android BroadcastReceiver 发送有序广播

    普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...

  5. Android学习笔记(十二)BroadcastReceiver的有序广播和优先级

    前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...

  6. Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播:   1.   我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...

  7. Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...

  8. BroadcastReceiver之有序广播

    有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...

  9. Android BroadcastReceiver广播接受者

    静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用  方式一:sendBroadCastReceive   广播的步骤:       发送  无序广播,普通广播       (1).发送方    ...

随机推荐

  1. oracle中创建一个用户,只能查看指定的视图,如何授权,创建别名

    1.create user A identified by Apassword,创建用户,用户名是A,密码是Apassword2.grant connect to A --授予connect权限3.g ...

  2. dbms_job dbms_scheduler简单比较

    ---------------------------陈旧的-------------------------------------/*--------------------- 创建job --- ...

  3. Mysql软删除

    所谓软删除(Soft Deleting),即在删除数据表中的数据时,并不直接将其从数据表中删除,而是将其标志为删除,即在每张表中设置一个删除字段(如:IsDeleted)等,默认情况下其值为0,及未删 ...

  4. 重新开始学习javase_集合_List

    一,List之ArrayList(转:http://blog.csdn.net/zheng0518/article/details/42198205) 1. ArrayList概述: ArrayLis ...

  5. Shell 脚本编程笔记(一) Hello Shell

    最近不断在接触Linux操作系统,对它一个终端走天下的特性感到十分新奇和伟大.同时也被各种命令折磨的死去活来...公司的一个老同事给我讲,在公司的极品geek宅都是只用一个黑黑的框完成一切的.结果我一 ...

  6. Linux 查看文件内容的命令

    转载自:新浪博客 (观看档案内容 : cat, tac, more, less, head, tail, nl, 刚刚我们提到的都只是在于显示档案的外观,或者是移动与复制一个档案或目录而已,那么如果我 ...

  7. 【jquery学习笔记】关于$(window),$("html,body").scroll()的在不同浏览器的不同反应

    已经很几次碰到了这种问题, 例子: $(window).scroll(function(){ var num=$(window).scrollTop();              //之前的写法是$ ...

  8. PHP错误报告级别及调整方法

    运行PHP脚本时,PHP解析器会尽其所能能报告它遇到的问题.在PHP中错误报告的处理行为,都是通过PHP的配置文件php.ini中有关的配置指令确定的.另外PHP的错误报告有很多种级别,可以根据不同的 ...

  9. Python爬虫预备知识

    1.http编程知识 http中client 和server的工作模式 client和server建立可靠的tcp链接(在HTTP1.1中这个链接是长时间的,超时断开策略) client通过socke ...

  10. java中加载xml文件方法

    this.getclass().getclassloader().getresourceasstream(String file); 可以加载文件,比如xml.