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

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

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. Groovy 学习手册(7)

    10. Groovy GPars GPars 一开始在 Groovy 中作为一个单独的项目,同时带来了很多并发的机制.它包含了很多并行的map/redue,Actors,以及其他很多并发的模块. 1. ...

  2. 【OpenCV】给图像加入噪声

    图像噪声使图像在获取或是传输过程中收到随机信号干扰,妨碍人们对图像理解及分析处理的信号.非常多时候将图像噪声看做多维随机过程,因而描写叙述噪声的方法全然能够借用随机过程的描写叙述,也就是使用随机过程的 ...

  3. POJ 2115:C Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19536   Accepted: 5204 Descr ...

  4. (面试题)有关Integer

    今天在一家公司做了个面试题:运行下列代码,输出结果是什么 Integer a=new Integer("12");  Integer b=new Integer("12& ...

  5. Android Activity全面解析

    Android Activity全面解析 首先,就从Android四大组件Activity开始. 1.Activity生命周期方法完全解析   activity_lifecycle.png 1).on ...

  6. mysql 中 delete 子查询的限制

    1 DELETE FROM tablename 中的 tablename 不能起别名 delete ; [Err] - You have an error in your SQL syntax; 2 ...

  7. python之celery使用详解一

    前段时间需要使用rabbitmq做写缓存,一直使用pika+rabbitmq的组合,pika这个模块虽然可以很直观地操作rabbitmq,但是官方给的例子太简单,对其底层原理了解又不是很深,遇到很多坑 ...

  8. Python-获取法定节假日

    获取公共节假日的接口,http://www.easybots.cn/holiday_api.net, 具体代码如下: # -*- coding:utf-8 -*- import json import ...

  9. Postman调用WebService,包括头验证部分

    Postman调用WebService时,Body中选择Raw,最右端选择XML(txt/xml),然后把某个方法显示在页面的xml拷贝到请求框中即可,如下图: 以下是postman中的设置,

  10. 菜鸟学Java(十一)——GET与POST

    相信大家在面试的时候经常会被问到:GET与POST有什么区别吧?你是怎么回答的呢?POST比GEt安全?GET有URL的长度限制而POST没有或者很大?GET通过URL或者Cookie传参数,POST ...