1、广播5种分类
- 普通广播(Normal Broadcast)是开发者自己自定义的广播,是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎同时接收到这条广播消息,它们之间没有任何项目顺序可言。这种广播的效率比较高,但也意味着它是无法被截断的。在广播发送过程中是无视优先级。
- 系统广播(System Broadcast)是手机系统的广播,例如蓝牙、网络变化、电量低系统都发起广播 ,每个广播都有特定的Intent - Filter
- 有序广播(Ordered Broadcast)发送出去的广播被广播接收者按照先后顺序接收。此时的广播接收器是有先后顺序,优先级高的广播接收器就可以先收到广播消息,并且前面的广播接收器还可以截断正在传递的广播,这样后面的广播接收器就无法收到广播消息了。当广播为有序广播时:优先级高的先接收(不分静态和动态)。同优先级的广播接收器,动态优先于静态。同优先级的同类广播接收器,静态:先扫描的优先于后扫描的,动态:先注册的优先于后注册的。
- 粘性广播(Sticky Broadcast) 由于在Android5.0 & API 21中已经失效,不建议用
- App应用内广播(Local Broadcast)Android中的广播可以跨App直接通信,App应用内广播可理解为一种局部广播,广播的发送者和接收者都同属于一个App
2、广播注册方式
静态注册和动态注册两种注册方式,他们的区别分别是
动态注册广播不是常驻型广播,也就是说广播跟随Activity的生命周期。注意在Activity结束前,移除广播接收器。
静态注册是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。
静态注册方式,注册广播直接写到AndroidManifest文件中,
代码如下
自定义一个广播,静态注册广播
package com.nyw.mybroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
//添加自定义广播类型,包名.intent.action.广播类名
public static final String ACTION="com.nyw.mybroadcast.intent.action.MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
try {
String data = intent.getStringExtra("data");
Log.i("log_info", data);
}catch (Exception e){
Log.e("log_info", "数据异常,没有数据");
}
//中断广播,activity里发送广播 消息的时候,需要使用发送有序广播才生效sendOrderedBroadcast(intent,null);
// abortBroadcast();
}
}
java代码如下
package com.nyw.mybroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button tv_send_receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_send_receiver=findViewById(R.id.tv_send_receiver);
tv_send_receiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送静态广播,要在AndroidManifest里配制广播注册
Intent intent=new Intent();
//静态注册广播可以使用
intent.setClass(MainActivity.this,MyReceiver.class);
intent.putExtra("data","发送静态注册的广播数据");
sendBroadcast(intent);
//发送有序广播,第二个参数是权限,这里写null就行了
// sendOrderedBroadcast(intent,null);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nyw.mybroadcast">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyBroadcast">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="10">
<action android:name="com.nyw.mybroadcast.intent.action.MyReceiver"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<intent-filter android:priority="10"> <action android:name="com.nyw.mybroadcast.intent.action.MyReceiver"/> </intent-filter>
如上面配制中,有多个广播的时候,设置android:priority优先级,可以让程序优先接收到消息,android:priority设置的值越高,优先级越高。
第二种方式注册广播 动态注册广播
package com.nyw.mybroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
//添加自定义广播类型,包名.intent.action.广播类名
public static final String ACTION="com.nyw.mybroadcast.intent.action.MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
try {
String data = intent.getStringExtra("data");
Log.i("log_info", data);
}catch (Exception e){
Log.e("log_info", "数据异常,没有数据");
}
}
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_send_receiver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送静态广播" />
<Button
android:id="@+id/btn_send_receiver_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送动态广播" />
<Button
android:id="@+id/btn_register_receiver_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册广播" />
<Button
android:id="@+id/btn_cancel_receiver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消注册广播" />
</LinearLayout>
package com.nyw.mybroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn_send_receiver,btn_send_receiver_two,btn_cancel_receiver,btn_register_receiver_two;
//动态注册广播
private MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver=new MyReceiver();
btn_send_receiver=findViewById(R.id.btn_send_receiver);
btn_send_receiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送静态广播,要在AndroidManifest里配制广播注册
Intent intent=new Intent();
//静态注册广播可以使用
intent.setClass(MainActivity.this,MyReceiver.class);
intent.putExtra("data","发送静态注册的广播数据");
sendBroadcast(intent);
//发送有序广播,第二个参数是权限,这里写null就行了
// sendOrderedBroadcast(intent,null);
}
});
btn_send_receiver_two=findViewById(R.id.btn_send_receiver_two);
btn_send_receiver_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送动态广播消息,不需要在AndroidManifest里配制广播注册
Intent intent=new Intent();
//动态注册广播使用
intent.setAction(MyReceiver.ACTION);
intent.putExtra("data","发送动态注册的广播数据");
sendBroadcast(intent);
//发送有序广播,第二个参数是权限,这里写null就行了
// sendOrderedBroadcast(intent,null);
}
});
btn_register_receiver_two=findViewById(R.id.btn_register_receiver_two);
btn_register_receiver_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//注册广播
//动态注册广播,不需要在AndroidManifest里配制广播注册
IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction(MyReceiver.ACTION);
registerReceiver(myReceiver,intentFilter);
}
});
btn_cancel_receiver=findViewById(R.id.btn_cancel_receiver);
btn_cancel_receiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//取消注册广播
if (myReceiver!=null) {
unregisterReceiver(myReceiver);
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (myReceiver!=null) {
unregisterReceiver(myReceiver);
}
}
}
代码下载地址