Broadcast
静态注册
| |
| <receiver android:name=".CustomReceiver"> |
| |
| <intent-filter android:priority="100"> |
| <action android:name="com.example.receiver_flag" /> |
| </intent-filter> |
| </receiver> |
| |
| public void sendAction1(View view) { |
| Intent intent = new Intent(); |
| intent.setAction(ActionUtils.ACTION_FLAG); |
| |
| intent.setPackage("com.example.myactivity1"); |
| sendBroadcast(intent); |
| |
| |
| } |
| package com.example.myactivity1.receiver; |
| |
| import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.util.Log; |
| import android.widget.Toast; |
| |
| public class CustomReceiver extends BroadcastReceiver { |
| |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| Toast.makeText(context, "静态接收者", Toast.LENGTH_SHORT).show(); |
| |
| } |
| } |
动态注册
| |
| CustomReceiver2 customReceiver2 = new CustomReceiver2(); |
| IntentFilter intentFilter = new IntentFilter(); |
| intentFilter.addAction(ActionUtils.ACTION_FLAG2); |
| registerReceiver(customReceiver2, intentFilter); |
| |
| |
| |
| intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); |
| registerReceiver(networkChangeReceiver, intentFilter); |
| |
| public void sendAction2(View view) { |
| Intent intent = new Intent(); |
| intent.setAction(ActionUtils.ACTION_FLAG2); |
| sendBroadcast(intent); |
| } |
| package com.example.myactivity1.receiver; |
| |
| import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.util.Log; |
| import android.widget.Toast; |
| |
| public class CustomReceiver2 extends BroadcastReceiver { |
| |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| Toast.makeText(context, "动态接收者", Toast.LENGTH_SHORT).show(); |
| } |
| } |
- NetworkChangeReceiver.java
| package com.example.myactivity1.receiver; |
| |
| import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.net.ConnectivityManager; |
| import android.net.NetworkInfo; |
| import android.widget.Toast; |
| |
| 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.isAvailable()) { |
| Toast.makeText(context, "network is working", Toast.LENGTH_SHORT).show(); |
| } else { |
| Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show(); |
| } |
| } |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15248576.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步