Reputation: 19
I am trying to integrate AdMob in my application and i have checked successfully AdMob example and got add on my android device but when i am trying to do same code for my app that s not working ... Please help
My Code
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("0C3BF98F2CA55F66D0BE2D63D962BF6D")
.build();
mInterstitialAd.loadAd(adRequest);
}
});
}
}
InterstitialAd mInterstitialAd;
void StartAds()
{
MobileAds.initialize(this, "ca-app-pub-3940256099942544~1033173712");
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(getString(R.string.google_home_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
}
});
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
startGame();
}
}
manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ads.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "example.ads.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.firebase:firebase-ads:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
/
/ Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Upvotes: 2
Views: 453
Reputation: 81
Check this link it's not your fault.
I have same problem and it's solution is increase you app user so send ad request to admob and show your ad.
It's rule is latest rule and admob privacy make strong so apply this rule
Before 2017 it's rule is not so ad show is work fine, but right now first to send some ad request than ad show
I hope solve your question.
When apps are newly registered with AdMob, it takes some time and a few ad requests to allow inventory to build. Because of this, you may not see live impressions immediately.
Upvotes: 0
Reputation: 77
For Banner Ads
java class
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
xml
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
ads:adSize="BANNER"
ads:adUnitId="@string/adUnitID">
//dependencies
dependencies{
compile 'com.google.android.gms:play-services:9.2.0'
}
//String
<string name="adUnitID">your add mob UnitID</string>
<string name="interstial_id">your add mob UnitID</string>
//manifests
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
For Interstitial Ads
java class
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
interstitial = new InterstitialAd(INTERSTIAL_ads_Activity.this);
interstitial.setAdUnitId(getResources().getString(R.string.interstial_id);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
private void displayInterstitial(){
if (interstitial.isLoaded()) {
interstitial.show();
}
}
Upvotes: 4