一、使用BroadcastRceiver

1、创建BroadcastRceiver(MyRceiver),重写OnReceiver:

  public void onReceive(Context context, Intent intent) {
    System.out.println("接收到了消息,消息内容是:"+intent.getStringExtra("data"));
  }

2、在主布局添加一个按钮:

  <Button
    android:text="发送消息"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnSendMsg" />

3、实现发送消息按钮的监听功能:

  findViewById(R.id.btnSendMsg).setOnClickListener(this);

  public void onClick(View v) {
    switch(v.getId()){
      case R.id.btnSendMsg:
        Intent i = new Intent(this,MyReceiver.class);
        i.putExtra("data","Android");
        sendBroadcast(i);
      break;
    }
  }

二、动态注册和注销BroadcastReceiver

  在某种情况,我们并不希望BroadcastReceiver始终从处于监听状态。这就需要我们动态地注册和注销BroadcastReceiver。

1、删除AndroidManifest.xml中关于MyReceiver的配置,则不能再发送消息:

  <receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true"></receiver>

2、在主布局中添加注册和注销按钮:

  <Button
    android:text="注册接收器"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnRes" />

  <Button
    android:text="注销接收器"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnUnres" />

3、MyReceiver文件:

  public static final String ACTION = "com.w.learnbroadcastrceiver.intent.action.MyReceiver";

4、功能实现:

  //防止重复注册多个Receive
  private MyReceiver receiver = null;

  findViewById(R.id.btnRes).setOnClickListener(this);
  findViewById(R.id.btnUnres).setOnClickListener(this);

  public void onClick(View v) {
    switch(v.getId()){
      case R.id.btnSendMsg:
        // Intent i = new Intent(this,MyReceiver.class);
        Intent i = new Intent(MyReceiver.ACTION);   //隐式Intent
        i.putExtra("data","Android");
        sendBroadcast(i);
        break;
      case R.id.btnRes:
        if(receiver == null){
          receiver = new MyReceiver();
          registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
        }
        break;
      case R.id.btnUnres:
        if(receiver != null){
          unregisterReceiver(receiver);
          receiver = null;
        }
        break;
    }
  }

三、BroadcastRceiver的优先级

1、Androidmanifest.xml中后注册的Receiver先接收到消息,即后注册的Receiver优先级高。

  <intent-filter>中属性 android:priority="10",优先级越高就越先接收到消息

2、优先级高的receiver不想让后续的receiver接收到消息,如何做(MyReceiver.java)?

    public void onReceive(Context context, Intent intent) {
      System.out.println("MyReceiver 接收到了消息");
      abortBroadcast();  //中断广播(MyReceiver比MyReceiver1优先级高的情况)
    }

  而MainActivity.java中:

    //sendBroadcast(i);
    sendOrderedBroadcast(i,null);  //参数分别代表Intent对象和权限

Android广播接收器BroadcastRceiver的更多相关文章

  1. android广播接收器

    Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...

  2. Xamarin.Android广播接收器与绑定服务

    一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...

  3. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  4. Android广播接收器和Activity间传递数据

    Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...

  5. Android广播接收器里弹出对话框

    不多说,直接上车... public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(fina ...

  6. android广播接收器BroadcastReceiver

    首先看一下什么是 BroadcastReceiver BroadcastReceiver:直译是"广播接收者",所以它的作用是用来接收发送过来的广播的. 那我们有必要知道:什么是广 ...

  7. (八)Android广播接收器BroadcastReceiver

    一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...

  8. Android -- 简单广播接收与发送(2)--动态注册广播接收器

    1. 效果图

  9. Android基础总结(4)——广播接收器

    在Android中的每个应用程序可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能来自于系统的,也可能来自于其他应用程序的.Android提供了一整套完整的API, ...

随机推荐

  1. mysql limit分页查询优化写法

    在mysql中进行分页查询时,一般会使用limit查询,而且通常查询中都会使用orderby排 序.但是在表数据量比较大的时候,例如查询语句片段limit 10000, 20,数据库会读取10020条 ...

  2. Python测试函数的方法之一

    Python测试函数的方法之一 首先介绍简单的try......except尝试运行的放例如下面的图和代码来简单介绍下: 注释:提醒以下代码环境为2.7.x 请3.x以上的同学们老规矩print(把打 ...

  3. ajax请求成功后打开新开窗口(window.open())被拦截的解决方法

    问题:今天在做项目时需要在ajax请求成功后打开一个新的窗口,此时遇到浏览拦截了新窗口的问题,尝试在ajax 回调函数中模拟执行 click 或者 submit 等用户行为(trigger('clic ...

  4. An error occurred during the installation of assembly 'Microsoft.VC90.CRT……的问题

    有一段时间没有用到AnkhSvn了,今天工作需要安装了一下.结果安装到一半就无法继续了,提示An error occurred during the installation of assembly ...

  5. [No0000A1]人体排毒时间表,别再信了

    经常可以看到有「人体排毒时间表」这样的说法,不同的媒体反复传播,大同小异.这些说法里,大多把人体的系统器官都给安排了一个特定的时段,认为在某时段是某器官的排毒时间,睡觉能排一切毒.事实上果真如此么?让 ...

  6. font-size 兼容问题

    早年~ 楔子 在为“我的抵扣券”添加  按钮时,为了将文字隐掉,给节点设置了“font-size:0;”,设置后刷一下浏览器,webkit下按钮掉下去了,而其他浏览器(包括IE6/7)都正常: 按理说 ...

  7. [LeetCode] Combine Two Tables 联合两表

    Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...

  8. 关于js的回调函数的一点看法

    算了一下又有好几个月没写博客了,最近在忙公司android的项目,所以也就很少抽时间来写些东西了.刚闲下来,我就翻了翻之前看的东西.做了android之后更加感觉到手机端开发的重要性,现在做nativ ...

  9. C/C++头文件区别

    在从C迁移到C++时,引用的头文件经常忘记是C的还是C++特有的 1. *.h   limits.h ctype.h 2. c* climits cctype [结尾不含.h] 3. 其余的都属于C+ ...

  10. redis分片

    本文是在window环境下测试 什么是分片 当数据量大的时候,把数据分散存入多个数据库中,减少单节点的连接压力,实现海量数据存储 那么当多个请求来取数据时,如何知道数据在哪个redis呢,redis有 ...