双击返回键退出应用——自定义Activity基类

注意事项:
application没有onkeydown方法,可以自己定义一个Activity的基类,在里面做统一的处理;

public class BaseActivity extends Activity {

public static String TAG = "BaseMapActivity------基础服务类";

public static List<Activity> activityList = new ArrayList<Activity>();

public static boolean isLog = false;

/**
    * 应用上下文
    */
    public static Context mContext;
    /**
    * 应用的资源
    */
    public static Resources res;

    /**
    * 获取屏幕分辨率(宽度,高度)
    */
    public static int scW, scH;
    public static int reX, reY;

    public static DisplayMetrics dm;

    public static String hwID;

    public static int operate;

    public static int callBack = 1;

    /**
    * 返回键
    */
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        activityList.add(this);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        // WindowManager.LayoutParams.FLAG_FULLSCREEN);// 强制横屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 设置屏幕常亮
        mContext = this;
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(mContext);

        res = getResources();
        // 获取分辨率宽度,高度
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        scW = dm.widthPixels;
        scH = dm.heightPixels;
        reX = dm.widthPixels / 8;
        reY = dm.heightPixels / 4;
        UIHelper.Log("", "", "设备分辨率 : " + scW + "X" + scH);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                UIHelper.showMsg(getApplicationContext(), "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                for (int i = 0; i < activityList.size(); i++) {
                    if (null != activityList.get(i)) {
                    activityList.get(i).finish();
                }
            }
                System.exit(0);
            }
                return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME) {
            UIHelper.showMsg(getApplicationContext(), "请双击返回键退出");
            return true;
        }
            return super.onKeyDown(keyCode, event);
    }

    public static String getStr(int key) {
        return res.getString(key);
    }

    public static String[] getStrs(int key) {
        return res.getStringArray(key);
    }

    public static Integer getInt(int key) {
        return res.getInteger(key);
    }

    public static Drawable getDrawable(int key) {
        return res.getDrawable(key);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    /**
    * @return 到得设备号
    */
    public static String getPadID() {
        TelephonyManager tm = (TelephonyManager) mContext
        .getSystemService(Context.TELEPHONY_SERVICE);
        String pid = tm.getDeviceId();
        if (pid.equals("")) {
        pid = "";
        }
        return pid;
    }
}

你可能感兴趣的:(双击返回键退出应用——自定义Activity基类)