e10be9e4b9c4a1a1ebcd24253755b06954a3f681.svn-base 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.example.myapplication.utils;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.text.TextUtils;
  7. import androidx.appcompat.app.AlertDialog;
  8. import com.example.myapplication.service.UserService;
  9. import com.example.myapplication.ui.MainActivity;
  10. import com.orhanobut.logger.Logger;
  11. import java.lang.ref.WeakReference;
  12. public class DialogUtils {
  13. private static AlertDialog sDialog;
  14. private static AlertDialog.Builder sDialogBuilder;
  15. private static Activity mContext;
  16. public static void showTokenOutConfirmDialog(Context context, String message) {
  17. synchronized (DialogUtils.class) {
  18. Logger.e("--------------dialog " + sDialog);
  19. if (sDialog == null) {
  20. showTokenOutDialog(context, message);
  21. } else {
  22. if (mContext != null && mContext == context) {
  23. if (!sDialog.isShowing()) {
  24. showTokenOutDialog(context, message);
  25. }
  26. return;
  27. }
  28. sDialog.dismiss();
  29. sDialog = null;
  30. showTokenOutDialog(context, message);
  31. }
  32. }
  33. }
  34. private static void showTokenOutDialog(Context context, String message) {
  35. mContext = (Activity) new WeakReference<>(context).get();
  36. if (mContext == null || mContext.isFinishing()) {
  37. return;
  38. }
  39. sDialogBuilder = new AlertDialog.Builder(mContext);
  40. sDialogBuilder.setMessage(TextUtils.isEmpty(message) ? "登录时间过期,需要重新登录~" : message + ",需要重新登录");
  41. sDialogBuilder.setCancelable(false);
  42. // sDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  43. // @Override
  44. // public void onClick(DialogInterface dialog, int which) {
  45. // mContext = null;
  46. // }
  47. // });
  48. sDialogBuilder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
  49. @Override
  50. public void onClick(DialogInterface dialog, int which) {
  51. sDialog.dismiss();
  52. clickRetryLogin();
  53. mContext = null;
  54. }
  55. });
  56. // sDialog.setCancleListener(new View.OnClickListener() {
  57. // @Override
  58. // public void onClick(View v) {
  59. // sDialog.dimiss();
  60. // mContext = null;
  61. // }
  62. // }).setComfirmListener(new View.OnClickListener() {
  63. // @Override
  64. // public void onClick(View v) {
  65. // sDialog.dimiss();
  66. //// SPUtils.put(mContext, Constants.SP_FILE_NAME_GESTURE_PATTERN, Constants.SP_KEY_GESTURE_PATTERN_PASSWORD, "");
  67. // // 退出登录
  68. // UserService.getInstance(mContext).logout();
  69. // if (!(mContext instanceof HomeActivity)) {
  70. // mContext.startActivity(new Intent(mContext, HomeActivity.class));
  71. // }
  72. // ReceiverManager.getIntance().sendBroadCastReceiver(mContext, ReceiverManager.ACTION_LOGIN_OUT);
  73. // }
  74. // }
  75. //
  76. // );
  77. sDialog = sDialogBuilder.create();
  78. sDialog.show();
  79. }
  80. /**
  81. * 重新登录
  82. */
  83. private static void clickRetryLogin() {
  84. UserService.getInstance(mContext).logout();
  85. if (mContext != null && !(mContext instanceof MainActivity)) {
  86. mContext.startActivity(new Intent(mContext, MainActivity.class));
  87. }
  88. ReceiverManager.getIntance().sendBroadCastReceiver(mContext, ReceiverManager.ACTION_LOGIN_OUT);
  89. }
  90. }