Android非页内广告不显示 [英] Android Interstitial Ad does not show

查看:73
本文介绍了Android非页内广告不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示非页内广告。
这是我要展示广告的活动。

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);

    }

    }

我已经阅读有关Admob中非页内广告的所有内容。但是,没有任何效果。

I've read everything about the Interstitial Ad in Admob. But, nothing works. Could the banner be the problem?

推荐答案

更改 interstitialAd = new InterstitialAd(this);

interstitialAd = new InterstitialAd(getContext());

还要检查 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.

这篇关于Android非页内广告不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆