Android——BroadcastReceiver



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

注释:这是用动态注册的广播,必须要解绑
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的更多相关文章
- android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到
android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...
- Android BroadcastReceiver 简介
Android BroadcastReceiver 简介 在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver 活动(A ...
- 4、android BroadcastReceiver详细用法
BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...
- android BroadcastReceiver
AndroidManifast.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Android BroadcastReceiver广播接受者
静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用 方式一:sendBroadCastReceive 广播的步骤: 发送 无序广播,普通广播 (1).发送方 ...
- Android BroadcastReceiver实时监听电量
Android系统中实时的监听手机电量以及开机启动功能都是通过BroadcastReceiver组件实现的.我们可以动态注册这个类的一个实例通过 Context.registerReceiver()方 ...
- Android BroadcastReceiver实例Demo(有序广播的发送)
上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...
- Android BroadcastReceiver 接收收到短信的广播
一.知识介绍 1.broadcastReceiver是广播接受者,四大组件之一. 2.Android中内置了很多系统级别的广播,可以在应用程序中得到各种系统的状态信息. 3.使用场景: ①当手机没有电 ...
- android BroadcastReceiver组件简单的使用
1.清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=& ...
- Android BroadcastReceiver解析
目录 示意图 1. 定义 即 广播,是一个全局的监听器,属于Android四大组件之一 Android 广播分为两个角色:广播发送者.广播接收者 2. 作用 监听 / 接收 应用 App 发出的广 ...
随机推荐
- SharePoint 2013怎样创建Wiki库
们使用Wiki页面来分享知识,增进团队合作.下面我将向大家展示SharePoint 2013 Wiki的使用方法.教程我都将以这张Wiki页面(即当前页)为示例. 编辑页面 如要编辑页面,单击顶部Ed ...
- 第3章 Python基础-文件操作&函数 文件操作 练习题
一.利用b模式,编写一个cp工具,要求如下: 1. 既可以拷贝文本又可以拷贝视频,图片等文件 2. 用户一旦参数错误,打印命令的正确使用方法,如usage: cp source_file target ...
- 物联网将在2018年实现大规模发展--IBM的四大预测
物联网将在2018年实现大规模发展--IBM的四大预测 数据是数字化变革的基本组成部分,物联网.人工智能.区块链.边缘计算等技术预计将在来年掀起巨浪, 因为这些技术是收集.分析和存储信息的方法. ...
- 滴滴passport设计之道:帐号体系高可用的7条经验
导读:应对高可用及极端峰值,每个技术团队都有自己的优秀经验,但是这些方法远没有得到体系化的讨论.高可用架构在 6 月 25 日举办了『高压下的架构演进』专题活动,进行了闭门私董会研讨及对外开放的四个专 ...
- go环境变量配置 (GOROOT和GOPATH)
GOROOT就是go的安装路径在~/.bash_profile中添加下面语句: GOROOT=/usr/local/go export GOROOT 当然, 要执行go命令和go工具, 就要配置go的 ...
- tips:解决bootstrap-switch 在jqgrid中动态加载不显示的问题
bootstrapo-switch 是一个十分好用的插件,用来关闭开启再好不过了,适合状态类型只有两种的情况下可以进行切换 在使用中,在jqgrid动态加载的时候出现不能加载的问题 原因是html代码 ...
- DDR3控制
很简单的,app_en和app_rdy一握手,代表MIG接受了一个写数据请求或者读数据请求,只要保证app_en和app_rdy握手,根本就不关心写数据rdy,这是MIG的一个bug,你看它源码就知道 ...
- async与await 实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 4-4-串的KMP匹配算法-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第4章 串 - KMP匹配算法 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码 ...
- .NET CORE EF 框架调用存储过程
; //多个参数多表组合值 SqlParameter[] Param = { new SqlParameter("@UserId", System.Data.SqlDbType.V ...