Android完全退出应用

可以实现无论在那个Activity中都可以完全退出当前应用程序。

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);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }
}

在每个Activity的onCreate()方法里添加SysApplication.getInstance().addActivity(this);
调用SysApplication.getInstance().exit();实现完全退出当前应用程序。

posted @ 2013-09-13 19:16  BetterMan-  阅读(207)  评论(0编辑  收藏  举报