广播接收者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()方法中定义了监听到广播,要执行的操作: ...
随机推荐
- 正益工作是何许APP?凭什么作为第一届大会的“闪亮”点
参加过很多发布会,看过很多宣传稿,渐渐的你也读懂了“大会亮点”,这是技术人.市场人绞尽脑汁.加班加点的成果,更渗透着企业未来的战略思路.在2016AppCan移动开发者大会的官方新闻发布后,很多人留言 ...
- poj 1459 Power Network
题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...
- go语言值得学习的开源项目推荐
谷歌官方维护了一个基于go语言的开源项目列表: https://github.com/golang/go/wiki/Projects 其中有非常多的优秀项目值得学习,有几百行代码适合新手阅读的项目,也 ...
- [转]理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]
[转]doc,ppt,xls文件格式转PDF格式 http://blog.csdn.net/lee353086/article/details/7920355 确实好用. 需要注意的是#import文 ...
- 从烙铁手到IT男
时间:2015年8月27日 21:37:44 作者:luomg 摘要: 简要记录一写自己干的这个行当,多少是个回忆,不然某一天呜呼哀哉了啥也没有记录,会随着时间更新(大学时熬夜绘制了很多altium ...
- Android facebook-android-sdk-3.5.2 FQL
android平台下使用Facebook 3.5.2的SDK实践 步骤总结: 1.引用SDK 在Facebook官网下载最新版的SDK,解压. 打开Eclipse, 'File' -> 'Imp ...
- 设置xx-net,访问youtube等国外网站
配合使用chrome+xx-net,就可以免费访问youtube等外网了.步骤如下: 1. 按照https://github.com/XX-net/XX-Net/wiki/%E4%BD%BF%E7%9 ...
- 使用C语言在Win控制台中输出带颜色的文字
学了这么久的C语言,一直停留在编写“控制台”程序的水平.黑色窗口,白色的文字,看多了着实让人感到枯燥无味.但是作为业余爱好者,我既没有那么多时间和精力去学习如何编写窗口程序,也没有那个必要一定用C去调 ...
- Ionic入门一:Hello Ionic
1.在终端里面进入准备存放App的目录:  2.Ionic官网提供了三个项目模板blank.tabs和sideMenu ,用“ionic start myApp tabs”创建ionic项目:  ...