退出整个应用:
//关闭当前activity方法一
finish();
//关闭当前界面方法二
android.os.Process.killProcess(android.os.Process.myPid());
//关闭当前界面方法三
System.exit(0);
//关闭当前界面方法四
this.onDestroy();
1.制造抛异常导致整个程序退出
2.将所有的activity放入到一个list中,然后在需要退出的时候,将所有的activity,finish掉
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
/**
* 一个类 用来结束所有后台activity
* @author Administrator
*
*/
public class SysApplication extends Application {
//运用list来保存们每一个activity是关键
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);
}
//关闭每一个list内的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();
}
}
3.通过广播来完成退出功能,具体实现过程是这样的:在每个Activity创建时(onCreate时)给Activity注册一个广播接收器,当退出时发送该广播即可。大概的代码如下:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter.addAction("finish"); registerReceiver(Receiver, filter); } private BroadcastReceiver Receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if("finish".equals(intent.getAction())) { Log.e("#########", "I am " + getLocalClassName() + ",now finishing myself..."); finish(); } } };