自定义广播的步骤有:注册,重写onReceiver(),发送广播。
自定义广播示例如下,此示例发送了无序,有序,本地广播以及有序广播resultReceiver的用法。
(1)注册广播 AndroidManifest.xml
<receiver android:name=".MyReceiver01">
<intent-filter android:priority="1000">
<action android:name="com.nxyuntui.testproject.CUSTOM"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver02">
<intent-filter android:priority="500">
<action android:name="com.nxyuntui.testproject.CUSTOM"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver03">
<intent-filter android:priority="0">
<action android:name="com.nxyuntui.testproject.CUSTOM"/>
</intent-filter>
</receiver>
(2)布局xml中只有三个button
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送无序广播"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="click1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送有序广播"
android:onClick="click2"/>\
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/localButton"
android:text="发送本地广播"
app:layout_constraintTop_toBottomOf="@+id/button2"
/>
</android.support.constraint.ConstraintLayout>
(3)MainActivity.java文件中定义了如何发送有序/无序/本地广播
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取本地广播管理者实例
final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
Button button = (Button) findViewById(R.id.localButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.nxyuntui.testproject.CUSTOM");
localBroadcastManager.sendBroadcast(intent); //发送本地广播
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.nxyuntui.testproject.CUSTOM");
LocalReceiver localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter); //注册本地广播接收器
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void click1(View view){
Log.i("MainActivity","无序广播");
Intent intent = new Intent();
intent.setAction("com.nxyuntui.testproject.CUSTOM");
sendBroadcast(intent); //发送无序广播
}
public void click2(View view){
Log.i("MainActivity","有序广播");
Intent intent = new Intent();
intent.setAction("com.nxyuntui.testproject.CUSTOM");
sendOrderedBroadcast(inten,null); //发送有序广播
sendOrderedBroadcast(intent,null,new FinalResultReceiver(),null,0,null,null); //resultReceiver重载方法
}
class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"收到了本地广播",Toast.LENGTH_SHORT).show();
}
}
}
(4)MyReceiver01.java ,另外MyReceiver02和MyReceiver03一样继承BroadcastReceiver ,不过onReceive() 方法为空。
public class MyReceiver01 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver01","MyReceiver01");
abortBroadcast();
}
}
(5)FinalResultReceiver.java 有序广播的另一个重载方法,指定结果的广播接收者,该广播必须是当前程序内部,不论前面有多少个广播接收者,或者广播中断,resultReceiver都能接收到广播,而且不需要注册。
public class FinalResultReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("FinalResultReceiver","最终广播");
}
}
(6)本地广播
此类型的广播只能够在应用程序的内部进行传递,并且广播接收器只能接收来自本应用程序发出的广播,避免某些安全隐患。本地广播无法静态注册,因为发送本地广播的时候,程序肯定已经启动了,而静态注册主要是让程序在未启动的情况下也能收到广播。
此示例见上述例子。