0
我创建了一个小型拼图应用程序,在完成每个关卡的完成后,我都希望展示插页式广告,但它需要几秒钟来加载广告。点击按钮后不久,它将导航到下一个活动,而不显示广告。如果我暂停几秒钟并按下按钮,它将显示广告。请帮助我!如何预加载插页式广告 - android studio?
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();
}
});
}
}
我应该在那里放置哪些片段?完();或mInterstitialAd.show(); ? –
@SathyaApps,如果你想显示使用InterstitialAd.show(); 。我不知道你想要什么行为。 – Vyacheslav