第一种:
public void clearUsedActivity() {
for (Activity ac : usedList) {
if (ac != null && !ac.isFinishing()) {
ac.finish();
}
}
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() != KeyEvent.ACTION_UP) {
Intent intent = new Intent(PlanDisplayActivity.IS_CURRENT_ACTION);
intent.putExtra("isCurrent", true);
sendBroadcast(intent);
ConfirmDialog dialog = ConfirmDialog.createDialog(this);
dialog.setDesc(getString(R.string.exitapp));
dialog.setCancelText(getString(R.string.alertDialog_cancle));
dialog.setPostiveText(getString(R.string.alertDialog_ensure));
dialog.setPostiveLisener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppUntil.insertComplete(MainActivity.this, false);
// UserController.getInstance().insertAppAction(AppAction.Exit);
UserManager.getInstance().insertAppAction(AppAction.Exit);
clearUsedActivity();
System.exit(0);
finish();
}
});
dialog.show();
return true;
}
return super.dispatchKeyEvent(event);
}
第二种
@Override
public void onBackPressed() {
if (!mBackKeyPressed) {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {//延时两秒,如果超出则擦除第一次按键记录
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
} else {//退出程序
this.finish();
System.exit(0);
}
}
public void onBackPressed() {
if (!mBackKeyPressed) {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {//延时两秒,如果超出则擦除第一次按键记录
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
} else {//退出程序
this.finish();
System.exit(0);
}
}
第三种