I would just save the time of the backpress and then compare the time of the latest press to the new press.
long lastPress;
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if(currentTime - lastPress > 5000){
Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG).show();
lastPress = currentTime;
}else{
super.onBackPressed();
}
}
You can also dismiss the toast when the app the back press is confirmed (cred @ToolmakerSteve):
long lastPress;
Toast backpressToast;
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if(currentTime - lastPress > 5000){
backpressToast = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
backpressToast.show();
lastPress = currentTime;
} else {
if (backpressToast != null) backpressToast.cancel();
super.onBackPressed();
}
}