gpt4 book ai didi

java - Admob 广告在使用 webview 和进度条的应用程序中不可见

转载 作者:行者123 更新时间:2023-12-01 16:33:46 24 4
gpt4 key购买 nike

我尝试为我的网站制作一个带有 WebView 和进度条的应用程序, WebView 的可见性在页面加载之前保持隐藏。现在我尝试在底部添加 google admob 横幅广告,但它没有显示,我使用的是相对布局,广告显示在 xml 预览中,但不显示在应用程序中。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<WebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone" />

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="#000000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3467064285652442/5673227109">
</com.google.android.gms.ads.AdView>

<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:layout_constraintStart_toStartOf="@+id/activity_main_webview"
app:layout_constraintTop_toTopOf="@+id/activity_main_webview" />



</RelativeLayout>

MainActivity.java:

package com.dhruv.spadebee;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
import android.util.Log;
import android.view.MenuItem;
import android.view.Window;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.view.View;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

String ShowOrHideWebViewInitialUse = "show";
private WebView mWebView;
private ProgressBar spinner;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#080808")));
final ImageView img = (ImageView) findViewById(R.id.toolbar_top);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, img);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
mWebView.loadUrl("https://spadebee.com/");
break;
case R.id.two:
mWebView.loadUrl("https://spadebee.com/category/general/");
break;

case R.id.three:
mWebView.loadUrl("https://spadebee.com/category/programming/");
break;

case R.id.four:
mWebView.loadUrl("https://spadebee.com/category/gaming/");
break;

case R.id.five:
mWebView.loadUrl("https://spadebee.com/category/android/");
break;

case R.id.six:
mWebView.loadUrl("https://spadebee.com/about");
break;

}

return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method






OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Stop local links and redirects from opening in browser instead of WebView
spinner = (ProgressBar)findViewById(R.id.progressBar1);
mWebView.setWebViewClient(new MyAppWebViewClient() {
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(webview.INVISIBLE);
}
}


@Override
public void onPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('custom-header')[0].style.display='none'; " +
"})()");
mWebView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('main-navigation')[0].style.display='none'; " +
"})()");
spinner.setVisibility(View.GONE);

mWebView.setVisibility(WebView.VISIBLE);
super.onPageFinished(view, url);
}
});
mWebView.loadUrl("https://spadebee.com/");
}

@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {

Log.i("OSNotificationPayload", "result.notification.payload.toJSONObject().toString(): " + result.notification.payload.toJSONObject().toString());
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
String appURL = result.notification.payload.launchURL;
Log.i("appURL", appURL);
mWebView.loadUrl("https://spadebee.com");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
}
public class jparser {
public void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href].more-link");
Elements media = doc.select("img");
Elements summary = doc.select("div.entry-summary");

}
}
}

预览:

Android Studio preview

任何有关它不起作用的帮助将不胜感激,我已尝试浏览此处的所有帖子,在线性布局中添加进度条和网页 View ,将广告 View 可见性设置为可见,但没有任何效果。

最佳答案

锁定日志输出的最后一行

I/Ads: Ad failed to load : 3

指出加载广告时出现问题,因此可能没有任何内容可显示?

正如这篇文章所述failed to load ad : 3关于错误“3”:

If you are getting this error, then your code is correct. The issue is that AdMob does not always have an ad to return for every request.

所以也许这只是一个暂时的问题,您是否在不同的设备上或稍稍休息后尝试过您的应用程序?您也可以尝试使用 Android 示例广告 ID https://developers.google.com/admob/android/test-ads#sample_ad_units他们通常工作

关于java - Admob 广告在使用 webview 和进度条的应用程序中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62003784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com