回答

2

通過覆蓋主要活動的onBackPressed()方法可以輕鬆實現此功能。在這個例子中,當用戶按下返回按鈕時,應用程序將顯示一個4秒鐘的敬酒,在這段時間內,新的背部按壓立即終止應用程序。

ref

你可以把它在擴展活動像這樣的BaseActivity:

public class BaseActivity extends Activity{ 

    private Toast toast; 
    private long lastBackPressTime = 0; 
    . . . 

    /** 
    * Prevent accidental app exit by requiring users to press back twice when 
    * the app is exiting w/in 4sec 
    */ 
    @Override 
    public void onBackPressed() { 
     if (this.lastBackPressTime < System.currentTimeMillis() - 4000) { 
     toast = Toast.makeText(this, "Press back again to close this app", 4000); 
     toast.show(); 
     this.lastBackPressTime = System.currentTimeMillis(); 
     } else { 
     if (toast != null) { 
     toast.cancel(); 
     } 
     super.onBackPressed(); 
    } 
    } 
    . . . 
} 

編輯:添加片段堆棧中的兼容性

對於使用來檢測的最後一個片段bacstack whos應用程序完全使用片段,我強烈建議將您的dispatchKeyEvents放入BaseActivity類並實現上述方法,如下所示:

public class BaseActivity extends Activity { 

    public boolean dispatchKeyEvent(KeyEvent event) { 
     int backCount = getFragmentManager().getBackStackEntryCount(); 
     int action = event.getAction(); 
     int keyCode = event.getKeyCode(); 

     FragmentManager fm = getFragmentManager(); 

    . . . 

     case KeyEvent.KEYCODE_BACK : 
       if (action == KeyEvent.ACTION_DOWN && backCount == 0) { 
        onexitNotify(); 
       }else { 
        fm.popBackStack(); 
       } 
       return true; 

      default : 
       return super.dispatchKeyEvent(event); 
     } 
    } 

/** 
* Prevent accidental app exit by requiring users to press back twice when 
* the app is exiting w/in 8sec 
*/ 
    private Toast toast; 
    private long lastBackPressTime = 0; 

    public void onexitNotify() { 
     if (this.lastBackPressTime < System.currentTimeMillis() - 8000) { 
      toast = Toast.makeText(this, "Press back again to close this app", 8000); 
      toast.show(); 
      this.lastBackPressTime = System.currentTimeMillis(); 
     } else { 
      if (toast != null) { 
       toast.cancel(); 
      } 
      super.onBackPressed(); 
     } 
    } 
} 

*如果您使用的是2.0+,onBackPressed()簡化了所需的代碼量,所以不需要onKeyDown()。

每androd模式recomendation:

有些應用程序提示用戶,當它即將退出。這似乎是 在遊戲應用中特別常見。這種做法不是在正常應用中推薦的 。確認提示 會中斷用戶的正常工作流程。即使在使用此選項的遊戲中,也應該仔細考慮 。

+0

'onBackpressed'在API級別5中引入,而keydown則在API級別1後引入。另外API級別5是Android 2.0。 – Orlymee

0

您可以檢查在應用程序的主要活動中何時按下後退鍵。然後,您可以向用戶顯示alertdialog以確認退出。

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    //Handle the back button 
    if(keyCode == KeyEvent.KEYCODE_BACK) {   
     checkExit();    
     return true; 
    }  
    else { 
     return super.onKeyDown(keyCode, event); 
    } 

} 

private void checkExit() 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this);   
    builder.setMessage("Are you sure you want to exit?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        //take actions here accordingly as the user has pressed yes 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      });  
    AlertDialog alert = builder.create(); 
    alert.show();  
} 
+0

代碼太多。 onBackPressed()簡化了它。 – CelticParser

+0

這是相同數量的代碼,而不是使用壓縮我使用keydown。其餘部分用於創建alertdialog。根據你的目標受衆可能會錯過敬酒。 Alertdialog在屏幕上等待進一步的行動。每個android模式推薦的 – Orlymee

+0

:某些應用程序在即將退出時提示用戶。這似乎是特別常見的遊戲應用程序。在常規應用中不推薦這種做法。確認提示會破壞用戶的正常工作流程。即使在使用這個選項的遊戲中也應該仔細考慮。 – CelticParser