123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package com.example.myapplication.utils;
- import android.app.Activity;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.text.TextUtils;
- import androidx.appcompat.app.AlertDialog;
- import com.example.myapplication.service.UserService;
- import com.example.myapplication.ui.MainActivity;
- import com.orhanobut.logger.Logger;
- import java.lang.ref.WeakReference;
- public class DialogUtils {
- private static AlertDialog sDialog;
- private static AlertDialog.Builder sDialogBuilder;
- private static Activity mContext;
- public static void showTokenOutConfirmDialog(Context context, String message) {
- synchronized (DialogUtils.class) {
- Logger.e("--------------dialog " + sDialog);
- if (sDialog == null) {
- showTokenOutDialog(context, message);
- } else {
- if (mContext != null && mContext == context) {
- if (!sDialog.isShowing()) {
- showTokenOutDialog(context, message);
- }
- return;
- }
- sDialog.dismiss();
- sDialog = null;
- showTokenOutDialog(context, message);
- }
- }
- }
- private static void showTokenOutDialog(Context context, String message) {
- mContext = (Activity) new WeakReference<>(context).get();
- if (mContext == null || mContext.isFinishing()) {
- return;
- }
- sDialogBuilder = new AlertDialog.Builder(mContext);
- sDialogBuilder.setMessage(TextUtils.isEmpty(message) ? "登录时间过期,需要重新登录~" : message + ",需要重新登录");
- sDialogBuilder.setCancelable(false);
- // sDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- // @Override
- // public void onClick(DialogInterface dialog, int which) {
- // mContext = null;
- // }
- // });
- sDialogBuilder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- sDialog.dismiss();
- clickRetryLogin();
- mContext = null;
- }
- });
- // sDialog.setCancleListener(new View.OnClickListener() {
- // @Override
- // public void onClick(View v) {
- // sDialog.dimiss();
- // mContext = null;
- // }
- // }).setComfirmListener(new View.OnClickListener() {
- // @Override
- // public void onClick(View v) {
- // sDialog.dimiss();
- //// SPUtils.put(mContext, Constants.SP_FILE_NAME_GESTURE_PATTERN, Constants.SP_KEY_GESTURE_PATTERN_PASSWORD, "");
- // // 退出登录
- // UserService.getInstance(mContext).logout();
- // if (!(mContext instanceof HomeActivity)) {
- // mContext.startActivity(new Intent(mContext, HomeActivity.class));
- // }
- // ReceiverManager.getIntance().sendBroadCastReceiver(mContext, ReceiverManager.ACTION_LOGIN_OUT);
- // }
- // }
- //
- // );
- sDialog = sDialogBuilder.create();
- sDialog.show();
- }
- /**
- * 重新登录
- */
- private static void clickRetryLogin() {
- UserService.getInstance(mContext).logout();
- if (mContext != null && !(mContext instanceof MainActivity)) {
- mContext.startActivity(new Intent(mContext, MainActivity.class));
- }
- ReceiverManager.getIntance().sendBroadCastReceiver(mContext, ReceiverManager.ACTION_LOGIN_OUT);
- }
- }
|