1

So I found some code (How to call admob Interstitial ad from android webview JavascriptInterface) that allows the use of javascript on a webview page to activate within the app.

Now The issue I'm having is when the button is pressed, it toasts that the Ad hasn't loaded. Why isn't it loading the ad?

Here is the full code used in the MainActivity.java

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {

public InterstitialAd mInterstitialAd;
WebView ProfOak;
String URL;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdView mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    URL = "http://url-here";
    ProfOak = findViewById(R.id.ProfOak);

    ProfOak.getSettings().setJavaScriptEnabled(true);
    ProfOak.getSettings().setDomStorageEnabled(true);
    ProfOak.getSettings().setUseWideViewPort(true);
    ProfOak.setWebChromeClient(new WebChromeClient());
    ProfOak.addJavascriptInterface(new WebAppInterface(this), "Android");
    ProfOak.setVerticalScrollBarEnabled(false);
    ProfOak.setWebChromeClient(new WebChromeClient());
    ProfOak.setBackgroundColor(Color.TRANSPARENT);
    ProfOak.loadUrl(URL);
    ProfOak.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView ProfOak, int errorCode, String description, String failingUrl) {
            try {
                ProfOak.stopLoading();
            } catch (Exception e) {
            }

            if (ProfOak.canGoBack()) {
                ProfOak.goBack();
            }

            ProfOak.loadUrl("about:blank");
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("Connection Required");
            alertDialog.setMessage("This app requires an active Internet Connection to communicate with the Database.");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    startActivity(getIntent());
                }
            });

            alertDialog.show();
            super.onReceivedError(ProfOak, errorCode, description, failingUrl);
        }
    });

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/5224354917");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener(){
        @Override
        public void onAdLoaded(){

        }
        @Override
        public void onAdClosed() {
            // Load the next interstitial.
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });
}
public void displayLoadedAd(){
    runOnUiThread(new Runnable() {
        public void run() {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
            else
                Toast.makeText(getApplicationContext(), "Ad not loded", Toast.LENGTH_SHORT).show();
        }

    });


}
public class WebAppInterface {
    Context mContext;
    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }
    @JavascriptInterface
    public void showAdFromJs(){

        Toast.makeText(mContext, "Loading Ad", Toast.LENGTH_SHORT).show();
        displayLoadedAd();
    }
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;

    }
}

//Controlling navigation
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (ProfOak.canGoBack()) {
                    ProfOak.goBack();
                }
                else {
                    finish();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

}

This is what i have in the webview URL

<input class="button" type="button" value="Ignore this" onclick="myFunction()">
<script type="text/javascript">
function myFunction() { Android.showAdFromJs(); };
</script>

1 Answers1

0

It seems that you are using ad-unit id for rewarded video i.e ca-app-pub-3940256099942544/5224354917. For Interstitial the test ad unit id is ca-app-pub-3940256099942544/1033173712. Refer to https://developers.google.com/admob/android/test-ads

Md Junaid Alam
  • 1,131
  • 13
  • 20
  • Yeah it didnt matter if I used the other, it still showed the ad not loading. I was seing if it would detect the video ad (making it load) with a no go. Still does it with the Interstitial ca-app-pub-3940256099942544/1033173712 – Adam Wakefield Apr 12 '18 at 11:54