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 发出的广 ...
随机推荐
- php把采集内容中图片地址下载并替换成本地地址
把字符串中地址全部获取到一个数组我们利用preg_match_all函数 代码如下 复制代码 <?php$str='<p><img border="0" s ...
- 7.翻译:EF基础系列---EF中的实体类型
原文地址:http://www.entityframeworktutorial.net/Types-of-Entities.aspx 在Entity Framework中有两种实体类型:一种是POCO ...
- Proxy源代码分析——谈谈如何学习Linux网络编程
Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...
- 从代码上解决Jenkins 发送邮件中文乱码问题
在实践中,使用Jenkins发送测试报告,收到邮件,邮件内容中的中文为乱码,邮件发送的方式是在Jenkins发邮件设置中设置邮件内容为:${FILE,path="report_ug.html ...
- p标签不折行的问题
问题描述: 一个固定宽度的div里面包了一个p元素,由于p元素中的文字比较长并且没有换行,最终看到的效果就是p中的 文字“跑”了出来. 问题复现: <div style="width: ...
- ${HADOOP_CONF_DIR:-$HADOOP_PREFIX/$DEFAULT_CONF_DIR}
在hadoop-config.sh中,有如下语句:${HADOOP_CONF_DIR:-$HADOOP_PREFIX/$DEFAULT_CONF_DIR} ...
- 【剑道】日常练习相关Q&A 整理
Q:如何使手腕灵活,手指灵活.有力量? A: 1)提重物.将手腕搁在膝盖上,凭手指和手腕的力量将重物提上来 2)指卧撑.用十个指头着地的方法做俯卧撑 Q:怎样才算肩膀放松,如何方式? A:收放自如,多 ...
- jQuery学习笔记(jquery.simplemodal插件)
官网地址:http://www.ericmmartin.com/ SimpleModal是一个轻量级的jQuery插件,它为模式窗口的开发提供了一个强有力的接口,可以把它当作模式窗口的框架.Simpl ...
- 如何使Android应用支持多种屏幕分辨率
原文:http://android.eoe.cn/topic/android_sdk 描述: 让您指定您的应用支持的屏幕的大小并且可以通过屏幕兼容模式来支持比您应用所支持更大的屏幕.所以这对于您需要在 ...
- Fiddlercore Demo - Fiddler
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Loa ...