如何在应用程序关闭时显示插页式广告?

时间:2014-08-11 23:40:10

标签: android admob ads interstitial

我有这个代码,并且工作得很好,但是当我启动应用时,广告会显示,并且当我按下(退出应用)并按是时我想显示广告。所以当完成();发生了,我想展示我的插页式广告。

package inter.ad;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;



public class MainActivity extends Activity implements AdListener {

  private InterstitialAd interstitial;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the interstitial
    interstitial = new InterstitialAd(this, "my ad id");

    // Create ad request
    AdRequest adRequest = new AdRequest();

    // Begin loading your interstitial
    interstitial.loadAd(adRequest);

    // Set Ad Listener to use the callbacks below
    interstitial.setAdListener(this);
  }

  @Override
  public void onReceiveAd(Ad ad) {
    Log.d("OK", "Received ad");
    if (ad == interstitial) {
      interstitial.show();
    }
  }

@Override
public void onDismissScreen(Ad arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onLeaveApplication(Ad arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPresentScreen(Ad arg0) {
    // TODO Auto-generated method stub

}



public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        exitByBackKey();

        //moveTaskToBack(false);

        return true;
    }
    return super.onKeyDown(keyCode, event);
}
protected void exitByBackKey() {

    AlertDialog alertbox = new AlertDialog.Builder(this)
    .setMessage("Do you want to Exit?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

            finish();
            //close();


        }
    })

    .setNeutralButton("Rate us!", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

             {
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getApplicationContext(), "Couldn´t launch Google Play", 
                            Toast.LENGTH_LONG).show();
                }
            }


        }
    })

    .setNegativeButton("No", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {
                       }
    })
      .show();
    }
 }

2 个答案:

答案 0 :(得分:4)

不要这样做。

插页式广告适用于您在应用中停留的情况,但我保证当您在退出应用时尝试向他们发送垃圾邮件时,他们的手指会非常快速地流入"卸载"按钮。

如果您想在活动离焦时执行操作,则该活动属于onPause()onStop()onDestroy(),具体取决于您需要的控制级别。

详细了解Android documentation如何做到这一点。

答案 1 :(得分:3)

我不是建议你这样做。请阅读谷歌的这篇博客

https://support.google.com/admob/answer/6201362?hl=en

我的2份申请因此被拒绝。请不要这样做。