Hi, I have tried to strip as much as possible to show the problem.
1.Start a new project, with an Empty Activity. Company name:
www.testappadverts.com, Application Name: TestAppAdverts, just Phone and Tablet with API 19.
2. Update or create the files below.
3. In the debugger put breakpoints on MainActivity.java lines; 305, 373 and 379.
4. Let the program load.
5. The European stuff about GDPR dialog should come up. (I am in the UK, so you may need to force yourself to be in the EU.)
6. Click the top one saying personal advertising.
7, Wait and an ad comes up at the bottom in the white bar. Everything OK so far.
8. Now click the button on the Webpage saying 'Trigger Full Page Advert'.
9. The breakpoints will lead you through. It gets to the code to display a full advert, but does nothing.
(Note in the triggerAdvert function in MainAtivity, when I set
m_NextAdvertFullScreen = false; and unhash the adView and adSize code, it should increase the BANNER ad size, but just crashes instead.)
I tried to upload the whole project, but it has memory limits, so here are all the files:
app|java|com.testappadverts.www.testappadverts|MainActivity.java
package com.testappadverts.www.testappadverts;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.ads.consent.ConsentForm;
import com.google.ads.consent.ConsentFormListener;
import com.google.ads.consent.ConsentInfoUpdateListener;
import com.google.ads.consent.ConsentInformation;
import com.google.ads.consent.ConsentStatus;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
// MUST REMOVE FOR RELEASE
private static final String myDeviceIdForTestConsent = "8CE710413B34F4BB57DDD60BD1D880D6"; // Search for // Todo: Remove in Release build
// Keep a reference here
WebAppInterface webInterface;
// This is the page advert
boolean advertsOn = true;
long minutesDay = 24 * 60;
long minutesLastPageAdvert = 0;
private InterstitialAd mInterstitialAd;
// This is the consent form
private ConsentForm form;
private boolean m_NextAdvertFullScreen = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// This if want to hide title bar
//requestWindowFeature(Window.FEATURE_NO_TITLE); // This seems to have no effect
// Instead in app|res|value|styles.xml change
// <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
// To <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
// The following FLAG_FULLSCREEN gets rid of the battery etc info at top
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// The GDPR consent
// https://gist.github.com/ameerhamza6733/36d88c6a74c9ae6dd7b6355cee0d5de7
checkForConsent();
WebView webview = (WebView) findViewById(R.id.webView);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/www/index.html?IsAndroidWebview=true"); // This is a cheat to tell the html we are calling in AndroidApp
//webview.loadUrl("file:///android_asset/www/index.html"); // This is a cheat to tell the html we are calling in AndroidApp
// These things allegedly make it faster
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(false);
//settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setScrollbarFadingEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webInterface = new WebAppInterface(this, webview);
webview.addJavascriptInterface(webInterface, "Android");
// Adverts
// Must do this before the billing is initialized,
// So getIAP will be able to work on the advert
AdView adView;
String myAdModAppId = "ca-app-pub-3940256099942544~3347511713"; // This is for testing
MobileAds.initialize(this, myAdModAppId);
// This is when you add your adview programatically - it needs to be added to the webview
//adView = new AdView(this);
//adView.setAdSize(AdSize.BANNER);
//adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Each context should have a new id
// Load an advert
/*
adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
//View view = getView();
//if (view != null) {
// Toast.makeText(getView().getContext(), "Thank you for tapping an advert.", Toast.LENGTH_SHORT).show();
//}
Window win;
Context cont;
if((win = getWindow()) != null && (cont = win.getContext()) != null) {
Toast.makeText(cont, "Thank you for tapping an advert.", Toast.LENGTH_SHORT).show();
}
}
});
*/
// This is the page advert
/*
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
// Load it at start, so it is ready
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
// Turn off the game loop
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
// This method is invoked when when the interstitial ad is closed
// due to the user tapping on the close icon or using the back button.
// If your app paused its audio output or game loop,
// this is a great place to resume it.
// Get the next advert to load
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
*/
}
// --- Adverts ---
public void turnAdvertsOnOff(boolean on) {
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest;
if(on){
advertsOn = true;
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView.setVisibility(View.VISIBLE);
}else {
advertsOn = false;
adView.destroy();
adView.setVisibility(View.GONE);
}
}
public void loadPageAdvert() {
long minutes = System.currentTimeMillis() / (60000);
if (minutes > (minutesDay + minutesLastPageAdvert)){
//mInterstitialAd.isLoaded()) {
minutesLastPageAdvert = minutes;
mInterstitialAd.show();
}
}
// --- Advert Consent ---
private void checkForConsent() {
ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
String[] publisherIds = {"pub-id-from-publishers-like-admob"};
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();
break;
case NON_PERSONALIZED:
showNonPersonalizedAds();
break;
case UNKNOWN:
if (ConsentInformation.getInstance(getBaseContext())
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
break;
}
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});
}
private void requestConsent() {
URL privacyUrl = null;
try {
privacyUrl = new URL("http://www.chartmygolf.com/privacy.html");
} catch (MalformedURLException e) {
//e.printStackTrace();
// Handle error.
}
form = new ConsentForm.Builder(MainActivity.this, privacyUrl)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
// Consent form loaded successfully.
showForm();
}
@Override
public void onConsentFormOpened() {
// Consent form was displayed.
}
@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
if (userPrefersAdFree) {
// Buy or Subscribe
//webInterface.returnToHtmlOnMyMenu("divInAppPurchase");
} else {
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();break;
case NON_PERSONALIZED:
showNonPersonalizedAds();break;
case UNKNOWN:
showNonPersonalizedAds();break;
}
}
// Consent form was closed.
}
@Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.withAdFreeOption()
.build();
form.load();
}
/*
want test your app watch this video https://youtu.be/_JOapnq8hrs?t=654
*/
private void showPersonalizedAds() {
/* this line code save consent status if you want to show your ads in next activty just get getConsentStatus and load ads accouding to status e.g.
MainActivty2 ConsentStatus consentStatus =ConsentInformation.getInstance(this).getConsentStatus();
if (consentStatus.toString().equals("NON_PERSONALIZED")) loadNonPersonlizedAds(); i hateeeeeeeeeet This new policy
*/
ConsentInformation.getInstance(this).setConsentStatus(ConsentStatus.PERSONALIZED);
if(m_NextAdvertFullScreen) {
m_NextAdvertFullScreen = false;
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("Add_unit");
mInterstitialAd.loadAd(new AdRequest.Builder()
// adView = findViewById(R.id.adView);
// adSize = adView.getAdSize();
// if(adSize != AdSize.LARGE_BANNER)
// adView.setAdSize(AdSize.LARGE_BANNER);
m_NextAdvertFullScreen = true;
checkForConsent();
}
}
}
app|java|com.testappadverts.www.testappadverts|MainActivity.java
package com.testappadverts.www.testappadverts;
import android.content.Context;
import android.webkit.WebView;
public class WebAppInterface {
Context mContext;
//private final Callback callback;
WebView webview;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c, WebView w) {
mContext = c;
//this.callback = callback;
webview = w;
}
// --- This to trigger a new major advert update ---
@android.webkit.JavascriptInterface
public void triggerAdvert() {
((MainActivity) mContext).triggerAdvert();
}
}
app|manifests|AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testappadverts.www.testappadverts">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
app|assets|www|index.html
<!DOCTYPE html>
<html>
<head>
<title>Thought Provoking Title</title>
<!-- This following line is for mobiles, it makes sure they work properly -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<!-- Thought this would stop zooming but it does not -ms-content-zooming: none | zoom; -->
<body style="width:100%; height: 100%; padding: 0 0 0 0; margin: 0 0 0 0; overflow: hidden; background:#FFAAAA; font-family: 'Arial', sans-serif;">
<div>
<h3>A Thought Provoking Title</h3>
<p>Great words of profound wisdom.</p>
<button onclick="Android.triggerAdvert();">Trigger Full Page Advert</button>
</div>
</body>
</html>
app|res|layout|activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/webView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintTop_toTopOf="parent"