package com.githang.androidkit.utils.app; import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.os.Build; import com.githang.androidkit.utils.Log4AK; import java.util.Stack; /** * 应用程序Activity管理类:用于Activity管理和应用程序退出 * * @author liux (http://my.oschina.net/liux) * @version 1.0 * @created 2012-3-21 */ public class AppManager { private static final Log4AK log = Log4AK.getLog(AppManager.class); private static Stack<Activity> activityStack = new Stack<Activity>(); /** * 添加Activity到堆栈 */ public static void addActivity(Activity activity) { activityStack.push(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) */ public static Activity currentActivity() { return activityStack.lastElement(); } /** * 结束当前Activity(堆栈中最后一个压入的) */ public static void finishCurrentActivity() { Activity activity = activityStack.pop(); activity.finish(); } /** * 结束指定的Activity */ public static void finishActivity(Activity activity) { if (activity != null) { activityStack.remove(activity); activity.finish(); } } /** * 结束指定类名的Activity */ public static void finishActivity(Class<?> cls) { for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * 结束所有Activity */ public static void finishAllActivity() { for (Activity activity : activityStack) { if (activity != null) { activity.finish(); } } activityStack.clear(); } /** * 退出应用程序 */ @TargetApi(Build.VERSION_CODES.FROYO) public static void AppExit(Context context) { try { finishAllActivity(); ActivityManager manager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); manager.killBackgroundProcesses(context.getPackageName()); System.exit(0); } catch (Exception e) { log.w(e); } } }
AppManager Activity管理类
最新推荐文章于 2022-03-29 14:13:38 发布