BroadcastReceiver 类所有广播接收器的抽象基类,其实现类用来发送出来的广播进行筛选并作出响应,接收器调用onReceive()方法
静态广播
AndroidManifest.xml
<!--静态注册广播广播-->
<receiver android:name=".ImoocBroadcastReceiver" android:exported="true">
<!--接收哪些广播-->
<intent-filter>
<!--开机广播-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<!--电量低广播-->
<action android:name="android.intent.action.BATTERY_LOW"/>
<!--应用被安装广播-->
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<!--应用被卸载广播-->
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<!--接收的数据类型-->
<data android:scheme="package"/>
</intent-filter>
</receiver>
package com.example.testapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
// 系统广播
public class ImoocBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "ImoocBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// 接收广播
if(intent != null){
// 接收到是什么广播
String action = intent.getAction();
// ImoocBroadcastReceiver: onReceive:android.intent.action.PACKAGE_REMOVED
Log.d(TAG,"onReceive:" + action);
// 用来监听设备网络状态
if(action.equals(ConnectivityManager.EXTRA_NO_CONNECTIVITY)){}
}
}
}
动态注册系统广播
package com.example.testapplication;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class BroadcastReceiverActivity extends AppCompatActivity {
private ImoocBroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcastreceiver);
// 新建一个广播接收器
broadcastReceiver = new ImoocBroadcastReceiver();
// 要接收哪些广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
// 注册广播接收器
registerReceiver(broadcastReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 取消注册广播接收器
if(broadcastReceiver != null){
unregisterReceiver(broadcastReceiver);
}
}
}
动态注册⼴播是通过IntentFilter类的addAction()⽅法添加的。静态注册才是通过Android
Manifest.xml⽂件。
在 BroadcastReceiver⾥,不能做⼀些⽐较耗时的操作,否则会弹出ANR(Application N
o Response)的对话框。建议交给Activity 或者 Service 去处理,由它们来开启线程。注
意:不建议在BroadcastReceiver中开启线程处理耗时操作,因为BroadCastReceiver的
⽣命周期很短,可能出现⼦线程还没有结束,BroadCastReceiver就已经退出了。
自定义广播
![](https://i-blog.csdnimg.cn/blog_migrate/ed61b144cc6f21cac76080273eb9b0ca.png)
package com.example.testapplication;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class BroadcastReceiverActivity extends AppCompatActivity {
private ImoocBroadcastReceiver broadcastReceiver;
private EditText inputEditText;
private Button sendBroadcastButton;
private TextView resultTextView;
public static final String MY_ACTION = "com.imooc.demo.afdsabfdaslj";
public static final String BROADCAST_CONTENT = "broadcast_content";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcastreceiver);
inputEditText = findViewById(R.id.inputEditText);
sendBroadcastButton = findViewById(R.id.sendBroadcastButton);
resultTextView = findViewById(R.id.resultTextView);
// 新建一个广播接收器
broadcastReceiver = new ImoocBroadcastReceiver(resultTextView);
// 注册广播接收器
// 要接收哪些广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(MY_ACTION);
// 注册广播接收器
registerReceiver(broadcastReceiver,intentFilter);
sendBroadcastButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// 新建广播
Intent intent = new Intent(MY_ACTION);
// 放入广播要携带的数据
intent.putExtra(BROADCAST_CONTENT, inputEditText.getText().toString());
sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 取消注册广播接收器,不然会导致内存泄露
if(broadcastReceiver != null){
unregisterReceiver(broadcastReceiver);
}
}
}
package com.example.testapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
// 系统广播
public class ImoocBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "ImoocBroadcastReceiver";
TextView mTextView;
public ImoocBroadcastReceiver() {
}
public ImoocBroadcastReceiver(TextView textView) {
mTextView = textView;
}
@Override
public void onReceive(Context context, Intent intent) {
// 接收广播
if(intent != null){
// 接收到是什么广播
String action = intent.getAction();
// ImoocBroadcastReceiver: onReceive:android.intent.action.PACKAGE_REMOVED
Log.d(TAG,"onReceive:" + action);
// 用来监听设备网络状态
if(action.equals(ConnectivityManager.EXTRA_NO_CONNECTIVITY)){}
// 判断是什么广播
if(TextUtils.equals(action,BroadcastReceiverActivity.MY_ACTION)){
// 获取广播携带的内容,可自定义的数据
String content = intent.getStringExtra(BroadcastReceiverActivity.BROADCAST_CONTENT);
if(mTextView != null){
mTextView.setText("接收到的action是:" + action + "\n接收的内容是:\n" + content);
}
}
}
}
}