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 发出的广 ...
随机推荐
- C#基础第六天-作业-利用面向对象的思想去实现名片
1.利用面向对象的思想去实现: (增加,修改,删除,查询,查询全部)需求:根据人名去(删除/查询).指定列:姓名,年龄,性别,爱好,电话. 本系列教程: C#基础总结之八面向对象知识点总结-继承与多态 ...
- Linux 密码过期(WARNING:Your password has expired )
最近遇到两次这个问题,我们公司用的是开源的堡垒机Jumpserver但是最近有两个同学遇到了 WARNING:Your password has expired 第一次遇到这个问题也没有往深了去查,当 ...
- activiti并行和串行区别
多实例还有并行.串行区分.以下解释一下什么是并行与串行 并行代表同时进行,如把任务分给5个人来处理,这5个人同时会收到任务,并且可以同时处理,不受各自的影响. 串行代表工作或任务由一个人完成后,再由另 ...
- 复习下C 链表操作(单向链表)
Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...
- rdesktop 源码安装
# ./configure --prefix=/data/apps/rdesktop-1.6.0 #make && make install
- 【小白的CFD之旅】小结及预告
这是小白系列的索引,后续会继续更新. 已更新的部分 01 引子02 江小白03 老蓝04 任务05 补充基础06 流体力学基础07 CFD常识08 CFD速成之道09 初识FLUENT10 敲门实例1 ...
- 微信JSAPI 公众号支付 H5支付以及APP支付 WEBAPI接口开发测试
统一下单入口 调用该方法入口: public void WxPayAPI() { //string PayPrice ="99.9"; ////订单号 //string Payor ...
- windows操作系统自带的TCP端口转发
假定需要通过192.168.1.8的14941端口连接192.168.1.118的1494端口,则需要在192.168.1.8主机的命令行输入如下语句netsh interface ipv6 ins ...
- 【Mysql】linux连接mysql错误解决方案
1.源码 //connect-mysql.c #include <stdio.h> #include "/usr/include/mysql/mysql.h" int ...
- Context.startActivity出现AndroidRuntimeException
转:http://hi.baidu.com/huaxinchang/item/e1a771cf4d424312b77a2416 昨天做了一个Activity的启动动画,效果是点击桌面图标先出现动画后启 ...