Android:点击按钮启动广告

时间:2014-02-19 11:45:27

标签: android

我开发了一个Android应用程序,我计划在Google Playstore上免费启动它。但是,出于收入目的,我已在我的应用中嵌入了AdMob。根据我的研究,开发人员只需点击广告即可获得付款。

我想要做的是,当用户按下我的应用的退出按钮时,我想在关闭我的应用之前从AdMob发布一些广告/广告。

为了达到这个目的,我有以下代码:

Button exitButton = findViewById(R.id.exitButton);
exitButton.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /*Some code here that would launch an AD*/

            MyActivity.this.finish(); //To exit from the app
        }
    }); 

在布局的AdView中显示广告的代码是:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
AdView adView = new AdView(this, AdSize.BANNER, AdUnitID);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR); //this is just for testing on emulator
adView.loadAd(adRequest);

有没有办法在按下退出按钮时启动广告的内容?

提前致谢

2 个答案:

答案 0 :(得分:1)

我认为您可能需要广告插播广告,即应用中显示的全屏广告。你要求的是一个小横幅,它应该出现在每个活动中,而不仅仅是在最后。

关于插页式广告的信息:

https://developers.google.com/mobile-ads-sdk/docs/admob/advanced

此外,强迫用户点击是被admob禁止的,可能你会被禁止你的帐户(你的费率会太高,有人会检查)

答案 1 :(得分:1)

在活动onbackpressed或退出按钮onclicklistener代码中,加载插页式广告并完成()活动。

 public class InterstitialsAdsExampleActivity extends Activity implements
    AdListener {
/** Called when the activity is first created. */
private InterstitialAd interstitialAds = null;


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

    this.interstitialAds = new InterstitialAd(this, "Your ad unit id");
    this.interstitialAds.setAdListener(this);

}



public void onBackPressed() {
    AdRequest adr = new AdRequest();

    interstitialAds.loadAd(adr);

    finish();
};

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

}

@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
    String message = "Load Ads Failed: (" + error + ")";

}

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

/**
 * Called when an Activity is created in front of the app (e.g. an
 * interstitial is shown, or an ad is clicked and launches a new Activity).
 */
@Override
public void onPresentScreen(Ad arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onReceiveAd(Ad arg0) {
    if (interstitialAds.isReady()) {
        interstitialAds.show();
    } else {
        //Interstitial ad was not ready to be shown;
    }
}

}