我正在尝试将广告添加到我当前的活动中,但该活动未显示任何广告。我还为 Android Manifest.xml
添加了互联网权限这是我的
.Gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "sms.and.call.alert.flashlight.lpa.com.myapplication"
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.google.android.gms:play-services:9.6.0'
compile 'com.google.firebase:firebase-ads:9.6.0'
}
这是 MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
// An example device ID
.build();
mAdView.loadAd(request);
}
}
这是 MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sms.and.call.alert.flashlight.lpa.com.myapplication.MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
答案 0 :(得分:6)
2016年10月10日更新 - MM / dd / yyyy
第1步:请务必使用最新版本的Google Play服务和Google信息库
第2步:在Firebase控制台上创建项目后,将google-services.json放入app文件夹中 更多信息:https://firebase.google.com/docs/android/setup#add_firebase_to_your_app
第3步:设置项目级build.gradle,如下所示。
更多信息:https://firebase.google.com/docs/android/setup#add_the_sdk
buildscript {
// ...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
}
第4步:按以下步骤设置您的应用级build.gradle:
更多信息:https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "your.package.app"
minSdkVersion 9
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')
compile 'com.google.firebase:firebase-ads:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
第5步:将这些权限添加到AndroidManifest(如果它们尚未出现)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
此外,请确保AndroidManifest中的包名与app build.gradle的applicationId匹配,否则会出现编译错误。
第6步:继续关注快速入门教程,了解如何创建广告单元以在您的观看次数上展示广告:https://firebase.google.com/docs/admob/android/quick-start
第7步:如果您仍然遇到编译时问题,请检查那些非常有用且常见的线程和示例:
Manifest和applicationId问题:https://stackoverflow.com/a/37177173/2910520,
多种口味问题:
https://stackoverflow.com/a/34990999/2910520
Google Admob示例:https://github.com/googleads/googleads-mobile-android-examples
答案 1 :(得分:-1)
也许您忘了通过致电加载广告:
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(request);