Skip to content

Instantly share code, notes, and snippets.

@takeshiyako2
Created December 18, 2015 08:49
Show Gist options
  • Save takeshiyako2/4f5a6f1b0b29dadc6e26 to your computer and use it in GitHub Desktop.
Save takeshiyako2/4f5a6f1b0b29dadc6e26 to your computer and use it in GitHub Desktop.
Android AdMob Interstitial
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.admob.com.admobinterstitialtest">
<!-- Include required permissions for Google Mobile Ads to run. -->
<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">
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Include the AdActivity configChanges and theme. -->
<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>
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "test.admob.com.admobinterstitialtest"
minSdkVersion 15
targetSdkVersion 22
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:22.2.1'
compile 'com.google.android.gms:play-services-ads:8.3.0'
}
package test.admob.com.admobinterstitialtest;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
loadinterstitial();
// Listener for Ad
interstitial.setAdListener(new AdListener()
{
// When Closed Ad, Load new Ad
@Override
public void onAdClosed()
{
super.onAdClosed();
loadinterstitial();
}
});
}
private void loadinterstitial()
{
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
}
// Show Interstitial Ad
private void showInterstitialAd()
{
// return, if Ad data is no loaded
if (!interstitial.isLoaded()) {
return;
}
// Show Ad
interstitial.show();
}
// Back button
@Override
public void onBackPressed()
{
super.onBackPressed();
// Make rand
Random rnd = new Random();
// Omikuji
int Omikuji = rnd.nextInt(2);
if (Omikuji == 0) {
// Go to Show Interstitial Ad
showInterstitialAd();
}
}
}
<resources>
<string name="app_name">AdMobInterstitialTEST</string>
<!-- Interstitial ad unit ID -->
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>
@sodikinm
Copy link

not work for me

@sodikinm
Copy link

setAdUnitId
setAdListener
loadAd
isLoaded

error for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment