Skip to content Skip to sidebar Skip to footer

Showing Admob Interstitial In Non-activity Class

In my game, I have a class which constructs the Gameview and allows the user to play various levels till he want. Now I want to show an admob interstitial when the level is over. T

Solution 1:

You're calling it from a worker thread. You need to call from within the main thread. You could use a handler, for example.

 activity.runOnUiThread(newRunnable() {
      publicvoidrun() {
        InterstitialAdinterstitial=newInterstitialAd((MainActivity)mContext,
        "admobunitid");
// Create ad requestAdRequestadRequest=newAdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener((MainActivity)mContext);
      }
    });

Solution 2:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

This usually happens when you try to show a Dialog from outside the Main Thread.

Try to wrap that code inside a Runnable and then execute it using MainActivity.runOnUiThread();

Solution 3:

Are there any contraindications of using interfaces? Because using them in this case would be resonable.

Solution 4:

This is not Activity Class then you need to call within runOnUiThread. Like it is android asynctask class doBackground method then you need to call like this.

activity/context.runOnUiThread(newRunnable() {
  publicvoidrun() {
    /// your Ui view amd messageInterstitialAdinterstitial=newInterstitialAd((MainActivity)mContext,
        "admobunitid");
// Create ad requestAdRequestadRequest=newAdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener((MainActivity)mContext);
  }
});

Thanks

Post a Comment for "Showing Admob Interstitial In Non-activity Class"