android之用于退出本应用
1.SysApplication 类
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
/**
* 用于退出本应用
* @author Administrator
*
*/
public class SysApplication extends Application {
private List<Activity> mList = new LinkedList<Activity>();
private static SysApplication instance;
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
2.在每个Activity加上这行代码
// 添加当前Activity到activity list里面去
SysApplication.getInstance().addActivity(this);
3.//退出应用程序
SysApplication.getInstance().exit();