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.

void
doUpdateVisitedHistory(WebView view, String url, boolean isReload)
@Nullable WebViewClient

Gets the delegate WebViewClient.

void
onFormResubmission(WebView view, Message dontResend, Message resend)
void
void
@RequiresApi(api = VERSION_CODES.M)
onPageCommitVisible(WebView view, String url)
void
void
onPageStarted(WebView view, String url, Bitmap favicon)
void
void
@RequiresApi(api = VERSION_CODES.M)
onReceivedError(
    WebView view,
    WebResourceRequest request,
    WebResourceError error
)
void
onReceivedError(
    WebView view,
    int errorCode,
    String description,
    String failingUrl
)

This method is deprecated.

void
onReceivedHttpAuthRequest(
    WebView view,
    HttpAuthHandler handler,
    String host,
    String realm
)
void
@RequiresApi(api = VERSION_CODES.M)
onReceivedHttpError(
    WebView view,
    WebResourceRequest request,
    WebResourceResponse errorResponse
)
void
onReceivedLoginRequest(
    WebView view,
    String realm,
    @Nullable String account,
    String args
)
void
onReceivedSslError(
    WebView view,
    SslErrorHandler handler,
    SslError error
)
boolean
@RequiresApi(api = VERSION_CODES.O)
onRenderProcessGone(WebView view, RenderProcessGoneDetail detail)
void
@RequiresApi(api = VERSION_CODES.O_MR1)
onSafeBrowsingHit(
    WebView view,
    WebResourceRequest request,
    int threatType,
    SafeBrowsingResponse callback
)
void
onScaleChanged(WebView view, float oldScale, float newScale)
void
onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg)

This method is deprecated.

void
void

Sets a WebViewClient to delegate methods not handled by H5AdsWebViewClient.

@Nullable WebResourceResponse
@Nullable WebResourceResponse

This method is deprecated.

boolean
boolean
@RequiresApi(api = VERSION_CODES.N)
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
boolean

This method is deprecated.

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 and .

doUpdateVisitedHistory

public void doUpdateVisitedHistory(WebView view, String url, boolean isReload)

getDelegateWebViewClient

public @Nullable WebViewClient getDelegateWebViewClient()

Gets the delegate WebViewClient.

onFormResubmission

public void onFormResubmission(WebView view, Message dontResend, Message resend)

onLoadResource

public void onLoadResource(WebView view, String url)

onPageCommitVisible

@RequiresApi(api = VERSION_CODES.M)
public void onPageCommitVisible(WebView view, String url)

onPageFinished

public void onPageFinished(WebView view, String url)

onPageStarted

public void onPageStarted(WebView view, String url, Bitmap favicon)

onReceivedClientCertRequest

public void onReceivedClientCertRequest(WebView view, ClientCertRequest request)

onReceivedError

@RequiresApi(api = VERSION_CODES.M)
public void onReceivedError(
    WebView view,
    WebResourceRequest request,
    WebResourceError error
)

onReceivedError

public void onReceivedError(
    WebView view,
    int errorCode,
    String description,
    String failingUrl
)

onReceivedHttpAuthRequest

public void onReceivedHttpAuthRequest(
    WebView view,
    HttpAuthHandler handler,
    String host,
    String realm
)

onReceivedHttpError

@RequiresApi(api = VERSION_CODES.M)
public void onReceivedHttpError(
    WebView view,
    WebResourceRequest request,
    WebResourceResponse errorResponse
)

onReceivedLoginRequest

public void onReceivedLoginRequest(
    WebView view,
    String realm,
    @Nullable String account,
    String args
)

onReceivedSslError

public void onReceivedSslError(
    WebView view,
    SslErrorHandler handler,
    SslError error
)

onRenderProcessGone

@RequiresApi(api = VERSION_CODES.O)
public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail)

onSafeBrowsingHit

@RequiresApi(api = VERSION_CODES.O_MR1)
public void onSafeBrowsingHit(
    WebView view,
    WebResourceRequest request,
    int threatType,
    SafeBrowsingResponse callback
)

onScaleChanged

public void onScaleChanged(WebView view, float oldScale, float newScale)

onTooManyRedirects

public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg)

onUnhandledKeyEvent

public void onUnhandledKeyEvent(WebView view, KeyEvent event)

setDelegateWebViewClient

public void setDelegateWebViewClient(@Nullable WebViewClient delegate)

Sets a WebViewClient to delegate methods not handled by H5AdsWebViewClient.

shouldInterceptRequest

public @Nullable WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)

shouldInterceptRequest

public @Nullable WebResourceResponse shouldInterceptRequest(WebView view, String url)

shouldOverrideKeyEvent

public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)

shouldOverrideUrlLoading

@RequiresApi(api = VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

shouldOverrideUrlLoading

public boolean shouldOverrideUrlLoading(WebView view, String url)

Protected methods

getDelegate

protected WebViewClient getDelegate()