在android上加载webview后没有显示插页式广告?

Posted

技术标签:

【中文标题】在android上加载webview后没有显示插页式广告?【英文标题】:Interstitial ad is not showing up after webview is loaded on android? 【发布时间】:2021-08-09 10:28:57 【问题描述】:

我已经使用 java 在我的 android 应用上实现了插页式 admob 广告。当广告在 web 视图之前加载时,插页式广告正确显示,但如果在 web 视图加载后加载,则插页式广告变得无响应,即无法关闭。简而言之,如果 webview 在 interstitial admob 广告的 onAdLoaded 方法之前加载,则 interstitial ad 不会显示。

请为此建议适当的解决方案。谢谢

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

【问题讨论】:

【参考方案1】:

我能够通过删除对pauseTimers()的所有引用来解决它

【讨论】:

以上是关于在android上加载webview后没有显示插页式广告?的主要内容,如果未能解决你的问题,请参考以下文章

在带有导航应用程序的加载页面的计时器上加载插页式广告?

表单提交后,WebView 未加载页面

在每 x 次加载的 viewdidload 上显示插页式广告

在android webview应用程序的页面上单击链接时如何防止显示加载微调器?

如何在 WebView App 中的 URL 点击设置插页式广告?

插页式dfp广告未在Android平板电脑上全屏显示