Android studio 添加admob googgle play services

Android studio 添加admob googgle play services

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

private InterstitialAd mInterstitialAd;
 // Initialize the Mobile Ads SDK.
MobileAds.initialize(getApplicationContext(), "ca-app-pub-8067415904846806/59920985");
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId("xxx");

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// startGame();
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.i("sdf","Ad loaded !");
}
});
AdRequest adRequest = new AdRequest.Builder().addTestDevice("0E1F15F35CE93F7A77BDDB59F7FE29AF").build();
mInterstitialAd.loadAd(adRequest);


AndroidManifest.xml

<!-- Used to request banner and interstitial ads. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Required permissions for video ads. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

<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"
android:theme="@android:style/Theme.Translucent" />


app build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-ads:10.2.0'
compile 'com.android.support:multidex:1.0.1'


}
apply plugin: 'com.google.gms.google-services'





project  build.gradle

dependencies {
classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
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
}


原文地址:https://www.cnblogs.com/zzxap/p/6797452.html