如何预加载插页式广告 - android studio?

时间:2016-01-13 10:26:45

标签: android android-layout android-fragments android-intent android-activity

我创建了一个小型拼图应用,在每个级别完成后我想展示插页式广告,但加载广告只需几秒钟。单击按钮后不久,它会导航到下一个活动而不显示广告。如果我暂停几秒钟并按下按钮则显示广告。请帮帮我!

public class Completed extends AppCompatActivity {

InterstitialAd mInterstitialAd;

public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setMessage("Do you want to go to Home?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user pressed "yes", then he is allowed to exit from application
            finish();
        }
    });
    builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user select "No", just cancel this dialog and continue with app
            dialog.cancel();
        }
    });
    AlertDialog alert=builder.create();
    alert.show();
}


private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

    //AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_completed);

    requestNewInterstitial();
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxx");

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();

        }
    });

    //requestNewInterstitial();

    Typeface MyTypeFace=Typeface.createFromAsset(getAssets(),"comic.ttf");
    Button btn=(Button)findViewById(R.id.btncomplete);
    TextView txt=(TextView)findViewById(R.id.txtcomplete);
    btn.setTypeface(MyTypeFace);
    txt.setTypeface(MyTypeFace);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MainActivity.setsound) {
                MainActivity.mp.start();
            }
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();

            }
            finish();


        }
    });

}

}

2 个答案:

答案 0 :(得分:1)

在应用程序启动时调用init方法:

public void init()
    {
        mInterstitialAd = new InterstitialAd(baseAppController);
        mInterstitialAd.setAdUnitId(baseAppController.getString(R.string.interstitial_ad_unit_id));
        mInterstitialAd.setAdListener(new AdListener()
        {
            @Override
            public void onAdClosed()
            {
                requestNewInterstitial();
            }
        });
        requestNewInterstitial();
    }
private void requestNewInterstitial()
    {
        mInterstitialAd.loadAd(getAdRequest());
    }

    public AdRequest getAdRequest()
    {
        AdRequest adRequest = new AdRequest.Builder()
                //.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
//                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
        return adRequest;
    }

当您想要显示广告时,请调用以下方法:

public void showInterstitial()
    {
        if (mInterstitialAd.isLoaded())
        {
            mInterstitialAd.show();
        }
    }

答案 1 :(得分:0)

使用

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
        requestNewInterstitial();

    }
public void onAdLoaded (){
//... your code is here
}
});