package com.cqupt.util;import java.util.LinkedList;import java.util.List;import android.app.Activity;import android.app.Application;public class ExitApplication extends Application { private ListactivityList = new LinkedList(); private static ExitApplication instance; private ExitApplication() { } //单例模式中获取唯一的ExitApplication实例 public static ExitApplication getInstance() { if(null == instance) { instance = new ExitApplication(); } return instance; } //添加Activity到容器中 public void addActivity(Activity activity) { activityList.add(activity); } //遍历所有Activity并finish public void exit() { for(Activity activity : activityList) { activity.finish(); } System.exit(0); } }
在每个Activity的OnCreate方法里面写上
ExitApplication.getInstance().addActivity(this);在要结束程序的地方:
if (!isExit) { Toast.makeText(MoreActivity.this, "再点一次退出程序", Toast.LENGTH_SHORT).show(); isExit = true; }else{ ExitApplication.getInstance().exit(); }转载自: