简体   繁体   English

Android非页内广告不显示

[英]Android Interstitial Ad does not show

I'm having a problem with an Interstitial Ad that is not displaying. 我无法显示非页内广告。 This is the activity that I want to show the ad. 这是我要展示广告的活动。

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.enjoyyourriding.example.diana.mountainbikequest.DbHelper.DbHelper;
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.enjoyyourriding.example.diana.mountainbikequest.R.id.txtTotalScore;

public class Done extends AppCompatActivity {
    Button btnTryAgain;
    TextView txtResultScore, txtResultQuestions;
    ProgressBar progressBarResult;
private InterstitialAd interstitialAd;
    final String TAG = this.getClass().getName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_done);
        AdView adview = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
        adview.loadAd(adRequest);

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-6594925022395892/2492722367");
        interstitialAd.loadAd(adRequest);
        Typeface mytupeface = Typeface.createFromAsset(getAssets(), "Helveticrap.ttf");
        TextView myTextView = (TextView) findViewById(R.id.textView);
        myTextView.setTypeface(mytupeface);
        DbHelper db = new DbHelper(this);
        btnTryAgain = (Button) findViewById(R.id.btnTryAgain);
        txtResultScore = (TextView) findViewById(txtTotalScore);
        txtResultQuestions = (TextView) findViewById(R.id.txttotalQuestions);
        btnTryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);

                finish();
            }

        });

        adview.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }



            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                finish();
            }

            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }
        });

        Bundle extra = getIntent().getExtras();
        if (extra != null) {
            int score = extra.getInt("SCORE");
            int totalQuestions = extra.getInt("TOTAL");
            int correctAnswer = extra.getInt("CORRECT");
            txtResultScore.setText(String.format("SCORE : %d", score));
            txtResultQuestions.setText(String.format("PASSED : %d/%d", correctAnswer, totalQuestions));

            if (score == 225) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage5,
                        (ViewGroup) findViewById(R.id.custommessage5));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("You are one of the best!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                    toast.show();
                    db.insertScore(score);
                }
            }else if (score == 250) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage6,
                        (ViewGroup) findViewById(R.id.custommessage6));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("You are true mountain bike legend!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);


                }

            }else if (score == 200) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage4,
                        (ViewGroup) findViewById(R.id.custommessage4));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("Well done, you passed 200 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 170) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage7,
                        (ViewGroup) findViewById(R.id.custommessage7));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("Well done, you passed 170 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }
            }else if (score == 140) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage3,
                        (ViewGroup) findViewById(R.id.custommessage3));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 140 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 110) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage8,
                        (ViewGroup) findViewById(R.id.custommessage8));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 110 questions!");


                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();
                db.insertScore(score);


            }else if (score == 30) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.customtoast,
                        (ViewGroup) findViewById(R.id.customtoast));

                TextView text = (TextView) layout.findViewById(R.id.textView5);
                text.setText("Congratulations, you passed 30 questions!");
                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);

                }
            }else if (score == 50) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage9,
                        (ViewGroup) findViewById(R.id.custommessage9));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 50 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 80) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage2,
                        (ViewGroup) findViewById(R.id.custommessage2));

                TextView text = (TextView) layout.findViewById(R.id.textView6);
                text.setText("Congratulations, you passed 80 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }
            else if(score>=51&& score <=70  ){
               if (interstitialAd.isLoaded()){
                   interstitialAd.show();
               }
                interstitialAd.loadAd(adRequest);

            }


            db.insertScore(score);
        }
    }

    boolean twice;

    @Override
    public void onBackPressed() {

        Log.d(TAG, "click");

        if(twice==true){
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();


        }
        twice = true;
        Log.d(TAG, "twice:" + twice);

        Toast.makeText(Done.this,"Please press BACK again to exit the application",Toast.LENGTH_LONG).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                twice = false;
                Log.d(TAG, "twice:" + twice);
            }
        }, 3000);

    }

    }

I've read everything about the Interstitial Ad in Admob. 我已经阅读了有关Admob中非页内广告的所有内容。 But, nothing works. 但是,没有任何效果。 Could the banner be the problem? 横幅广告可能是问题吗?

Change interstitialAd = new InterstitialAd(this); 更改interstitialAd = new InterstitialAd(this); to
interstitialAd = new InterstitialAd(getContext());

And also check if the adUnitId is correct. 还要检查adUnitId是否正确。

Otherwise everything else seems fine. 否则,其他一切似乎都很好。 And, no, Interstitial ad wont be affected by the banner in any form since both are completely different entities and are independent of each other. 而且,不,插页式广告不会以任何形式受到横幅广告的影响,因为两者都是完全不同的实体并且彼此独立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM