(1)定义即将要发送的内容
Intent intent = new Intent();
intent.putExtra("key", "我是一个广播");
(2)创建静态注册的receiver,继承BroadcastReceiver,并重写onReceive(Context context, Intent intent)接收消息
package com.afang.day22_exec01.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收消息
String info = intent.getStringExtra("info");
Log.i("---","onReceive");
// 处理消息
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
(3)AndroidManifest.xml中进行静态注册,并添加意图过滤器的Action标签
静态注册 --清单文件中 <Application>标签下 <receiver>标签
添加意图过滤器 IntentFilter 定义接收者的Action
exported = true 支持接收外部应用发送的广播
<receiver
android:name="com.afang.day22_exec01.receiver.StaticReceiver"
android:exported="true">
<intent-filter >
<action android:name="com.afang.day22_exec01.receiver.StaticReceiver"/>
</intent-filter>
</receiver>
(4)定义广播发送的action
//4、定义广播发送的action
intent.setAction("com.afang.day22_exec01.receiver.StaticReceiver");
(5)发送广播,使用sendBroadcast(Intent intent)方法
sendBroadcast(intent);
完整代码如下
activity_main.xml文件
<RelativeLayout 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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="静态注册"
android:id="@+id/button"
android:onClick="show"/>
</RelativeLayout>
MainActivity.java代码
package com.afang.day22_exec01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View v)
{
//1、定义要发送的内容
Intent intent = new Intent();
intent.putExtra("info","静态注册");
//4、定义广播发送的action
intent.setAction("com.afang.day22_exec01.receiver.StaticReceiver");
//5、发送广播
sendBroadcast(intent);
Log.i("---", "发送完了");
}
}
StaticReceiver.java代码
package com.afang.day22_exec01.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收消息
String info = intent.getStringExtra("info");
Log.i("---","onReceive");
// 处理消息
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml静态注册代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afang.day22_exec01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.afang.day22_exec01.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册广播接收器
exported = true 支持接收外部应用发送的广播
action 定义:尽量使用包名来定义
-->
<!-- 静态注册 -->
<receiver
android:name="com.afang.day22_exec01.receiver.StaticReceiver"
android:exported="true">
<intent-filter >
<action android:name="com.afang.day22_exec01.receiver.StaticReceiver"/>
</intent-filter>
</receiver>
</application>
</manifest>
2、动态注册
(1)定义广播的发送内容(Intent)
Intent intent = new Intent();
intent.putExtra("info","动态注册");
(2)创建动态注册的receiver,继承BroadcastReceiver,并重写onReceive(Context context, Intent intent)接收消息
package com.afang.day22_exec01.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MoveReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收消息
String info = intent.getStringExtra("info");
Log.i("---","onReceive");
// 处理消息
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
(3)创建receiver的对象private MoveReceiver receiver;
(4)创建文件过滤器对象,并添加Action
(5)注册
(6)设置广播发送的action
(7)发送广播
<span style="white-space:pre"> </span>//3、创建receiver的对象
receiver = new MoveReceiver();
//4、创建文件过滤器对象,并添加Action
IntentFilter intentFilter = new IntentFilter();
//不一定是完整的类名,也可以指定其他,但是尽量使用包名
intentFilter.addAction("com.afang.day22_exec01.receiver.MoveReceiver");
//5、注册
registerReceiver(receiver,intentFilter);
//6、设置广播发送的action
intent.setAction("com.afang.day22_exec01.receiver.MoveReceiver");
//7、发送广播
sendBroadcast(intent);
(8)注销动态注册的receiver,通常在onDestroy()方法中
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
完整代码如下:
activity_main.xml
<RelativeLayout 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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态注册"
android:id="@+id/button"
android:onClick="show"/>
</RelativeLayout>
MainActivity.java
<span style="font-size:18px;">package com.afang.day22_exec01;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.afang.day22_exec01.receiver.MoveReceiver;
public class MainActivity extends Activity {
private MoveReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View v)
{
//1、定义要发送的内容
Intent intent = new Intent();
intent.putExtra("info","动态注册");
//3、创建receiver的对象
receiver = new MoveReceiver();
//4、创建文件过滤器对象,并添加Action
IntentFilter intentFilter = new IntentFilter();
//不一定是完整的类名,也可以指定其他,但是尽量使用包名
intentFilter.addAction("com.afang.day22_exec01.receiver.MoveReceiver");
//5、注册
registerReceiver(receiver,intentFilter);
//6、设置广播发送的action
intent.setAction("com.afang.day22_exec01.receiver.MoveReceiver");
//7、发送广播
sendBroadcast(intent);
Log.i("---", "发送完了");
}
//8、注销动态注册的receiver
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}</span>
MoveReceiver.java
<span style="font-size:18px;">package com.afang.day22_exec01.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MoveReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收消息
String info = intent.getStringExtra("info");
Log.i("---","onReceive");
// 处理消息
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}</span>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------广播的生命周期:
广播接收者 重写方法 onReceive
生命周期
当onReceiver方法接收到数据(执行)开始 onReceiver 有10s 的活跃期 10s过后处于失活状态
失活状态的时候 ,当其他的进程有需要内存空间的时候将会将该receiver 销毁
所以 在该方法中不要去执行一些延时操作
需要时: 可以启动衍生线程(子线程)来完成延时操作
------------------------------------------------------------------------------------------------------
有序广播
根据接收者 的优先级来决定是谁先接收到广播
发送方法为: sendOrderedBroadcast(Intent intent, String receiverPermission)
第二个参数是 权限名称 有需要的填写 不需要可以为Null
注册接收者的时候需要添加 优先级
优先级的取值范围是 -1000+1000
数大的为优先级高的 -- 先接收到广播
设置优先级的代码
<intent-filter
<span style="color:#ff0000;">android:priority="-1000"></span>
<action android:name="com.afang.day22_broadcast02.recevier.info" />
</intent-filter>
常用的方法
setResultExtras(Bundle)用于设置要传递的数据
getResultExtras(boolean)返回一个Bundle对象
boolean参数含义为:true代表如果前面的接收器没有存放数据,则自动创建一个空的Bundle对象,false则表示如果前面的接收器如果没有存放任何数据则返回null。
演示代码如下:
创建三个接收器类
MyReceiver1.java
package com.phone.day22_broadcastorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String info = intent.getStringExtra("key");
// Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
Toast.makeText(context, "第一个接到广播 "+info, Toast.LENGTH_SHORT).show();
}
}
MyReceiver2.java
package com.phone.day22_broadcastorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "第二个接到广播 优先级 -200", Toast.LENGTH_SHORT).show();
intent.putExtra("key", "我不饿了");
// setResultData(data);
Bundle bundle = new Bundle();
bundle.putString("info", "我是一个bundle对象");
// 设置值 将该bundler 传递给优先级低的 接收者
setResultExtras(bundle);
}
}
MyReceiver3.java
package com.phone.day22_broadcastorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String info = intent.getStringExtra("key");
// 接收优先级比自身高的广播传递过来的信息
Bundle bundle = getResultExtras(true);
Toast.makeText(context, "第三个接到广播" +info+ bundle.getString("info"),
Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
要想接收到同一个广播的消息,Action必须配置相同
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phone.day22_broadcastorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.phone.day22_broadcastorder.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
定义广播接收者的优先级
intent-filter priority
取值范围为 -1000 到+1000
数大的 先接收到广播 数小的后接收到广播
-->
<receiver
android:name="com.phone.day22_broadcastorder.MyReceiver1"
android:exported="true" >
<intent-filter android:priority="300">
<action android:name="com.phone.day22_Myreceiver" />
</intent-filter>
</receiver>
<receiver
android:name="com.phone.day22_broadcastorder.MyReceiver2"
android:exported="true" >
<intent-filter
android:priority="200" >
<action android:name="com.phone.day22_Myreceiver" />
</intent-filter>
</receiver>
<receiver
android:name="com.phone.day22_broadcastorder.MyReceiver3"
android:exported="true" >
<<intent-filter android:priority="100" >
<action android:name="com.phone.day22_Myreceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
package com.phone.day22_broadcastorder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 发送一个有序广播 这是一个Button的点击事件
public void OnClick(View view) {
Intent intent = new Intent();
intent.putExtra("key", "我饿了————————");
intent.setAction("com.phone.day22_Myreceiver");
sendOrderedBroadcast(intent, null);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
package com.phone.day22_broadcastorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "第二个接到广播 优先级 -200", Toast.LENGTH_SHORT).show();
intent.putExtra("key", "我不饿了");
// setResultData(data);
Bundle bundle = new Bundle();
bundle.putString("info", "我是一个bundle对象");
// 设置值 将该bundler 传递给优先级低的 接收者
setResultExtras(bundle);
abortBroadcast();// 广播拦截方法 执行该方法后 优先级低于当前receiver 的接收者 都将接收不到广播了
}
}
MyReseiver2.java中添加如abortBroadcast(),那么MyReceiver3.java的接受者就不能收到广播了
大 发的萨芬撒的