如何在 Android 应用中实现 Admob AdView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3604093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement Admob AdView in Android app
提问by WeNeigh
I want to use Admob in my app. I've downloaded the SDK and followed the steps. Sometimes, I get an ad in return, but most of the time, I get an entry in LogCat that says "Server did not find any ads" or something to that effect. Test mode is enabled, says the Admob site. I think I might be doing something wrong. Where can I get a step-by-step guide to insert admob ads in Android apps? The Admob developer site is rather lacking.
我想在我的应用中使用 Admob。我已经下载了 SDK 并按照步骤操作。有时,我会收到一个广告作为回报,但大多数时候,我会在 LogCat 中收到一个条目,上面写着“服务器没有找到任何广告”或类似的内容。Admob 网站说,测试模式已启用。我想我可能做错了什么。在哪里可以获得在 Android 应用中插入 admob 广告的分步指南?Admob 开发者网站相当缺乏。
Also, let's assume that everything's gone well and that I'd now like to deploy the app. How do I turn off test mode for Admob ads?
另外,假设一切顺利,我现在想部署应用程序。如何关闭 Admob 广告的测试模式?
Thank you.
谢谢你。
采纳答案by Mark
just follow the instructions on this site: http://developer.admob.com/wiki/Android#AdMob_Android_SDK
只需按照本网站上的说明进行操作:http: //developer.admob.com/wiki/Android#AdMob_Android_SDK
I guess you did not activate the test mode for your device or the emulator?!
我猜您没有为您的设备或模拟器激活测试模式?!
AdManager.setTestDevices( new String[] {
AdManager.TEST_EMULATOR, // Android emulator
"E83D20734F72FB3108F104ABC0FFC738", // My T-Mobile G1 Test Phone
} );
回答by LuizAurio
Download the AdMob jar file http://www.admob.com/my_sites/
下载 AdMob jar 文件http://www.admob.com/my_sites/
Create a package on your project and call it "libs" and paste this file AdMob.jar there
在您的项目上创建一个包并将其命名为“libs”并将此文件 AdMob.jar 粘贴到那里
Right click on your project a select the library, add there the path for the ADMOB.jar you just saved.
右键单击您的项目并选择库,在那里添加您刚保存的 ADMOB.jar 的路径。
If you're creating your AdView on your XML, you can add this line.
如果您在 XML 上创建 AdView,则可以添加此行。
This is an example for testing. When you get your own ID from ADMob, place it on the adUnitID and erase the test line.
这是一个测试示例。当您从 ADMob 获得自己的 ID 时,将其放在 adUnitID 上并擦除测试行。
com.google.ads.AdView
android:id="@+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="a14f59e5c442767"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"
</com.google.ads.AdView>
Now go to your .java that it calling this layout and create your AdView
现在转到调用此布局的 .java 并创建您的 AdView
AdView adView = (AdView)this.findViewById(R.id.adView1);
adView.loadAd(new AdRequest());
This is how I do and its been working good so far.
这就是我所做的,到目前为止它一直运作良好。
Sorry about bad english, to much code and no sleep!
抱歉英语不好,代码太多,没有睡觉!
回答by Caltor
It seems as though this might have changed to
似乎这可能已更改为
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
request.addTestDevice("E83D20734F72FB3108F104ABC0FFC738"); // My T-Mobile G1 test phone
see http://code.google.com/mobile/ads/docs/android/intermediate.html
请参阅http://code.google.com/mobile/ads/docs/android/intermediate.html
As per Tom's comment below the value to provide for addTestDevice is actually the MD5 hash of the device ID. YOu can get this from the logcat.
根据 Tom 下面的评论,为 addTestDevice 提供的值实际上是设备 ID 的 MD5 哈希值。你可以从 logcat 得到这个。
回答by Narasimha
Just add a permission to the Android mainfest:
只需向 Android mainfest 添加一个权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Check it once.
检查一次。
Then add this code:
然后添加以下代码:
AdManager.setTestDevices( new String[] {
AdManager.TEST_EMULATOR, // Android emulator
"E83D20734F72FB3108F104ABC0FFC738", // My T-Mobile G1 Test Phone
} );
回答by Paulo Miguel Almeida
You don't need to call it programmatically.
您不需要以编程方式调用它。
It took me a while until I get what device is AdMob's sdk what expecting cuz' I was thinking it was something related to the real device like ( adb devices )
我花了一段时间才知道 AdMob 的 sdk 是什么设备 期待什么 因为我认为它与真实设备有关,例如( adb devices )
But here is a comment from the official documentation that cleared it up.
但这里是官方文档中的一条评论,澄清了这一点。
There will be a log message with the code needed to add the current device to the list of test devices
将有一条日志消息,其中包含将当前设备添加到测试设备列表所需的代码
You may get an message similar to it
您可能会收到类似的消息
I/Ads(26674): To get test ads on this device, call adRequest.addTestDevice("F1254CDFBA84BDC27F5C7C6E12445D06");
All you have to do after that is to place this ID into your layout xml as below
之后您要做的就是将此 ID 放入您的布局 xml 中,如下所示
<com.google.ads.AdView
android:layout_alignParentBottom="true"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/publisherId"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, F1254CDFBA84BDC27F5C7C6E12445D06" />
Hope it helps you guys out
希望能帮到大家
Paulo Miguel Almeida
保罗·米格尔·阿尔梅达