Android中实现再按一次返回键退出应用程序

编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/64234676

思路

  在BaseActivity中重写onKeyDown方法,监听每个页面的返回键点击操作,如果有两次返回键的点击时间间距小于两秒就退出程序

Demo图片展示

Android中实现再按一次返回键退出应用程序_第1张图片

实现

  • 创建一个ExitApplication类 对所有Activity的退出进行管理
import android.app.Activity;
import android.app.Application;
import java.util.ArrayList;
import java.util.List;

public class ExitApplication extends Application{
    private List activityList = new ArrayList<>();
    private static ExitApplication instance;

    public ExitApplication(){}
    public static ExitApplication getInstance(){
        if(null == instance){
            instance = new ExitApplication();
        }
        return instance;
    }

    //添加Activity到容器中
    public void addActivity(Activity activity){
        //Log.i("activity", "size:" + activityList.size());
        //Log.i("activity", "name:" + activity.getIntent());
        activityList.add(activity);
    }

    //遍历所有Activity并finish
    public void exit(){
        for(Activity activity : activityList){
            //依次关闭
            activity.finish();
            //Log.i("activity", "del_size:" + activityList.size());
            //Log.i("activity", "del_name:" + activity.getIntent());
        }
        //强制退出
        System.exit(0);
    }
}
  • 在BaseActivity中将各Activity添加进集合,并重写onKeyDown方法,监听每个页面的返回键点击操作
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;

public class BaseActivity extends AppCompatActivity {

    private long exitTime = 0;

    public void exitActivity(Activity activity) {
        ExitApplication.getInstance().addActivity(activity);
    }

    /**
     * 重写onKeyDown方法
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                exitApp();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出程序
     */
    private void exitApp() {
        // 判断2次点击事件时间
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            // 退出程序关键代码
            ExitApplication.getInstance().exit();
        }
    }
}
  • 打酱油的MainActivity,SecondActivity
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        exitActivity();
    }
    private void initView() {
        //此处均为findViewById操作,跟主题无关,代码省略
    }

    private void initData() {
        //跟主题无关,代码省略
    }
}

Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9893154


———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-

你可能感兴趣的:(Android中实现再按一次返回键退出应用程序)