Application 判断当前是否还有Activity处于激活状态

package com.pencho.pai.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;

/**
 * manageApplication是用于判断当前是否还有Activity处于激活状态
 *
 * 
 */
public class ManageApplication extends Application {
	private HashMap mapActivity = new HashMap();// 用于存储activity对应的激活状态
	private static ManageApplication application = null;

	@Override
	public void onCreate() {
		if (Constant.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
		}
		super.onCreate();
		application = this;
		initImageLoader(getApplicationContext());
	}

	public void addActivityStatus(Activity activity, boolean isAlive) {
		if (mapActivity.containsKey(activity.getClass().getName())) {
			mapActivity.remove(activity.getClass().getName());
			mapActivity.put(activity.getClass().getName(), isAlive);
		} else {
			mapActivity.put(activity.getClass().getName(), isAlive);
		}

	}

	/**
	 * 配置ImageLoaderConfiguration这个类实现全局ImageLoader的实现情况。
	 * @param context
	 */
	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs()
				.build();
		ImageLoader.getInstance().init(config);
	}
	
	/**
	 * 当前应用是否还有activity处于活动状态
	 * @return
	 */
	public boolean isAllActivityAlive() {
		boolean res = false;
		Iterator iter = mapActivity.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			boolean value = (Boolean) entry.getValue();
			if (value) {
				return true;
			}
		}

		return res;
	}

	public static ManageApplication getInstance() {
		return application;
	}
}


 
 

你可能感兴趣的:(Application 判断当前是否还有Activity处于激活状态)