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


     } 
    }); 

} 

}

回答

0

上的应用程序开始调用init方法使用

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

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

我应该在那里放置哪些片段?完();或mInterstitialAd.show(); ? –

+0

@SathyaApps,如果你想显示使用InterstitialAd.show(); 。我不知道你想要什么行为。 – Vyacheslav

0

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