package com.hamon.testandroid;
import android.app.Activity;
import android.widget.Toast;
public class BackPressCloseHandler {
private long backKeyPressedTime = 0;
private Toast toast;
private Activity activity;
public BackPressCloseHandler(Activity context) {
this.activity = context;
}
public void onBackPressed() {
if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
backKeyPressedTime = System.currentTimeMillis();
showGuide();
return;
}
if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
activity.finish();
toast.cancel();
}
}
private void showGuide() {
toast = Toast.makeText(activity, "뒤로 버튼을 한번 더 누르시면 종료됩니다.",
Toast.LENGTH_SHORT);
toast.show();
}
}
public class HomeActivity extends Activity ... {
private BackPressCloseHandler backPressCloseHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
...
backPressCloseHandler = new BackPressCloseHandler(this);
}
@Override
public void onBackPressed() {
backPressCloseHandler.onBackPressed();
}
}
< 출처 :
http://blog.naver.com/cestlavie_01/40189080974 [cobus님 블로그]>
댓글 없음:
댓글 쓰기