46d817699335c6e2c83bf5f8f1be40594f059c30.svn-base 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.example.myapplication.utils;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.text.TextUtils;
  7. import androidx.localbroadcastmanager.content.LocalBroadcastManager;
  8. /**
  9. * 广播管理类
  10. */
  11. public class ReceiverManager {
  12. //退出登录
  13. public static final String ACTION_LOGIN_OUT = "com.cunduoduo.action.loginout";
  14. //刷新token
  15. public static final String ACTION_TOKEN_UPDATE = "com.cunduoduo.action.token.update";
  16. //头像更改
  17. public static final String ACTION_USER_PHOTO_UPDATE = "com.cunduoduo.user.photo";
  18. public static final String THREAD_TAG = "threadTag";
  19. private LocalBroadcastManager manager;
  20. private static ReceiverManager receiverManager;
  21. /**
  22. * 实例化
  23. */
  24. public synchronized static ReceiverManager getIntance() {
  25. if (receiverManager == null) {
  26. receiverManager = new ReceiverManager();
  27. }
  28. return receiverManager;
  29. }
  30. /**
  31. * 注册广播
  32. */
  33. public void registerReceiver(Context context, BroadcastReceiver receiver,
  34. String... filters) {
  35. if (filters == null || filters.length == 0) {
  36. return;
  37. }
  38. manager = LocalBroadcastManager.getInstance(context);
  39. IntentFilter filter = new IntentFilter();
  40. for (String s : filters) {
  41. filter.addAction(s);
  42. }
  43. manager.registerReceiver(receiver, filter);
  44. }
  45. /**
  46. * 取消注册
  47. */
  48. public void unRegisterReceiver(BroadcastReceiver receiver) {
  49. manager.unregisterReceiver(receiver);
  50. }
  51. /**
  52. * 发送广播
  53. */
  54. public void sendBroadCastReceiver(Context context, String... filter) {
  55. sendBroadCastReceiver(null, context, filter);
  56. }
  57. public void sendBroadCastReceiver(String params, Context context, String... filter) {
  58. if (filter == null || filter.length == 0) {
  59. return;
  60. }
  61. manager = LocalBroadcastManager.getInstance(context);
  62. Intent intent = new Intent();
  63. if (!TextUtils.isEmpty(params)) {
  64. intent.putExtra(THREAD_TAG, params);
  65. }
  66. for (String s : filter) {
  67. manager.sendBroadcast(intent.setAction(s));
  68. }
  69. }
  70. }