码迷,mamicode.com
首页 > 移动开发 > 详细

android学习——退出整个应用程序

时间:2015-03-18 13:44:14      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

将下面SysApplication这个类复制到工程里面,然后在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this); 添加当前Acitivity到ancivitylist里面去,最后在想退出的时候调用SysApplication.getInstance().exit();可直接关闭所有的Acitivity并退出应用程序。

package com.example.Emotional.util;
 
import java.util.LinkedList; 
import java.util.List; 
import android.app.Activity; 
import android.app.Application; 
  
/**
 * 
* 类名称:SysApplication   
* 类描述:   退出整个应用程序的类
* 创建时间:2014-4-25 下午1:58:00   
* 修改备注:   
* @version    
*
 */
 
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(); 
    }  
}

在硬退出按钮点击时

//点击硬退出按钮
    public void clickQuitDialog() { 
         
            AlertDialog.Builder builder = new Builder(FirstActivity.this); 
            builder.setMessage("确定要退出吗?");
             
//          builder.setTitle("提示"); 
            builder.setPositiveButton("确认",new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
//                        FirstActivity.this.finish(); 
//                        System.exit(0);
                        SysApplication.getInstance().exit();
                    } 
                }); 
            builder.setNegativeButton("取消",new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
            builder.create().show(); 
        } 
     
        @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
             
            //event.getRepeatCount() == 0 防止重复点击
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
                clickQuitDialog(); 
                return false; 
            } 
            return false; 
        }
 

 

android学习——退出整个应用程序

标签:

原文地址:http://www.cnblogs.com/guichun/p/4346829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!