app从主界面后退到桌面的几种处理方法

1,重写disatchKeyEvent(KeyEvent event)方法,使用路由跳转到桌面

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent();
intent.setAction(“android.intent.action.MAIN”);
intent.addCategory(“android.intent.category.HOME”);
startActivity(intent);
}
return false;
}

2,多次点击退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() – mExitTime) > 2000) {
Object mHelperUtils;
show(“再按一次退出程序”);
mExitTime = System.currentTimeMillis();

} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}

3,多次点击最小化
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() – mExitTime) > 2000) {
Object mHelperUtils;
show(“再按一次退出程序”);
mExitTime = System.currentTimeMillis();

} else {
//                finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
return true;
}
return super.onKeyDown(keyCode, event);
}

 

未经允许不得转载:A66网 » app从主界面后退到桌面的几种处理方法

赞 (0)
分享到:更多 ()

评论 0