package com.example;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.widget.Toast;
public class BackPressCloseHandler {
private long backKeyPressedTime = 0;
private Toast toast;
private Activity activity;
private InterstitialAd interstitial;
public BackPressCloseHandler(Activity context)
{
this.activity = context;
Start ();
}
void Start () {
// ads
interstitial = new InterstitialAd(this.activity);
interstitial.setAdUnitId("ca-app-pub-**********************");
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().build();
// Load the Ads with the request.
interstitial.loadAd(request);
}
public void onBackPressed()
{
if(System.currentTimeMillis() > backKeyPressedTime + 2000)
{
backKeyPressedTime = System.currentTimeMillis();
showToast();
return;
}
if(System.currentTimeMillis() <= backKeyPressedTime + 2000)
{
// activity.finish();
Utils.ShutdownApplication();
toast.cancel();
if(interstitial != null)
{
interstitial.show();
}
}
}
private void showToast()
{
toast = Toast.makeText(activity, "\'뒤로\' 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT);
toast.show();
}
}
위의 코드로 Java Class를 만들고 각 화면 Activity에서 해당 Class를 이용하면 된다.
아래 코드는 각 Activity에서 사용하는 부분이다. 핵심적인 부분만 코드로 남겨둔다.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
backPressClosehandler = new BackPressCloseHandler(this);
}
'MOBILE' 카테고리의 다른 글
안드로이드 PHP GET 방식 통신에서 한글 깨짐(?) 해결 (0) | 2016.08.21 |
---|---|
Android SharedPreferences 사용 예제 (0) | 2016.08.21 |
알림창 띄우기(Multi Choice, Single Choice) (0) | 2016.08.21 |
안드로이드 뒤로가기 버튼 더블클릭해서 앱 종료하기 (0) | 2016.08.21 |
인텐트(Intent)로 데이터 전달(putExtra, getExtras) (0) | 2016.08.21 |
SharedPreferences란? (0) | 2016.08.21 |
[Android Intent Useage] 안드로이드 Intent 사용 방법 (0) | 2016.08.21 |
Android Intent - 안드로이드 인텐트 (0) | 2016.08.21 |
댓글