H5AdsWebViewClient

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
public final class H5AdsWebViewClient extends WebViewClient


A WebViewClient that intercepts and handles H5 ad requests. To use this, set this as the client of the WebView that contains the web apps that will be making H5 ad requests.

IMPORTANT: This WebViewClient cannot be used for multiple WebViews. Every new WebView that will support H5 ads must use a new H5AdsWebViewClient.

Example code:

public void onCreate(Bundle savedInstanceState) {
  ...

  // Initialize early to reduce latency on the session's first ad request.
  MobileAds.initialize(this);

  WebView myWebView = findViewById(R.id.my_webview);

  // JavaScript must be enabled.
  WebSettings settings = myWebView.getSettings();
  settings.setJavaScriptEnabled(true);

  H5AdsWebViewClient h5Client = new H5AdsWebViewClient(this, myWebView);

  // (Optional) any existing WebViewClient
  WebViewClient myClient = ...;
  h5Client.setDelegateWebViewClient(myClient);

  webView.setWebViewClient(h5Client);
}

Summary

Public constructors

H5AdsWebViewClient(Context context, WebView webView)

Constructor for H5AdsWebViewClient.

Public methods

void

Destroys all ads being managed by H5 ads.

@Nullable WebViewClient

Gets the delegate WebViewClient.

void

Sets a WebViewClient to delegate methods not handled by H5AdsWebViewClient.

Protected methods

WebViewClient

Inherited Constants

From android.webkit.WebViewClient
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Inherited methods

From android.webkit.WebViewClient
void
doUpdateVisitedHistory(WebView p, String p1, boolean p2)
void
void
void
void
void
void
void
void
onReceivedHttpAuthRequest(
    WebView p,
    HttpAuthHandler p1,
    String p2,
    String p3
)
void
void
void
boolean
void
onSafeBrowsingHit(
    WebView p,
    WebResourceRequest p1,
    int p2,
    SafeBrowsingResponse p3
)
void
onScaleChanged(WebView p, float p1, float p2)
void

This method is deprecated.

void
WebResourceResponse
boolean
boolean

Public constructors

H5AdsWebViewClient

public H5AdsWebViewClient(Context context, WebView webView)

Constructor for H5AdsWebViewClient.

Parameters
Context context

An activity or application context.

WebView webView

The WebView that will be associated with this H5AdsWebViewClient. JavaScript must be enabled on the WebView.

Public methods

clearAdObjects

public void clearAdObjects()

Destroys all ads being managed by H5 ads.

Note: This method has no effect on any ads created by the non-H5 AdMob API, such as com.google.android.gms.ads.interstitial.InterstitialAd and com.google.android.gms.ads.rewarded.RewardedAd.

getDelegateWebViewClient

public @Nullable WebViewClient getDelegateWebViewClient()

Gets the delegate WebViewClient.

setDelegateWebViewClient

public void setDelegateWebViewClient(@Nullable WebViewClient delegate)

Sets a WebViewClient to delegate methods not handled by H5AdsWebViewClient.

Protected methods

getDelegate

protected WebViewClient getDelegate()