【发布时间】: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);
}
【问题讨论】:
标签: java android admob interstitial