在实际的Android开发过程中常常有退出整个Android程序的需求,下面给大家介绍一下如何编写一个退出整个Android程序的工具类
package com.superGao.exitApp;import java.util.LinkedList;import java.util.List;import android.app.Activity;import android.app.Application;public class ExitApplication extends Application { private static List<Activity> activityList = new LinkedList<Activity>(); 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 static void exit() { for (Activity activity :activityList) { activity.finish(); } System.exit(0); }}在所有Activity中将Activity添加到工具类中 // 强制退出整个应用相关代码 ExitApplication.getInstance().addActivity(this);最后在退出按钮的点击事件中调用工具类的exit()即可ExitApplication.exit();