广播接收者BroadcastReceiver
IncomingSMSRecevier receiver = new IncomingSMSRecevier();
registerReceiver(receiver, filter);
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/send"
android:text="点击发送广播"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BroadCastMain extends Activity {
private Button sendBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendBtn = (Button) findViewById(R.id.send);
sendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//设置intent的Action属性
intent.setAction("com.lp.action.WELCOME_BROADCAST");
intent.putExtra("msg", "消息");
//发送广播,使用intent发送一条广播
sendBroadcast(intent);
}
});
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/** 当符合该MyReceiver的广播出现时,onReceive方法会被触发,从而显示广播所携带的消息 */
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "接受到的Intent的Action是:"
+intent.getAction()
+"\n消息的内容为:"+intent.getStringExtra("msg")
, Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".MyReceiver">
<intent-filter >
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击发送有序广播"
/>
</LinearLayout>
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BroadcastSorted extends Activity {
private Button sendbtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendbtn = (Button) findViewById(R.id.send);
sendbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.lp.action.WELCOME_BROADCAST");
intent.putExtra("msg", "消息");
//发送有序广播
sendOrderedBroadcast(intent, null);
}
});
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "接收到的intent的action为:"+intent.getAction()
+"\nintent的消息的内容为:"+intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();
//创建一个Bundle对象
Bundle mBundle = new Bundle();
mBundle.putString("first", "第一个BroadcastReceiver存入的消息");
//将Bundle放入结果中
setResultExtras(mBundle);
//取消broadcast的继续传播
//abortBroadcast();
}
}
step4第二个BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReciever2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
//解析前一个BroadcastReceiver所存入的key为first的消息
String first = bundle.getString("first");
Toast.makeText(context, "第一个Broadcast存入的消息为:"+first,500000).show();
}
}
step5:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lp.ecjtu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BroadcastSorted"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver" >
<intent-filter android:priority="20">
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReciever2" >
<intent-filter android:priority="0">
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
广播接收者BroadcastReceiver的更多相关文章
- 广播接收者 BroadcastReceiver 示例-1
广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者.广播作为Android组件间的通信方式,可以使用的场景如下: 1 ...
- Android系统编程入门系列之广播接收者BroadcastReceiver实现进程间通信
在前边几篇关于Android系统两个重要组件的介绍中,界面Activity负责应用程序与用户的交互,服务Service负责应用程序内部线程间的交互或两个应用程序进程之间的数据交互.看上去这两大组件就能 ...
- Android - 广播接收者 - BroadcastReceiver
BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...
- 广播接收者 BroadcastReceiver 示例-2
BaseActivity /**所有Activity的基类*/ public class BaseActivity extends Activity { @Override prote ...
- Android中广播接收者BroadcastReceiver详解
1. 接收系统的广播步骤 (1) 新建一个类继承BroadcastReceiver 以监听sd卡状态的广播接收者为例 public class SdCardBroadcastReceiver ext ...
- Android学习笔记_19_广播接收者 BroadcastReceiver及其应用_窃听短信_拦截外拨电话
一.广播接收者类型: 广播被分为两种不同的类型:“普通广播(Normal broadcasts)”和“有序广播(Ordered broadcasts)”. 普通广播是完全异步的,可以在同一时刻(逻辑上 ...
- 广播接收者 BroadcastReceiver
1. 分为动态注册和静态注册, 静态注册在清单文件里配置即可.动态创建为代码手动添加. 在锁屏广播中, 使用静态创建消息接受不成功, 原因未知. 动态即可. 代码如下: 2. 创建类, 继承与Broa ...
- 广播发送者&广播接收者介绍
1.广播接收者 广播接收者简单地说就是接收广播意图的Java类,此Java类继承BroadcastReceiver类,重写: public void onReceive(Context context ...
- Android(java)学习笔记175:BroadcastReceiver之 外拨电话的广播接收者
首先我们示例工程一览表如下: 1.首先我们还是买一个收音机,定义一个OutCallReceiver继承自BroadcastReceiver,onReceive()方法中定义了监听到广播,要执行的操作: ...
随机推荐
- StyleCop学习笔记——默认的规则
在StyleCop中有一些官方自己写好的检测规则下面就是英文的解释 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialEl ...
- ios中三种多线程的技术对比
1.NSThread 使用较少 在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露. 使用流程:创建线程-->启动线程 2.NSOpera ...
- iOS 初级数据持久化
数据持久化 什么是数据持久化? 数据的永久存储 为什么要做数据持久化::存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的 数据持久化的本质:数据保存成文件,存储到程序的沙盒中 一.沙 ...
- [转]论window和Linux之长短
论window和Linux之长短 王垠 http://www.kerneltravel.net/jiqiao/whyLinux.htm — 摈弃 Windows 低效率的工作方式,发掘 Linux 身 ...
- Win10无法上网提示缺少一个或者多个网络协议的处理方法
netsh winsock reset Win+x 再按A 输入 netsh winsock reset
- 【每日scrum】5.3
团队的Backlog 初期总目标:完成需求分析,做好软件前期的一切准备. 任务 认领人 估算完成时间 查找铁大离线电子地图 验证地图的准确性 万彤 司新红 3天 接口需求 (用户界面和软件接口等) 曹 ...
- “我爱淘”冲刺阶段Scrum站立会议9
完成任务: 完成了webservice的配置与测试. 计划任务: 可以通过webservice将数据库中的内容解析出来,通过查询可以得到想要的内容. 遇到问题: 服务器已经配好,,也已经测试成功,不过 ...
- JBOSS和WebLogic区别
JBoss: 1. JBoss开放源代码Java EE实现,成本低,好控制. 2. JBoss需要的内存和硬盘空间比较小,但是只适合做小项目. 3. 安装非常简单.先解压缩JBoss打包文件再配 ...
- 集成Spring后HibernateTemplate实现分页
spring 整合 hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate ...
- 21、HierarchyViewer使用记录
1.是啥 HierachyViewer是一种能够方便开发人员了解activity中的布局信息的工具. 2.异常 HierachyViewer在未root过的设备或者低版本的设备是无法使用的 3.怎么办 ...