Android studio 添加admob googgle play services
Posted 风语者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
}
以上是关于Android studio 添加admob googgle play services的主要内容,如果未能解决你的问题,请参考以下文章
将 Admob 添加到 Visual Studio 2015 cordova 项目?
为啥使用 Android Studio 在我的应用中集成 AdMob 不起作用?