每次按下按钮时如何显示Admob非页内广告

时间:2016-11-08 13:16:25

标签: java android android-studio admob interstitial

在Android开发方面,我是一个初学者。这是我的第一个申请。我想知道每次按下按钮时如何显示Admob插页式广告。我设法显示插页式广告,但仅在第一次按下按钮时才显示。但是,我在尝试加载广告时收到错误消息。这是我的代码:

package com.mycash.borgjake.mycash;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;

import android.view.View;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import static com.mycash.borgjake.mycash.R.styleable.View;

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitial;


Button btnClick;
Button btnWithdraw;

TextView txtBalance;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdView adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    adView.loadAd(adRequest);

    btnClick = (Button) findViewById(R.id.button);

    txtBalance = (TextView) findViewById(R.id.textView);

    btnClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String countValue = txtBalance.getText().toString();
            int dblCountValue = Integer.parseInt(countValue);

            if (mInterstitial.isLoaded()) {
                mInterstitial.show();
            }

            mInterstitial.loadAd(request); //<---ERROR

            dblCountValue++;
            txtBalance.setText(String.valueOf(dblCountValue));



        }
    });

    mInterstitial = new InterstitialAd(this);
    mInterstitial.setAdUnitId("ca-app-pub-2245289628812463/9905502438");
    AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mInterstitial.loadAd(request);

}

public void onButtonClick(View v) {
    if(v.getId() == R.id.button2) {
        Intent i = new Intent(MainActivity.this, Withdraw.class);
        startActivity(i);
    }
  }
}

提前感谢您的帮助。 :)

2 个答案:

答案 0 :(得分:0)

你不必每次都调用这个函数mInterstitial.loadAd(request);

或者如果你想打电话就叫它那样

mInterstitial.loadAd(request);
  if (mInterstitial.isLoaded()) {
                    mInterstitial.show();
  }

或每次点击按钮时删除此mInterstitial.loadAd(request);

答案 1 :(得分:0)

只需创建一个功能,并在需要时随时随地调用它:

public static void getAd(Context c) {

        //================Ad-section
        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(c);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app-pub-YOUR/ID");

        // Request for Ads
                AdRequest adRequest = new AdRequest.Builder()
                .build();

                // Load ads into Interstitial Ads
                interstitial.loadAd(adRequest);

                interstitial.setAdListener(new AdListener() {
                    public void onAdLoaded() {
                        // Call displayInterstitial() function
                        displayInterstitial();
                    }
                });
            }
            public static void displayInterstitial() {
                // If Ads are loaded, show Interstitial else show nothing.
                if (interstitial.isLoaded()) {
                    interstitial.show();
                }
      //=======================
    }

要在模拟器或测试设备中运行,请使用

public static void getAd(Context c) {

            //================Ad-section
        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(c);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app-pub-YOUR/ID");

        AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mInterstitial.loadAd(request);
}

致电该功能:

getAd(getApplicationContext());