赞
踩
系统广播是 Android 系统在特定事件发生时发出的广播,比如设备启动完成、网络状态变化等。
在 AndroidManifest.xml
中声明接收器:
<receiver android:name=".NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
接收器实现:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
Toast.makeText(context, "Network Connected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Network Disconnected", Toast.LENGTH_SHORT).show();
}
}
}
应用内自定义广播,用于应用内部组件之间的通信。
发送自定义广播:
Intent intent = new Intent("com.example.CUSTOM_ACTION");
intent.putExtra("data", "Broadcast Data");
sendBroadcast(intent);
在 AndroidManifest.xml
中声明接收器:
<receiver android:name=".CustomReceiver">
<intent-filter>
<action android:name="com.example.CUSTOM_ACTION"/>
</intent-filter>
</receiver>
接收器实现:
public class CustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("data");
Toast.makeText(context, "Received: " + data, Toast.LENGTH_SHORT).show();
}
}
有序广播允许多个接收器按优先级顺序接收,并且可以中止广播的传播。
发送有序广播:
Intent intent = new Intent("com.example.ORDERED_ACTION");
sendOrderedBroadcast(intent, null);
在 AndroidManifest.xml
中声明接收器,并设置优先级:
<receiver android:name=".FirstReceiver" android:priority="100">
<intent-filter>
<action android:name="com.example.ORDERED_ACTION"/>
</intent-filter>
</receiver>
<receiver android:name=".SecondReceiver" android:priority="50">
<intent-filter>
<action android:name="com.example.ORDERED_ACTION"/>
</intent-filter>
</receiver>
接收器实现:
public class FirstReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "First Receiver", Toast.LENGTH_SHORT).show(); // 继续传播广播 } } public class SecondReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Second Receiver", Toast.LENGTH_SHORT).show(); // 可以中止广播传播 abortBroadcast(); } }
本地广播用于应用内部组件通信,避免跨进程通信带来的安全和性能问题。
发送本地广播:
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
Intent localIntent = new Intent("com.example.LOCAL_ACTION");
localIntent.putExtra("data", "Local Broadcast Data");
localBroadcastManager.sendBroadcast(localIntent);
动态注册本地广播接收器:
@Override protected void onStart() { super.onStart(); IntentFilter filter = new IntentFilter("com.example.LOCAL_ACTION"); LocalBroadcastManager.getInstance(this).registerReceiver(localReceiver, filter); } @Override protected void onStop() { super.onStop(); LocalBroadcastManager.getInstance(this).unregisterReceiver(localReceiver); } private BroadcastReceiver localReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String data = intent.getStringExtra("data"); Toast.makeText(context, "Received: " + data, Toast.LENGTH_SHORT).show(); } };
onReceive
中避免执行耗时操作,使用 IntentService
或者其他异步机制。LocalBroadcastManager
进行应用内广播,避免不必要的跨进程通信。BroadcastReceiver
是 Android 应用程序中用于异步接收广播消息的重要组件。通过合理地使用系统广播、自定义广播、有序广播和本地广播,开发者可以实现松耦合的组件通信。与此同时,优化广播的处理流程和生命周期管理,能有效提升应用的性能和稳定性。了解和掌握 BroadcastReceiver
的高级使用和优化策略,是开发高效 Android 应用的重要技能。
欢迎点赞|关注|收藏|评论,您的肯定是我创作的动力 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。