Android 四大组件之BroadcastReceiver
程序员文章站
2022-05-09 20:53:20
...
一、什么是广播
在Android中,Broadcast是一种广泛应用在应用程序之间传输信息的机制,通过Intent携带我们要传送的数据。
二、广播应用场景
1.同一个app中,多进程间不同组件的消息传递
2.不同app之间的组件之间通信
三、广播分类
1.普通广播(Normal Broadcast)
2.系统广播(System Broadcast)
3.有序广播(Ordered Broadcast)
4.App应用内广播(Local Broadcast)
四、实现BroadcastReceiver
1.静态注册:常驻广播
①创建一个类继承BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.d("aaa", "网络状态已经改变");
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
String name = info.getTypeName();
Log.d("aaa", "当前网络名称:" + name);
} else {
Log.d("aaa", "没有可用网络");
}
}
}
}
②在清单文件中注册(记得加上权限)
<receiver android:name=".MyReceiver"
>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
③断开网络,然后连接网络结果如图
2.动态注册:跟随Activity生命周期
①创建广播类HomePressReceiver(监听home的点击)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class HomePressReceiver extends BroadcastReceiver {
private String TAG="HomePressReceiver";
private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
// private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(null != action && !"".equals(action) && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if(null != reason && !"".equals(reason)) {
if(reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {// �̰�home��
Log.e(TAG,"点击HOME");
}
}
}
}
}
② 在Activity中注册广播,并在activity销毁是反注册
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private HomePressReceiver homePressReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homePressReceiver = new HomePressReceiver();
//注册动态广播
registerReceiver(homePressReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
@Override
protected void onDestroy() {
unregisterHomeKeyReceiver();
super.onDestroy();
}
//反注册
private void unregisterHomeKeyReceiver() {
if (null != homePressReceiver) {
unregisterReceiver(homePressReceiver);
}
}
}
③运行程序,点击home键效果如图
发送普通广播
①为了方便,在注册HomePressReceiver时多加一个自定义的action,则HomePressReceiver可以同时监听多个action:
②在另一个activity中发送消息到HomePressReceiver
③HomePressReceiver中接收方式如下:
④运行结果如图:
github下载地址:https://github.com/crazysnail123/BroadcastReceiverDemo.git
csdn下载地址:https://download.csdn.net/download/u010350891/11143966