package com.cheng.utils; import android.app.Activity; import android.app.ActivityManager; import android.content.ClipData; import android.content.ClipboardManager; import android.content.ComponentName; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.view.View; import android.view.inputmethod.InputMethodManager; import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * Activity管理类:用于管理Activity和退出程序 */ public class AppManager { // Activity栈 private static Stack<Activity> sActivityStack; // 单例模式 private static AppManager sInstance; private AppManager() { Logger.TAG = "AppManager"; } /** * 单一实例 */ public static AppManager getAppManager() { if (sInstance == null) { sInstance = new AppManager(); } return sInstance; } /** * 添加Activity到堆栈 */ public void addActivity(Activity activity) { if (sActivityStack == null) { sActivityStack = new Stack<Activity>(); } sActivityStack.add(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) */ public Activity currentActivity() { Activity activity = sActivityStack.lastElement(); return activity; } /** * 结束当前Activity(堆栈中最后一个压入的) */ public void removeActivity() { Activity activity = sActivityStack.lastElement(); removeActivity(activity); } /** * 结束指定的Activity */ public void removeActivity(Activity activity) { if (activity != null) { sActivityStack.remove(activity); activity.finish(); activity = null; } } /** * 结束指定类名的Activity */ public void removeActivity(Class<?> cls) { for (Activity activity : sActivityStack) { if (activity.getClass().equals(cls)) { removeActivity(activity); } } } /** * 结束所有Activity */ public void removeAllActivity() { for (int i = 0; i < sActivityStack.size(); i++) { if (null != sActivityStack.get(i)) { sActivityStack.get(i).finish(); } } sActivityStack.clear(); } /** * 退出应用程序 */ public void AppExit(Context context) { try { removeAllActivity(); ActivityManager activityMgr = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); activityMgr.killBackgroundProcesses(context.getPackageName()); System.exit(0); } catch (Exception e) { Logger.e(e); } } /** * 清理后台进程与服务 * @param context 应用上下文对象context * @return 被清理的数量 */ public static int gc(Context context) { long i = getDeviceUsableMemory(context); int count = 0; // 清理掉的进程数 ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); // 获取正在运行的service列表 List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(100); if (serviceList != null) for (ActivityManager.RunningServiceInfo service : serviceList) { if (service.pid == android.os.Process.myPid()) continue; try { android.os.Process.killProcess(service.pid); count++; } catch (Exception e) { e.getStackTrace(); } } // 获取正在运行的进程列表 List<ActivityManager.RunningAppProcessInfo> processList = am.getRunningAppProcesses(); if (processList != null) for (ActivityManager.RunningAppProcessInfo process : processList) { // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着 if (process.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) { // pkgList 得到该进程下运行的包名 String[] pkgList = process.pkgList; for (String pkgName : pkgList) { Logger.d("======正在杀死包名:" + pkgName); try { am.killBackgroundProcesses(pkgName); count++; } catch (Exception e) { // 防止意外发生 Logger.e(e); } } } } Logger.d("清理了" + (getDeviceUsableMemory(context) - i) + "M内存"); return count; } /** * 获取设备的可用内存大小 * * @param context 应用上下文对象context * @return 当前内存大小 */ public static int getDeviceUsableMemory(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); // 返回当前系统的可用内存 return (int) (mi.availMem / (1024 * 1024)); } /** * 获取系统中所有的应用 * * @param context 上下文 * @return 应用信息List */ public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); List<PackageInfo> paklist = pManager.getInstalledPackages(0); for (int i = 0; i < paklist.size(); i++) { PackageInfo pak = paklist.get(i); if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { // customs applications apps.add(pak); } } return apps; } /** * 说明 * 创建一个Activity的基类,并在onCreate和onDestory方法里面调用对应的方法, * 这样就不需要单独在某个Activity里面添加重复逻辑,精简代码 * onCreate()方法中 * // 添加Activity到堆栈 * AppManager.getAppManager().addActivity(this); * onDestroy()方法中 * // 结束Activity&从堆栈中移除 * AppManager.getAppManager().removeActivity(this); */ /** * 判断应用是否处于后台状态 * @return */ public static boolean isBackground(Context context) { ActivityManager am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getApplicationContext().getPackageName())) { return true; } } return false; } /** * 复制文本到剪贴板 * @param text */ public static void copyToClipboard(Context context, String text){ ClipboardManager cbm = (ClipboardManager) context.getApplicationContext().getSystemService(Activity.CLIPBOARD_SERVICE); cbm.setPrimaryClip(ClipData.newPlainText(context.getApplicationContext().getPackageName(), text)); } /** * 关闭输入法 * @param act */ public static void closeInputMethod(Activity act){ View view = act.getCurrentFocus(); if(view!=null){ ((InputMethodManager)act.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE)). hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }