广播接收者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()方法中定义了监听到广播,要执行的操作: ...
随机推荐
- .NET开源工作流RoadFlow-流程设计-流程步骤设置-策略设置
策略设置包括当前步骤的流转方式,处理人员,退回策略等设置. 流转类型:当前步骤后面有多个步骤时,此类型选择可以决定后续步骤的发送方式. 1.系统控制:由系统根据您在线上设置的流转条件来判断该发送到哪一 ...
- kettle报错 ../deploy does not exist, please create it.
具体错误如下: Xlib: extension "RANDR" missing on display "localhost:10.0". ::, INFO [K ...
- 【转载】alter table move 和 alter table shrink space的区别
move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法: alter table TABLE_NAME shrink space [com ...
- Android中修改状态栏的颜色和我们App的风格一致
其实也没什么东西,就是一个主题,下面看代码: <resources> <!-- Base application theme. --> <style name=" ...
- Python-memcached的基本使用
想学Python,又想研究下memcached的客户端,于是拿Python-memcached研究研究~~~ 1.memcached的安装 请参考本博另一文章<Linux下安装memcached ...
- ExtJS 提示
要使ExtJS支持提示,需要在onReady的function中添加如下语句: Ext.QuickTips.init();//支持tips提示 Ext.form.Field.prototype.msg ...
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...
- [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取
[转]ubuntu(12.04)下, 命令 ,内核 源代码的获取 http://blog.chinaunix.net/uid-18905703-id-3446099.html 1.命令:例如:要查看l ...
- 5个SQL核心
5个核心的SQL语句 1.SELECT -查询语句的逻辑处理顺序 5 SELECT <columnlist> 1 FROM <source objectlist> ...
- 修改ip脚本
1.打开运行 2.输入CMD 3.在命令提示符下输入: netsh -c interface ip dump > C:\我的网络配置.txt 4.打开您在C:\ 下的"我的网络配置 . ...