一.简介
BrocastReceiver(广播)是Android四大组件之一,运行在UI线程。和activity一样需要注册 。.有生命周期即onReceive方法执行完成后广播结束。不可做耗时操作(10s ANR)。
二.Demo
BrocastReceiver(广播)分为 静态广播和动态广播。
1.动态广播
广播类
package com.example.test.component;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import static com.example.test.component.BrocastReceiverActivity.DynamicAction;
public class DynamicBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
Log.i("DynamicBroadcast","动态广播接收!!!action----:"+action);
if(DynamicAction.equals(action)){//动态广播
String staticString1=intent.getStringExtra("staticString1");
String staticString2=intent.getStringExtra("staticString2");
Log.i("DynamicBroadcast","动态广播接收!!!staticString1----:"+staticString1);
Log.i("DynamicBroadcast","动态广播接收!!!staticString2----:"+staticString2);
}
}
}
注册
<receiver
android:name=".component.DynamicBroadcast"
android:enabled="true"
android:exported="true">
</receiver>
Activity
package com.example.test.component;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.test.R;
public class BrocastReceiverActivity extends AppCompatActivity {
private DynamicBroadcast dynamicBroadcast = null;
public static final String DynamicAction = "com.example.test.component.dynamicBroadcast";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brocastreceiver);
registDynamicBroadcast();
TextView textView = findViewById(R.id.activity_brocastreceiver_textview);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendDynamicBroadcast();
}
});
}
/**
* 注册动态广播
*/
private void registDynamicBroadcast() {
dynamicBroadcast = new DynamicBroadcast();
IntentFilter filter = new IntentFilter();
filter.addAction(DynamicAction);//只有持有相同的action的接受者才能接收此广播
registerReceiver(dynamicBroadcast, filter);
}
/**
* 发送动态广播
*/
public void sendDynamicBroadcast() {
Intent intent = new Intent();
intent.setAction(DynamicAction);//设置你这个广播的action,只有和这个action一样的接受者才能接受者才能接收广播
intent.putExtra("staticString1", "张三");
intent.putExtra("staticString2", "李四");
sendBroadcast(intent);//发送广播
}
/**
* onDestroy方法 注销动态广播
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (null != dynamicBroadcast) {
unregisterReceiver(dynamicBroadcast);
}
}
}
结果
I/DynamicBroadcast: 动态广播接收!!!action----:com.example.test.component.dynamicBroadcast
I/DynamicBroadcast: 动态广播接收!!!staticString1----:张三
I/DynamicBroadcast: 动态广播接收!!!staticString2----:李四
2.静态广播
广播类
package com.example.test.component;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import static com.example.test.component.BrocastReceiverActivity.StaticAction;
public class StaticBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
Log.i("StaticBroadcast","静态广播接收!!!action----:"+action);
if(StaticAction.equals(action)){//静态广播
String staticString1=intent.getStringExtra("staticString1");
String staticString2=intent.getStringExtra("staticString2");
Log.i("StaticBroadcast","静态广播接收!!!staticString1----:"+staticString1);
Log.i("StaticBroadcast","静态广播接收!!!staticString2----:"+staticString2);
}
}
}
注册
<receiver
android:name=".component.StaticBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.test.component.staticBroadcast" />
</intent-filter>
</receiver>
Activity
package com.example.test.component;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.test.R;
public class BrocastReceiverActivity extends AppCompatActivity {
public static final String StaticAction = "com.example.test.component.staticBroadcast";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brocastreceiver);
TextView textView = findViewById(R.id.activity_brocastreceiver_textview);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendStaticBroadcast();
Log.i("StaticBroadcast","静态广播点击");
}
});
}
/**
* 发送静态广播
*/
public void sendStaticBroadcast() {
Intent intent = new Intent();
intent.setAction(StaticAction);//设置你这个广播的action,只有和这个action一样的接受者才能接受者才能接收广播
intent.putExtra("staticString1", "张三");
intent.putExtra("staticString2", "李四");
sendBroadcast(intent);//发送广播
}
}
结果
I/StaticBroadcast: 静态广播点击
说明
由上述可知,静态广播没有收到。测试机Android系统8.1.0。
原因
即Android8.0对静态广播做了限制,收不到静态广播。
3.总结
动态注册广播不是常驻型广播,也就是说广播跟随activity的生命周期。注意: 在activity结束前,移除广播接收器。
静态注册是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。
当广播为有序广播时:
1.优先级高的先接收。
2.同优先级的广播接收器,动态优先于静态。
3.同优先级的同类广播接收器,静态:先扫描的优先于后扫描的,动态:先注册的优先于后注册的。
当广播为普通广播时:
1.无视优先级,动态广播接收器优先于静态广播接收器。
2.同优先级的同类广播接收器,静态:先扫描的优先于后扫描的,动态:先注册的优先于后注册的。
3.静态广播使用示例:这里以开机启动为例(电池电量过低,设备内存不足...)
系统广播(以开机启动app为例)
清单中注册广播
<receiver android:name=".AutoStartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
权限设置
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
onReceive方法中
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);