Android 退出整个程序代码
今天在网上看见个博客关于退出整个程序的,感觉不错,拿来分享学习
SysApplication这个类复制到工程里面,然后在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this); 添加当前Acitivity到ancivitylist里面去,*后在想退出的时候调用SysApplication.getInstance().exit();可直接关闭所有的Acitivity并退出应用程序。
-
import java.util.LinkedList;
-
import java.util.List;
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.app.Application;
-
import android.content.DialogInterface;
-
import android.content.Intent;
-
-
public class SysApplication extends Application {
-
private List mList = new LinkedList();
-
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();
-
}
-
}
在应用程序里面 的activity的oncreate里面添加SysApplication.getInstance().addActivity(this)
如:
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
SysApplication.getInstance().addActivity(this);
-
}