I am creating an app that will display interstitial ads through Google AdMob. I have checked and double checked my codes over to see if I missed a step or something wasn't right, and from what I can tell it's correct according to this website. If I use the test id that they provide us to use for testing, the ad will popup. If I replace the test id with mine that I created almost two weeks ago, it will not popup. I get the following error in my logcat...
E/GmsClient: unable to connect to service: com.google.android.gms.leibniz.events.service.START on com.google.android.gms
Here are my codes...
MainActivity.Java
The following code is above the onCreate.
private InterstitialAd mInterstitialAd;
The following code is under the onCreate.
MobileAds.initialize(view.getContext(), new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(view.getContext(), myAdUnitID, adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
if (mInterstitialAd != null) {
mInterstitialAd.show(this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mathiasapps.tempconverterfreeversion">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_therm_icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_therm_icon_round"
android:supportsRtl="true"
android:theme="@style/Theme.TempConverter">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Help"
android:theme="@style/Theme.TempConverter"
android:parentActivityName=".MainActivity"
android:label="Help"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.support.PARENT_ACTIVITY"/>
<category android:name="com.mathiasapps.tempconverter.Help"/>
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-6771696786744697~7616850816"/>
</application>
</manifest>
Build.gradle(Project)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://maven.google.com/"}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.gradle(Module)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.mathiasapps.tempconverterfreeversion"
minSdkVersion 23
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.google.firebase:firebase-analytics:19.0.2'
implementation 'com.google.android.gms:play-services-ads:20.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Thanks! I appreciate the help!
CodePudding user response:
Your interstitial ads implementation is fine, and you don't have to do anything else to load real ads.
Test ads are showing and your real ads are not, is because Admob doesn't start showing ads until you get a certain amount of traffic on your app. You shouldn't try to load real ads from your device. As Admob will detect it and will place limited ad serving on your account.
From Admob documentation
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
Never test with real ads, if test ads are working fine then real ads will work too.