发送标准广播
1.新建一个广播接收器MyBroadcastReceiver
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Toast.makeText(context,"my broadcast",Toast.LENGTH_LONG).show();
}
}
2.在AndroidMainfest.xml中修改广播接收器
让MyBroadcastReceiver接收一条值为com.example.broadcast.MY_BROADCAST的广播。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcast.MY_BROADCAST"/>
</intent-filter>
</receiver>
3.定义一个按钮,用于点击发送广播
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Broad"
android:textAllCaps="false" />
</LinearLayout>
4.修改MainActivity
构建Intent对象,把要发送的光播的值传入。
调用Context的sendBroadcast()将广播发送出去。
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
});
5.运行程序
跨进程通信
6.新建一个项目BroadcastTest2
7.新建广播接收器AnotherBroadcastReceiver
package com.example.broadcasttest2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AnotherBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Toast.makeText(context,"received in another broadcast",Toast.LENGTH_LONG).show();
}
}
8.在AndroidMainfest.xml中对新广播接收器进行修改
Another同样接收com.example.broadcast.MY_BROADCAST。
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcast.MY_BROADCAST"/>
</intent-filter>
</receiver>
发送有序广播
1.修改sendBroadcast()方法
sendOrderBroadcast()方法接收两个参数,一个参数仍是Intent,一个参数是一个域权限相关的字符串,直接传入null即可。
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
//发送标准广播
//sendBroadcast(intent);
//发送有序广播
sendOrderedBroadcast(intent,null);
}
});
2.设置优先级
android:priority给广播接收器设置优先级,优先级比较高的广播接收器就可以先收到广播。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.broadcast.MY_BROADCAST"/>
</intent-filter>
</receiver>
3.选择是否允许广播继续传递
修改MyBroadcastReceiver中的代码
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Toast.makeText(context,"my broadcast",Toast.LENGTH_LONG).show();
//截断广播消息
abortBroadcast();
}
}
重新运行程序,点击Send Broad按钮,只有MyBroadcastReceiver中的信息都弹出。
本地广播机制
使用该机制发出的广播只能在应用程序的内部进行传递,而且广播接收器也只接收来自本应用程序的广播。
本地广播无法通过静态注册的方式来接收
修改MainActivity中的代码
先是通过LocalBroadcastManager的getInstance()方法得到它的一个实例,注册广播接收器时调用LocalBroadcastManager的registerReceiver()方法;发送广播时调用LocalBroadcastManager的sendBroadcast()方法;取消注册广播接收器调用LocalBroadcastManager的unregisterReceiver()方法。
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;
//private NetworkReceiver networkReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//发送本地广播
localBroadcastManager = LocalBroadcastManager.getInstance(this);//获取实例
Button button = (Button)findViewById(R.id.button);
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);//注册本地广播接收器
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);//发送本地广播
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//unregisterReceiver(networkReceiver);
localBroadcastManager.unregisterReceiver(localReceiver);
}
运行程序