[其他]代码库
package com.example.myactivitytest;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
public class BaseActivity extends Activity {
Timer timer;
boolean bFlag = false;// bFlag = false 没有按下back 键
static ArrayList<BaseActivity> mActivities = new ArrayList<BaseActivity>();
public BaseActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
timer = new Timer();
mActivities.add(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (bFlag == true) { // 第二按下back键
for (int i = 0; i < mActivities.size(); i++) {
BaseActivity baseActivity = mActivities.get(i);
baseActivity.finish();
}
mActivities.clear();
return true;
}
bFlag = true; // bFlag = false 没有按下back 键
timer.schedule(new TimerTask() {
@Override
public void run() {
bFlag = false;
}
}, 2000);
Toast.makeText(BaseActivity.this, "再按下一次Back键退出应用程序",
Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
[源代码打包下载]
by: 发表于:2017-09-07 15:47:52 顶(0) | 踩(0) 回复
??
回复评论