欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现双击退出的功能

程序员文章站 2024-03-05 09:25:48
实现android双击后退键退出当前app功能 实现该功能基本思路是, 1, 监听后退键 , 比较两次后退间隔 , 低于两秒则出发退出 2, 退出当前app 我选择...

实现android双击后退键退出当前app功能

实现该功能基本思路是,

1, 监听后退键 , 比较两次后退间隔 , 低于两秒则出发退出

2, 退出当前app

我选择在基类中baseactivity 中设置监听,代码如下:

 public void onbackpressed() {

    //preferences 中获取是否双击退出
    boolean isdoubleclick = true;
    //baseapplication.get("ifdoubleclickedback", true);

    if (isdoubleclick) {
      long curtime = systemclock.uptimemillis();
      if ((curtime - mbackpressedtime) < (2 * 1000)) {
        finish();
        //activity栈管理
        appmanager.getappmanager().appexit();
      } else {
        mbackpressedtime = curtime;
        toast.maketext(this, "再次点击退出", toast.length_short).show();
      }
    } else {
      finish();
    }

  }

在退出当前app中,参考开源中国,封装了activity栈

实例代码:

public class appmanager {
  private static stack<activity> activitystack;
  private static appmanager instance;

  private appmanager() {
  }

  /**
   * 单一实例
   */
  public static appmanager getappmanager() {
    if (instance == null) {
      instance = new appmanager();
    }

    if (activitystack == null) {
      activitystack = new stack<activity>();
    }

    return instance;
  }

  /**
   * 获取指定的activity
   *
   * @author kymjs
   */
  public static activity getactivity(class<?> cls) {
    if (activitystack != null)
      for (activity activity : activitystack) {
        if (activity.getclass().equals(cls)) {
          return activity;
        }
      }
    return null;
  }

  /**
   * 添加activity到堆栈
   */
  public void addactivity(activity activity) {
    activitystack.add(activity);
  }

  /**
   * 获取当前activity(堆栈中最后一个压入的)
   */
  public activity currentactivity() {
    activity activity = activitystack.lastelement();
    return activity;
  }

  /**
   * 结束当前activity(堆栈中最后一个压入的)
   */
  public void finishactivity() {
    activity activity = activitystack.lastelement();
    finishactivity(activity);
  }

  /**
   * 结束指定的activity
   */
  public void finishactivity(activity activity) {
    if (activity != null && activitystack.contains(activity)) {
      activitystack.remove(activity);
      activity.finish();
    }
  }

  /**
   * 结束指定的activity
   */
  public void removeactivity(activity activity) {
    if (activity != null && activitystack.contains(activity)) {
      activitystack.remove(activity);
    }
  }

  /**
   * 结束指定类名的activity
   */
  public void finishactivity(class<?> cls) {
    for (activity activity : activitystack) {
      if (activity.getclass().equals(cls)) {
        finishactivity(activity);
        break;
      }
    }
  }

  /**
   * 结束所有activity
   */
  public void finishallactivity() {
    for (int i = 0, size = activitystack.size(); i < size; i++) {
      if (null != activitystack.get(i)) {
        finishactivity(activitystack.get(i));
      }
    }
    activitystack.clear();
  }

  /**
   * 退出应用程序
   */
  public void appexit() {
    try {
      finishallactivity();
      // system.exit(0);
    } catch (exception e) {
    }
  }
}

思路就是在每次activity 压入activity栈中管理

在退出时遍历栈,挨个finish

希望本文所述对大家的android程序设计有所帮助。