[英]Interstitial ad is not showing up after webview is loaded on android?
我已經使用 java 在我的 android 應用程序上實施了插頁式 admob 廣告。 當廣告在 webview 之前加載時,插頁式廣告可以正確顯示,但如果在 webview 加載之后加載,則插頁式廣告變得無響應,即無法關閉。 簡而言之,如果 webview 在插頁式 admob 廣告的 onAdLoaded 方法之前加載,則插頁式廣告不會顯示。
請為此提出適當的解決方案。 謝謝
Admob 插頁碼:
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
getContext(),
"ca-app-pub-xxx/yyy",
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.d("TAG", "onAdLoaded");
mInterstitialAd.show(getActivity());
Toast.makeText(getContext(), "onAdLoaded()", Toast.LENGTH_SHORT).show();
mInterstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d("TAG", "The ad was dismissed trending videos.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
用於顯示 webview 的代碼:
{
String urllink = "https://www.example.com.com/";
// Save the web view
webView = (VideoEnabledWebView)rootView.findViewById(R.id.webView);
webView.onPause(); // This will pause videos and needs to be called for EVERY WebView you create
webView.pauseTimers(); // This will pause JavaScript and stop_btn for ALL WebViews and only needs to be called once to affect all WebViews
// Initialize the VideoEnabledWebChromeClient and set event handlers
View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout); // Your own view, read class comments
ViewGroup videoLayout = (ViewGroup)rootView.findViewById(R.id.videoLayout); // Your own view, read class comments
//noinspection all
View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
{
// Subscribe to standard events, such as onProgressChanged()...
@Override
public void onProgressChanged(WebView view, int progress)
{
// Your code...
}
};
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
@Override
public void toggledFullscreen(boolean fullscreen)
{
// Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
if (fullscreen)
{
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getActivity().getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
else
{
((AppCompatActivity) getActivity()).getSupportActionBar().show();
WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getActivity().getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
});
webView.setWebChromeClient(webChromeClient);
// Call private class InsideWebViewClient
webView.setWebViewClient(new InsideWebViewClient());
extraHeaders = new HashMap<>();
extraHeaders.put("Set-Cookie","HttpOnly;Secure;SameSite=Strict");
webView.loadUrl(urllink,extraHeaders);
}
嗨,我能夠通過刪除對pauseTimers()
的所有引用來解決它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.