Android 完全退出

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;


/**
 * 应用配置类
 * @author leehongee
 *
 */
public class AppManager extends Application{
	/**打开的activity**/
	private List<Activity> activities = new ArrayList<Activity>();
	/**应用实例**/
	private static AppManager instance;
	/**
	 * 	获得实例
	 * @return
	 */
	public static AppManager getInstance(){
		return instance;
	}
	/**
	 * 新建了一个activity
	 * @param activity
	 */
	public void addActivity(Activity activity){
		activities.add(activity);
	}	
	/**
	 * 	结束指定的Activity
	 * @param activity
	 */
	public void finishActivity(Activity activity){
		if (activity!=null) {
			this.activities.remove(activity);
			activity.finish();
			activity = null;
		}
	}	
	/**
	 * 应用退出,结束所有的activity
	 */
	public void exit(){
		for (Activity activity : activities) {
			if (activity!=null) {
				activity.finish();
			}
		}
		System.exit(0);
	}		
	
	@Override
	public void onCreate() {
            instance = this;           
	}		
}

 然后我写了一个BaseActivity,以后的Activity 都继承这个Activity,如下

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;

/**
 * 基本Activity
 * @author leehongee
 * @date 2013-1-26 下午6:24:47
 * @version
 */
public class BaseActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//添加到Activity集合
		AppManager.getInstance().addActivity(this);
		//手机端SDK>10,即 android 2.3.x之后在主线程中访问网络需要加入如下代码
		if (AppConfig.getSDKVersionNumber()>10) {
	        // 详见StrictMode文档
	        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	                .detectDiskReads()
	                .detectDiskWrites()
	                .detectNetwork()   // or .detectAll() for all detectable problems
	                .penaltyLog()
	                .build());
	        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	                .detectLeakedSqlLiteObjects()
	                .detectLeakedClosableObjects()
	                .penaltyLog()
	                .penaltyDeath()
	                .build());
		}
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		onDestroy();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//结束Activity&从集合中移除
		AppManager.getInstance().finishActivity(this);
	}

}

 

你可能感兴趣的:(android)