首先继承BroadcastReceiver类,并在manifest中注册

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");

    }
}

在mainifest中注册

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

动态注册和取消广播接收器

上代码:

Receiver部分:

public class MyReceiver extends BroadcastReceiver {
    public static final String ACTION = "peng.liu.testview.intent.action.MyReceiver";
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getStringExtra("data")+"hello");
    }
}

主类部分:

public class MainActivity extends Activity implements View.OnClickListener{
    private MyReceiver receiver = null;
    private Button send,reg,unReg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.send).setOnClickListener(this);
        findViewById(R.id.reg).setOnClickListener(this);
        findViewById(R.id.unReg).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send:
                Intent intent = new Intent(MyReceiver.ACTION);
                intent.putExtra("data","jiekxueyuan");
                sendBroadcast(intent);
                break;
            case R.id.reg:
                if (receiver == null){
                    receiver = new MyReceiver();
                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
                }
                break;
            case R.id.unReg:
                if (receiver != null){
                    unregisterReceiver(receiver);
                    receiver = null;
                }
                break;

        }
    }
}

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reg"
        android:id="@+id/reg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unReg"
        android:id="@+id/unReg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        android:id="@+id/send" />
</LinearLayout>

广播的优先级

这次我们在manifest中静态注册

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="8">
                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="9">
                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>
            </intent-filter>
        </receiver>

android:priority:用于设置优先级,数字越大,优先级越高。

高优先级的终端广播

//发送部分注意是发送sendOrderedBroadcast(intent,null);
Intent intent = new Intent(MyReceiver.ACTION);
                intent.putExtra("data","jiekxueyuan");
                sendOrderedBroadcast(intent,null);

接收部分

public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getStringExtra("data"));
        //这一户用于中断后面的低优先级的接受
        abortBroadcast();
    }
}

Android广播接收器Broadcast Receiver-android学习之旅(十二)的更多相关文章

  1. Android列表视图ListView和ListActivity-android学习之旅(二十四)

    ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...

  2. Spring学习之旅(十二)--持久化框架

    对于本职工作来说 JDBC 就可以很好的完成,但是当我们对持久化的需求变得更复杂时,如: 延迟加载 预先抓取 级联 JDBC 就不能满足了,我们需要使用 ORM框架 来实现这些需求. Spring 对 ...

  3. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...

  4. android广播接收器BroadcastReceiver

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

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

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

  6. android广播接收器

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

  7. 我的MYSQL学习心得(十二) 触发器

    我的MYSQL学习心得(十二) 触发器 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数 ...

  8. VSTO 学习笔记(十二)自定义公式与Ribbon

    原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...

  9. 我的MYSQL学习心得(十二)

    原文:我的MYSQL学习心得(十二) 我的MYSQL学习心得(十二) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYS ...

随机推荐

  1. ●UOJ 131 [NOI2015] 品酒大会

    题链: http://uoj.ac/problem/131 题解: 网上大多数的方法都是用并查集维护.这里呢,给出另一种自己YY的解法(但实际上本质差不多吧): 后缀数组,RMQ,单调栈 1).预处理 ...

  2. MYSQL 二进制安装

    系统环境:CentOs6.7 i386 Mysql版本:mysql-5.6.36 root登录linux cd pwd #/root/ wget http://mirrors.sohu.com/mys ...

  3. Python Web学习笔记之多道程序设计技术和操作系统的特性

    采用了多道程序设计技术的操作系统具有如下特性 : ① 并发性.它 是指两个或两个以上的事件或活动在同一时间间隔内发生.操作系统是一个并发系统,并发性是它的重要特征,操作系统的并发性指计算机系统中同时存 ...

  4. python,for,while循环控制

    1.for循环 for循环 for i in range(0,5): for j in range(0,5): print('#'*5) 2.while 循环 import random #get n ...

  5. 360面试-C++后端(实习)

    在线远程视频面试 一面: 自我介绍. 知道哪几种排序算法,各算法的时间复杂度. 解决hash冲突的几种方式. 有哪些方法清除cache中旧的数据.不太清楚,我扯到了操作系统中缺页中断的页面置换原理上, ...

  6. 关于Matchvs一些使用心得与建议

    我的项目是类似<贪吃蛇>玩法的一款IO游戏,就是几个玩家在游戏界面中可以吃食物,也可以相互吃,吃了食物或对方都会变大这样子.我是在用cocos creator做完前端开发的部分后,开始接入 ...

  7. Ajax来实现下拉框省市区三级联动效果(服务端基于express)

    //服务端JS代码: //提供服务端的处理 const express = require('express'); const fs = require('fs'); const app = expr ...

  8. java海量大文件数据处理方式

    1. 给定a.b两个文件,各存放50亿个url,每个url各占64字节,内存限制是4G,让你找出a.b文件共同的url? 方案1:可以估计每个文件安的大小为50G×64=320G,远远大于内存限制的4 ...

  9. ionic3-ng4学习见闻--(多环境方案)

    搜了很久,很难找到一个详细入微,开箱即用的方案. 于是我 百折不挠的,搞出来一个,也不知道是不是最完美的方案,有什么可以优化的地方可以指出,谢谢. 首先, 1.项目目录下(与src平级),新增conf ...

  10. Vue nextTick 机制

    背景 我们先来看一段Vue的执行代码: export default { data () { return { msg: 0 } }, mounted () { this.msg = 1 this.m ...