首先继承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. hdu 5505(GT and numbers)

    题意: 给你a和b,a每次和它的因子相乘得到一个新的a,求多少次后可以得到b. 输入样例 3 1 1 1 2 2 4 输出样例 0 -1 1 思路: 每次找出a和b/a的最大公约数(即当前a想得到b能 ...

  2. HWM、PCTFREE、PCTUSED

    什么是水线(High Water Mark)? HWM通常增长的幅度为一次5个数据块,原则上HWM只会增大,不会缩小,即使将表中的数据全部删除,HWM还是为原值,由于这个特点,使HWM很象一个水库的历 ...

  3. JS按照指定的周期来调用函数方法

    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 s ...

  4. python类库numpy中常见函数的用法

    1. numpy.reshape  重塑 reshape是一种函数,函数可以重新调整矩阵的行数.列数.维数. B = reshape(A,m,n) 返回一个m*n的矩阵B, B中元素是按列从A中得到的 ...

  5. 《Java技术》第三次作业

    (一)学习总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Gr ...

  6. Linux常用命令大全(归类)

    最近都在和Linux打交道,这方面基础比较薄弱的我只好买了本鸟哥的书看看,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因, ...

  7. 配置文件错误导致jenkins无法启动 org.xmlpull.v1.XmlPullParserException: only 1.0 is supported as <?xml version not '1.1' (position: START_DOCUMENT seen <?xml version=\'1.1\'... @1:19)

    org.xmlpull.v1.XmlPullParserException: only 1.0 is supported as <?xml version not '1.1' (position ...

  8. random 模块

    import stringprint (random.random()) # 0-1之间选浮点数print (random.randint(0,99,))#0-99之间选任意整数print (rand ...

  9. delphi 面向对象实用技能教学一(递归)

    本例使用类与TList相结合,用简洁的方法,实现了一个 HTML 解析与格式化功能.所用到的知识点如下:1.类的提前申明2.TList用法3.String的指针操作4.单例设计5.递归用法 编程是综合 ...

  10. Oracle数据库常用命令记录

    1.Sql建表 CREATE TABLE AAABBBCCCDDD( ID ) primary key, AAAAAAAA ) not NULL, BBBBBBBB ), CCCCCCCC ), DD ...