Android广播接收器Broadcast Receiver-android学习之旅(十二)
首先继承BroadcastReceiver类,并在manifest中注册
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
在mainifest中注册
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
</receiver>
动态注册和取消广播接收器
上代码:
Receiver部分:
public class MyReceiver extends BroadcastReceiver {
public static final String ACTION = "peng.liu.testview.intent.action.MyReceiver";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getStringExtra("data")+"hello");
}
}
主类部分:
public class MainActivity extends Activity implements View.OnClickListener{
private MyReceiver receiver = null;
private Button send,reg,unReg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.send).setOnClickListener(this);
findViewById(R.id.reg).setOnClickListener(this);
findViewById(R.id.unReg).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send:
Intent intent = new Intent(MyReceiver.ACTION);
intent.putExtra("data","jiekxueyuan");
sendBroadcast(intent);
break;
case R.id.reg:
if (receiver == null){
receiver = new MyReceiver();
registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
}
break;
case R.id.unReg:
if (receiver != null){
unregisterReceiver(receiver);
receiver = null;
}
break;
}
}
}
布局代码:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reg"
android:id="@+id/reg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unReg"
android:id="@+id/unReg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:id="@+id/send" />
</LinearLayout>
广播的优先级
这次我们在manifest中静态注册
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="8">
<action android:name="peng.liu.testview.intent.action.MyReceiver"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="9">
<action android:name="peng.liu.testview.intent.action.MyReceiver"/>
</intent-filter>
</receiver>
android:priority:用于设置优先级,数字越大,优先级越高。
高优先级的终端广播
//发送部分注意是发送sendOrderedBroadcast(intent,null);
Intent intent = new Intent(MyReceiver.ACTION);
intent.putExtra("data","jiekxueyuan");
sendOrderedBroadcast(intent,null);
接收部分
public class MyReceiver2 extends BroadcastReceiver {
public MyReceiver2() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getStringExtra("data"));
//这一户用于中断后面的低优先级的接受
abortBroadcast();
}
}
Android广播接收器Broadcast Receiver-android学习之旅(十二)的更多相关文章
- Android列表视图ListView和ListActivity-android学习之旅(二十四)
ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...
- Spring学习之旅(十二)--持久化框架
对于本职工作来说 JDBC 就可以很好的完成,但是当我们对持久化的需求变得更复杂时,如: 延迟加载 预先抓取 级联 JDBC 就不能满足了,我们需要使用 ORM框架 来实现这些需求. Spring 对 ...
- android 四大组件Broadcast Receiver
本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...
- android广播接收器BroadcastReceiver
首先看一下什么是 BroadcastReceiver BroadcastReceiver:直译是"广播接收者",所以它的作用是用来接收发送过来的广播的. 那我们有必要知道:什么是广 ...
- Android广播接收器和Activity间传递数据
Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...
- android广播接收器
Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...
- 我的MYSQL学习心得(十二) 触发器
我的MYSQL学习心得(十二) 触发器 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数 ...
- VSTO 学习笔记(十二)自定义公式与Ribbon
原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...
- 我的MYSQL学习心得(十二)
原文:我的MYSQL学习心得(十二) 我的MYSQL学习心得(十二) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYS ...
随机推荐
- bzoj 1085: [SCOI2005]骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...
- 习题10-1 UVA 11040(无聊水一水)
题意: 给你一个残缺的塔,每个数字由他下面左右两个数相加得.给你其中一部分,要求输出全部的数字. #include <iostream> #include <cstdio> # ...
- [BZOJ]2594 水管局长数据加强版(Wc2006)
失踪人口回归. LCT一直是小C的弱项,特别是这种维护链的信息的,写挂了就会调代码调到心态爆炸. 不过还好这一次的模板练习没有出现太多的意外. Description SC省MY市有着庞大的地下水管网 ...
- bzoj2811[Apio2012]Guard 贪心
2811: [Apio2012]Guard Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 905 Solved: 387[Submit][Statu ...
- 【NOIP2013货车运输】
描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多 ...
- 在移动端画出真正的1px边框
一.问题 写H5的样式时候,设置元素的边框为1px,不幸的事情在IOS设备上发生了,设计师会说,咦,边框怎么那么大,这是2px了吧?改成1px.我明明设置成1px了啊. 二.为什么边框变粗了? ...
- 2-学习GPRS_Air202(Air202开发板介绍和下载第一个程序)
http://www.cnblogs.com/yangfengwu/p/8887933.html 资料链接 链接:https://pan.baidu.com/s/1968t2QITuxoyXlE_Nz ...
- 剑指架构师系列-MySQL调优
介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...
- POJ 放苹果问题(递归)
首先我们想象有一个函数count f(m,n)可以把m个苹果放到n个盘子中. 根据 n 和 m 的关系可以进一步分析: 特殊的m <=1|| n <= 1时只有一种方法: 当 m < ...
- Swift基础之CoreData的使用
以前使用过OC版本的CoreData应该很好理解Swift方式,所以这里简单的展示一下,增删改查的方法使用,同时给大家说一下创建步骤,方便大家的使用,转载请注明出处,谢谢~ 步骤一:创建一个Swift ...