【问题标题】:How to preload interstitial ads - android studio?如何预加载插页式广告 - android studio?
【发布时间】:2016-01-13 10:26:44
【问题描述】:

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

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


        }
    });

}

}

【问题讨论】:

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


    【解决方案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();
            }
        }
    

    【讨论】:

      【解决方案2】:

      使用

      mInterstitialAd.setAdListener(new AdListener() {
          @Override
          public void onAdClosed() {
              requestNewInterstitial();
      
          }
      public void onAdLoaded (){
      //... your code is here
      }
      });
      

      【讨论】:

      • 我应该放哪个sn-p?结束();或 mInterstitialAd.show(); ?
      • @SathyaApps ,如果你想展示使用 InterstitialAd.show(); .我不知道你想要什么行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多