注释:一般广播不会被阻断,有序广播则会被阻断

注释:这是用动态注册的广播,必须要解绑

xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chenshuai.myapplication.Activityreceiver"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送一般广播"
android:textSize="20sp"
android:onClick="yibanguangboonclick"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送有序广播"
android:textSize="20sp"
android:onClick="youxuguangboonclick"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="动态注册广播接收器"
android:textSize="20sp"
android:onClick="dongtaizconclick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解注册广播接收器"
android:textSize="20sp"
android:onClick="jiezconclick"/> </LinearLayout>
Activityreceiver.java
package com.example.chenshuai.myapplication;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; public class Activityreceiver extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activityreceiver);
} //发送一般广播
public void yibanguangboonclick(View view)
{
//发送一般广播
//1.准备Intent
Intent intent = new Intent("com.example.chenshuai.action"); intent.putExtra("data","广播发送了,你要注意了"); //2.发送
sendBroadcast(intent); Toast.makeText(Activityreceiver.this, "我发送了广播", Toast.LENGTH_SHORT).show(); }
//发送有序广播
public void youxuguangboonclick(View view)
{
//发送有序广播
//1.准备Intent
Intent intent = new Intent("com.example.chenshuai.action"); intent.putExtra("data","有序广播发送了,你要注意了"); //2.发送
sendOrderedBroadcast(intent, null); Toast.makeText(Activityreceiver.this, "我发送了有序广播", Toast.LENGTH_SHORT).show(); } MyReceiver_receiver2 myReceiver_receiver2;
public void dongtaizconclick(View view)
{
//动态注册
//1- 实例化接收器
if (myReceiver_receiver2 == null) {
myReceiver_receiver2 = new MyReceiver_receiver2(); //2- 实例化IntentFilter
IntentFilter intentFilter = new IntentFilter("com.example.chenshuai.action");
intentFilter.setPriority(1000);
//3- 注册
registerReceiver(myReceiver_receiver2, intentFilter);
}
} //解动态注册
public void jiezconclick(View view)
{
if (myReceiver_receiver2 != null)
{
unregisterReceiver(myReceiver_receiver2); myReceiver_receiver2 = null; Toast.makeText(Activityreceiver.this, "解注册接收器", Toast.LENGTH_SHORT).show();
}
} //防止用户忘记解注册
@Override
protected void onDestroy() { super.onDestroy();
if (myReceiver_receiver2 != null)
{
unregisterReceiver(myReceiver_receiver2); }
}
}
MyReceiver_receiver.java
package com.example.chenshuai.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; public class MyReceiver_receiver extends BroadcastReceiver {
public MyReceiver_receiver() { Log.e("TAG","构造广播接收器");
} @Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented"); String str = intent.getStringExtra("data"); Log.e("TAG","收到广播了"+str);
}
}
MyReceiver_receiver2.java
package com.example.chenshuai.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast; public class MyReceiver_receiver2 extends BroadcastReceiver {
public MyReceiver_receiver2() { Log.e("TAG","构造广播接收器2");
} @Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented"); String str = intent.getStringExtra("data"); //处理广播
Log.e("TAG","收到广播了2"+str); Toast.makeText(context, "我阻断了有序广播", Toast.LENGTH_SHORT).show(); //判断是否是有序广播
if (isOrderedBroadcast())
{
abortBroadcast(); Log.e("TAG","我阻断了广播");
}
}
}

minifest.xml

 <receiver
android:name=".MyReceiver_receiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.chenshuai.action"/>
</intent-filter>
</receiver>
<!-- <receiver
android:name=".MyReceiver_receiver2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="com.example.chenshuai.action"/>
</intent-filter>
</receiver>-->

测试静态广播时在里面注册,测试动态广播时不需要。

Android——BroadcastReceiver的更多相关文章

  1. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  2. Android BroadcastReceiver 简介

    Android BroadcastReceiver 简介  在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver     活动(A ...

  3. 4、android BroadcastReceiver详细用法

    BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...

  4. android BroadcastReceiver

    AndroidManifast.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  5. Android BroadcastReceiver广播接受者

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

  6. Android BroadcastReceiver实时监听电量

    Android系统中实时的监听手机电量以及开机启动功能都是通过BroadcastReceiver组件实现的.我们可以动态注册这个类的一个实例通过 Context.registerReceiver()方 ...

  7. Android BroadcastReceiver实例Demo(有序广播的发送)

    上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...

  8. Android BroadcastReceiver 接收收到短信的广播

    一.知识介绍 1.broadcastReceiver是广播接受者,四大组件之一. 2.Android中内置了很多系统级别的广播,可以在应用程序中得到各种系统的状态信息. 3.使用场景: ①当手机没有电 ...

  9. android BroadcastReceiver组件简单的使用

    1.清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=& ...

  10. Android BroadcastReceiver解析

    目录   示意图 1. 定义 即 广播,是一个全局的监听器,属于Android四大组件之一 Android 广播分为两个角色:广播发送者.广播接收者 2. 作用 监听 / 接收 应用 App 发出的广 ...

随机推荐

  1. java中如何将byte[]里面的数据转换成16进制字符串

    原文链接: http://zhidao.baidu.com/link?url=RmLDjr4PtP_oUE5J2pKNZSvlHt1K7HcCh4-03Y7VkXYhJ0kawg01CtKHZc2uB ...

  2. SharePoint 2013 Farm 安装指南——Least Privilege

    写过很多关于SharePoint 2013 安装,这是第四篇.可能你会觉得为什么如此简单的安装至于花那么多精力去折腾吗.我的答案是肯定的.知识的积累不是一蹴而就的,而是循序渐进的去学习,每一个阶段都有 ...

  3. 适合移动端与PC端的 CSS Reset - m_base.css

    文章来源:http://www.cnblogs.com/HCJJ/p/6399185.html 具体代码 @charset "utf-8"; html { -ms-text-siz ...

  4. VMware Linux 共享文件夹 虚拟机无共享文件解决方法

    恢复虚拟机ubuntu后,首先,点击 虚拟机——安装VMware Tools(Install VMware Tools),这时我们会在Ubuntu系统桌面上发现VMware Tools的光盘图标. 双 ...

  5. [转]PostgreSQL 逻辑结构 和 权限体系 介绍

    摘要: 本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每 ...

  6. django缓存基于类的视图

    引子: 有些场景下由于数据库中的数据很长时间内都不会发生变化,如果每次调用view都要去查询一次数据库的话,明显是多做了一些无用 工的:django中可以通过装饰器方便的实现view的缓存. 为什么c ...

  7. SecureCRT中使用 rz 上传文件 遇到 rz: command not found 的解决办法

    -bash: rz: command not foundrz命令没找到?执行sz,同样也没找到. 安装lrzsz: # yum -y install lrzsz 现在就可以正常使用rz.sz命令上传. ...

  8. Fluent UDF【2】:学习途径

    要怎样做才能做到无畏惧编写任何UDF程序?估计很多与UDF打交道的人都会问到这个问题. 面对UDF文档中那众多的宏描述,小伙伴们是不是感觉到茫然无措,不知从何入手.有时候读别人写好的程序感觉并不难,然 ...

  9. Berkeley Packet Filter (BPF) BCC

    http://www.brendangregg.com/ebpf.html https://qmonnet.github.io/whirl-offload/2016/09/01/dive-into-b ...

  10. Java 必须掌握的 12 种 Spring 常用注解!

    1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller ...