将 Admob 集成到 Android 问题

Integrate Admob to Android Problem(将 Admob 集成到 Android 问题)
本文介绍了将 Admob 集成到 Android 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to integrate Admob to android, i end up with no success. The Document says Need to Provide DeviceID to get Ads for Real Devices.Could you please Help me about this. But I m getting ads in the Emulator by setting AdManager.TestEmulator.

解决方案

It's fairly straight forward to setup with AdMob, I'm using it on several applications. Once you define you application on the AdMob website/control panel you will see your unique ID for your application.

Then you just need to add the AdMob Jar to your project, assuming you're using eclipse easiest way is to create a libs folder in your project folder, copy the admob jar in there and from eclipse, right click it, and go to Build Path/Add to Build Path.

Then open your manifest file and add the following somewhere inside the tag

<meta-data android:value="<YOUR APPLICATION ID FROM ADMOB CONTROL PANEL>" android:name="ADMOB_PUBLISHER_ID" />  

Next decide which activity you wish the ad's to appear, I usually place the ad right at the bottom of a LinearLayout so add the following..

<com.admob.android.ads.AdView     
           android:id="@+id/ad" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content"
           myapp:backgroundColor="#000000"
           myapp:primaryTextColor="#FFFFFF"
           myapp:secondaryTextColor="#CCCCCC"
  />

At the top of your layout definition where you define your xml namespace you will see

xmlns:android="http://schemas.android.com/apk/res/android"

also add a reference to the admob namespace so you will have :

xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:myapp="http://schemas.android.com/apk/res/net.dbws.fv" **<-- change package (net.dbws.fv) to your package**

Finally create a file named attrs.xml in the values folder and insert the following :

<?xml version="1.0" encoding="utf-8"?>
     <resources>
        <declare-styleable name="com.admob.android.ads.AdView">            
           <attr name="backgroundColor" format="color" />
           <attr name="primaryTextColor" format="color" />
           <attr name="secondaryTextColor" format="color" />
           <attr name="keywords" format="string" />
           <attr name="refreshInterval" format="integer" />
        </declare-styleable>
     </resources>

Then you should be good to go, I certainly haven't needed to do anything different for real devices as opposed to the emulator, the above should work for you. You don't always see ad's especially the first few times you run the app but if you are seeing ADMOB entries in your logcat output when you run your app then you can be confident it's working.

Regards

这篇关于将 Admob 集成到 Android 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)