Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AdMob plugin that works in Unity 3.2

Discussion in 'Android' started by Wozik, Feb 11, 2011.

  1. Wozik

    Wozik

    Joined:
    Apr 10, 2009
    Posts:
    662
    Here is the basic AdMob plugin implementation that works with Unity 3.2

    Please give it a try and share your experience.
     

    Attached Files:

  2. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    Cool. I'll be trying this tonight along with the 3.2 OpenFeint Plugin.
     
  3. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    I imported the package and ran on my droid and got a force close. I'm not seeing anything relevant in the logs so I'm a bit lost. I'm also not sure how to implement this from the get go. I looked through all the included files and didn't see anything that points to a specific admob account. Any direction is greatly appreciated.

    Edit: I solved my issue and got this working, see my last post below.
     
    Last edited: Feb 16, 2011
  4. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    OK I attached the debugger and got the stack trace:

    E/AndroidRuntime(17925): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.oyo.FanBoyLite/com.oyo.FanBoyLite.AdMobTestActivity}: java.lang.ClassNotFoundException: com.oyo.FanBoyLite.AdMobTestActivity in loader dalvik.system.PathClassLoader[/data/app/com.oyo.FanBoyLite-1.apk]
    E/AndroidRuntime(17925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    E/AndroidRuntime(17925): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    E/AndroidRuntime(17925): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    E/AndroidRuntime(17925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    E/AndroidRuntime(17925): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(17925): at android.os.Looper.loop(Looper.java:123)
    E/AndroidRuntime(17925): at android.app.ActivityThread.main(ActivityThread.java:4627)
    E/AndroidRuntime(17925): at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(17925): at java.lang.reflect.Method.invoke(Method.java:521)
    E/AndroidRuntime(17925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    E/AndroidRuntime(17925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    E/AndroidRuntime(17925): at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime(17925): Caused by: java.lang.ClassNotFoundException: com.oyo.FanBoyLite.AdMobTestActivity in loader dalvik.system.PathClassLoader[/data/app/com.oyo.FanBoyLite-1.apk]
    E/AndroidRuntime(17925): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    E/AndroidRuntime(17925): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    E/AndroidRuntime(17925): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    E/AndroidRuntime(17925): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    E/AndroidRuntime(17925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    E/AndroidRuntime(17925): ... 11 more


    I know that class is in the plugin so I'm not sure why it can't find it.
     
  5. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    The example package works if run in its own project. I believe the problem is that my package name is com.oyo.FanBoyLite, where the example plugin uses org.example.AdMobTest . I am currently researching how to change the package name and rebuild. I'll report my results here.
     
  6. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    I tried importing the plugin into eclipse and renaming the package. Still getting the same error.
     
  7. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    Just tried adding the classes.jar and admobsdk.jar to my build path in eclipse and cleaned my project. STILL getting the classnotfound exception.
     
  8. Wozik

    Wozik

    Joined:
    Apr 10, 2009
    Posts:
    662
    did you try changing package org.example.AdMobTest; at AdMobTestActivity.java and package="org.example.AdMobTest" at AndroidManifest.xml to com.oyo.FanBoyLite?
     
  9. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    Yes. My initial problem was in the manifest file. Now I have copied my projects manifest file and attempted to add the admob activity to it:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Company.ProductName" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="6" />
    <supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:anyDensity="true"/>

    <application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name">

    <activity android:name="com.unity3d.player.UnityPlayerActivity"
    android:label="@string/app_name"
    android:configChanges="keyboardHidden|orientation">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <activity android:name=".AdMobTestActivity"
    android:label="admob">
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.NoTitleBar"
    android:screenOrientation="landscape"/>
    </activity>

    <!-- The application's publisher ID assigned by AdMob -->
    <meta-data android:value="YOUR_ID_HERE" android:name="ADMOB_PUBLISHER_ID" />


    <!-- AdMobActivity definition -->
    <activity android:name="com.admob.android.ads.AdMobActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />

    <!-- Track Market installs -->
    <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
    <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
    </receiver>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    </manifest>


    The game runs without crashing now. But I don't see anything indicating that the admob activity is running. There are no ads and nothing in logcat that indicates that it is running. Obviously I did replace the publisher ID with my publisher ID.
     
  10. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    OK, after changing all occurrences of the package name to my package name, I was still getting the class not found exception...BUT...at some point I had changed the package name in Player Settings to try test some things out. When I changed it back I changed it to com.oyo.FanBoyLIte...note the capital "i". Changing this to com.oyo.FanBoyLite fixed! I've never been so excited to see an ad in my life haha. I'll post my final manifest file below just for clarification.

    THANKS!

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.oyo.FanBoyLite" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="6" />
    <supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:anyDensity="true"/>

    <application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name">

    <activity android:name=".AdMobTestActivity"
    android:label="@string/app_name"
    android:configChanges="keyboardHidden|orientation">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <!-- The application's publisher ID assigned by AdMob -->
    <meta-data android:value="put your ID here" android:name="ADMOB_PUBLISHER_ID" />


    <!-- AdMobActivity definition -->
    <activity android:name="com.admob.android.ads.AdMobActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />

    <!-- Track Market installs -->
    <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
    <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
    </receiver>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    </manifest>
     
  11. Curious

    Curious

    Joined:
    Nov 19, 2009
    Posts:
    334
    Has anyone integrated this with OpenFeint at the same time?
     
  12. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    I know its possible. I think you have to combine this with the OpenFeintFacade example. In the OpenFeintFacade example, activities are called from C#, so theoretically you could extend the main activity for admob, then call openfeint activities using the Facade.

    Look at my manifest.xml in the post above, I'm already extending the main activity to use the admobtest activity, so you could paste the contents of the openfeintfacade manifest below that and it should work. Hopefully I'll be able to try it out tonight and I'll let you know how it goes.
     
  13. Curious

    Curious

    Joined:
    Nov 19, 2009
    Posts:
    334
    That would be great, thank you :)
     
  14. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    BTW add this line to your admobtestactivity.java in org/example/admobtest to place your ads at the bottom of the screen:

    layout.setGravity(android.view.Gravity.BOTTOM);

    you'll need to cd to your Plugins/Android directory and run "ant create-jar" to rebuild the plugin. If the build is successful you can hit build and run in unity and it should run on your device.
     
  15. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    OK It worked.
    First copy your project directory in case something bad happens (I had an unrecoverable crash when trying to import assets)

    next open your project with admob installed.
    drag/drop all the files from the openfeintfacade EXCEPT for androidmanifest.xml into your projects plugin dir
    copy the OF activities into your existing androidmanifest.xml
    Build>Run.

    I logged into OF successfully and Admob was called, though the ad failed. But that means that the class got called so thats really all that matters.

    Heres what your android manifest.xml should look like:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.oyo.FanBoyLite" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="6" />
    <supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:anyDensity="true"/>

    <application
    android:icon="@drawable/icon"
    android:label="@string/app_name">

    <activity android:name=".AdMobTestActivity"
    android:label="@string/app_name"
    android:configChanges="keyboardHidden|orientation"
    android:gravity="bottom">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <!-- The application's publisher ID assigned by AdMob -->
    <meta-data android:value="a14d56bcab13292" android:name="ADMOB_PUBLISHER_ID" />


    <!-- AdMobActivity definition -->
    <activity android:name="com.admob.android.ads.AdMobActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />

    <!-- Track Market installs -->
    <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
    <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
    </receiver>

    <activity android:name="com.openfeint.internal.ui.IntroFlow"
    android:label="IntroFlow"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.NoTitleBar"
    android:screenOrientation="portrait"/>

    <activity android:name="com.openfeint.api.ui.Dashboard"
    android:label="Dashboard"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.NoTitleBar"
    android:screenOrientation="portrait"/>

    <activity android:name="com.openfeint.internal.ui.Settings"
    android:label="Settings"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.NoTitleBar"
    android:screenOrientation="portrait"/>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    </manifest>
     
  16. Curious

    Curious

    Joined:
    Nov 19, 2009
    Posts:
    334
    Wow, thank you very much buddy, you just made my day ;)

    Regards
     
  17. christo

    christo

    Joined:
    Nov 23, 2010
    Posts:
    15
    One note here:

    I just tried to change my admobtestactivity.jar and ran ant create-jar. When I tried to build and run i got an error saying "building dex failed". I couldn't figure it out so what I did was go back to my project that just had admob (no openfeint) and made the change there and ran ant create-jar, then integrated openfeint again. This worked perfectly.

    I realize that this isn't an ideal fix if you are going to change your admobtestactivity.jar a lot, but I don't plan to.
     
  18. kmoll092

    kmoll092

    Joined:
    Nov 26, 2010
    Posts:
    1
    I am going through all the steps I found here. I changed the package in the AdMobTestActivity.java file and I changed the package in AndroidManifest.xml,

    Do I have to change the jar file to read my-package.jar instead of org.example.AdMobTest.jar, I changed that to be my-package.jar and I changed the structure of the filders in the bin/classes to com/my-company/my-productName

    and also change the folder structure in src to com/my-company/my-productName so the AdMobTestActivity.java file is in that structure.

    I can't get it to work, my device always force closes, and I check the log cat and its still giving me that ClassNotFoundException in the logcat.

    Anything else I can check? Or is there any other place I have to change names of folders, files, or instances in files??
     
  19. kalos

    kalos

    Joined:
    Dec 11, 2010
    Posts:
    121
    @Christo : Can you make a video tutorial? :) I think many of us may need that :)

    Thanks
     
  20. Argenex

    Argenex

    Joined:
    Jan 7, 2011
    Posts:
    34
    Edit #3
    Ok.
    Reason the package by itself doesnt display any ads is it looks like the manifest needs this line:
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    Tyvm christo.

    What I've done to get it working in my own project, is first add it to my project.
    Let it build and crash on the android.
    Went to my project's temp folder, copied out the Staging folder.
    Moved this to another area completely.
    Made a new project in Eclipse based on the Relocated Staging folder.
    Fixed all of the org.exmaple.AdMobTest calls.
    Fixed the references to Jar library files (Was pointing to an utterly different folder for Unity than what I have on x64 windows)
    >C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin
    Made sure to include the admob-sdk-android.jar file as a library.
    NOTE:
    This looks to be an older admob library, which I am guessing is where the edits were done Wozik, as the AdMobTestActivity class is in there along with a bunch of other things, that is not in the current admob library, GoogleAdMobAdsSDK-4.0.2.jar
    So good thing that was put together.

    There were a few minor fixes here and there with things to get it to generate AdMobTestActivity correctly. But its done and working now.

    I will now be attempting to convert this back to plug-in status for my project.

    Thanks.

    Edit #4
    Well, Day 3. I dont even know what I did, I literally changed nothing, even restorted the AdMobTestActivity project in Eclipse that was working. Now its Force Closing again and its nearly impossible to figure out why.

    Ive tried LogCat.
    Ive tried debugging mode with Eclipse, which only makes a mess of things and doesnt tell me what is going on.

    I am dumbfounded that it is this complex to get ads to work, let alone incorporate an external java library with Unity. It appears to be extremely sensitive and if a butterfly flutters in the wrong direction, everything goes nuts and explodes.

    Edit #5
    Haha Oops. Eclipse no likey if you replace files outside of eclipse or rename files.
     
    Last edited: Mar 24, 2011
  21. deepak

    deepak

    Joined:
    Aug 10, 2010
    Posts:
    44
    Hi Argenex,

    I am trying to integrate Admob for my unity android application. Please can you brief me how to integrate Admob with my application.

    Thanks in advance
     
  22. vmenge

    vmenge

    Joined:
    Apr 8, 2011
    Posts:
    3
    I'm sorry, but could we have some sort of retard-friendly instructions?
    I've tried changing package org.example.AdMobTest; at AdMobTestActivity.java and package="org.example.AdMobTest" at AndroidManifest.xml to no luck, and then went out to change org.example.AdMobTest that were in other places too, still to no avail.
    I would really appreaciate some sort of help on this, thanks :)
     
  23. andreasR

    andreasR

    Joined:
    Feb 4, 2010
    Posts:
    28
    It would be great if someone makes a little video about implementing admob into unity. this would also help the unity iphone developers.
     
  24. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    I finally got it working, this is how it is done on a Mac but Windows Version should work similar:

    Download the unity admob package and import it to your project. For the following steps let's say the project folder is "\syslord2k2\Unity\myproject" (%myproject%) while the android sdk folder is "\syslord2k2\android-sdk" (%androidsdk%).

    In your project click on File > Build Settings, choose Android, click Player Settings and find your bundle identifier, for example "de.myproject.mygame".

    Find AdMobTestActivity.java in your project folder under "Assets\Plugins\Android\src\org\example\AdMobTest" and open it with a text editor.

    Update the package name in Line 1 with your bundle identifier, for example change "package org.example.AdmobTest" to "package de.myproject.mygame".

    Uncomment lines 72 and 73 and change the values to your keywords and your desired interval.

    Save the file and rename the folder structure according to your bundle identifier, for example change "org" to "de", "example" to "myproject" and "AdMobTest" to "mygame" (Don't know if this step is necessary but it looks better :D).

    Open AndroidManifest.xml in "%myproject%/Assets/Plugins/Android".

    In line 3 change "package = "org.example.AdMobTest" according to your bundle identifier, for example to "package = 'de.myproject.mygame";

    In line 19 change the string "YOUR_ID_HERE" to your publisher id, obtained through the admob online platform.

    Save and exit AndroidManifest.xml.

    Open up a terminal and type "cd %androidsdk%\tools" so in my case "cd \syslord2k2\android-sdk\tools" to change to the android-sdk\tools folder.

    Type "./android update project -t 2 -p %myproject%\Assets\Plugins\Android" so in my case "./android update project -t 2 -p \syslord2k2\Unity\myproject\Assets\Plugins\Android"
    (Note: the -t paramete defines the target you are building for, you can see a list of available targets by typing "./android list targets")

    Change to your project folder by typing "cd %myproject%\Assets\Plugins\Android" so in my case "cd \syslord2k2\Unity\myprojec\Assets\Plugins\Android" and type "ant create-jar".

    In the finder delete the "%myproject%\Assets\Plugins\Android\bin\org.example.AdMobTest.jar" and the "org" folder in the "%myproject%\Assets\Plugins\Android\bin" folder. (Of course only do this if your bundle identifier does not start with "org" :D. This step is pure cosmetics and to reduce filesize, it does not add or substract functionality.)
     
    Last edited: Apr 12, 2011
  25. vmenge

    vmenge

    Joined:
    Apr 8, 2011
    Posts:
    3
    Got it to work, thanks :D
    Now to find out how to change position... hmmm
     
  26. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    I followed syslord2k2's post and am getting a Force Close event with every launch.

    I'm trying to run it with OpenFeint. Has anyone else run into this problem?
     
  27. gamestudio

    gamestudio

    Joined:
    Mar 10, 2010
    Posts:
    27
    Hi, Thank you syslord2k2.

    It work for me. If you can not see ad on game, Please Check ::.

    - set test mode on Admob website
    - Add new 1 house ad.

    Code (csharp):
    1. package de.myproject.mygame;
    2.  
    3. import android.os.Bundle;
    4. import android.util.Log;
    5. import android.view.View;
    6. import android.view.ViewGroup.LayoutParams;
    7. import android.widget.LinearLayout;
    8. import android.widget.RelativeLayout;
    9. import android.view.animation.*;
    10.  
    11. import com.unity3d.player.*;
    12.  
    13. import com.admob.android.ads.AdView;
    14. import com.admob.android.ads.SimpleAdListener;
    15. import com.admob.android.ads.AdManager;
    16.  
    17. public class AdMobTestActivity extends UnityPlayerActivity
    18. {
    19.     private AdView adView;
    20.  
    21.     protected void onCreate(Bundle savedInstanceState)
    22.     {
    23.         Log.i("AdMobTest", "onCreate");
    24.  
    25.         // super (UnityPlayerActivity) will use setContentView() ...
    26.         super.onCreate(savedInstanceState);
    27.  
    28.         // ... so instead of using setContentView(), we call addContentView() from setupAds()
    29.         setupAds();
    30.  
    31.         // Add test devices (check the logcat for the ID string of your device..)
    32.         //AdManager.setTesting(true);
    33.         //AdManager.setTestDevices( new String[] { "80A354043041544216" } );
    34.         AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR });
    35.     }
    36.  
    37.     private void HideAd()
    38.     {
    39.         // Hide the ad.
    40.         adView.setVisibility(View.GONE);
    41.  
    42.         // Fade the ad in over 4/10 of a second.
    43.         AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
    44.         animation.setDuration(400);
    45.         animation.setFillAfter(true);
    46.         animation.setInterpolator(new AccelerateInterpolator());
    47.         adView.startAnimation(animation);//*/
    48.  
    49.     }
    50.  
    51.     //private void RotateAd()
    52.     //{
    53.     //    // Hide the ad.
    54.     //    adView.setVisibility(View.VISIBLE);
    55.  
    56.     //    // Fade the ad in over 4/10 of a second.
    57.     //    RotateAnimation animation = new RotateAnimation(0, 90, 24, 24);
    58.     //    animation.setDuration(400);
    59.     //    animation.setFillAfter(true);
    60.     //    animation.setInterpolator(new AccelerateInterpolator());
    61.     //    adView.startAnimation(animation);//*/
    62.  
    63.     //}
    64.  
    65.     private void ShowAd()
    66.     {
    67.         // Unhide the ad.
    68.         adView.setVisibility(View.VISIBLE);
    69.  
    70.         // Fade the ad in over 4/10 of a second.
    71.         AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
    72.         animation.setDuration(400);
    73.         animation.setFillAfter(true);
    74.         animation.setInterpolator(new AccelerateInterpolator());
    75.         adView.startAnimation(animation);//*/
    76.     }
    77.  
    78.  
    79.     private void setupAds()
    80.     {
    81.         // And this is the same, but done programmatically
    82.  
    83.             //LinearLayout layout = new LinearLayout(this);
    84.             ////layout.setOrientation(LinearLayout.VERTICAL);
    85.             ////layout.setGravity(Gravity.BOTTOM);
    86.             //layout.setOrientation(LinearLayout.VERTICAL);
    87.             //addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    88.  
    89.             LinearLayout main = new LinearLayout(this);
    90.             main.setOrientation(LinearLayout.HORIZONTAL);
    91.             main.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    92.  
    93.             //create horizontal section
    94.             RelativeLayout.LayoutParams fpfp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    95.             LinearLayout horizontalLayout = new LinearLayout(this);
    96.             horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
    97.             horizontalLayout.setLayoutParams(fpfp);
    98.  
    99.             //RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    100.             //adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    101.             //adView = new AdView(this);
    102.             ////layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    103.             //layout.addView(adView, adParams);
    104.  
    105.             adView = new AdView(this);
    106.             horizontalLayout.addView(adView);
    107.  
    108.             adView.setVisibility(View.VISIBLE);
    109.             adView.setBackgroundColor(0xff000000);
    110.             adView.setPrimaryTextColor(0xffffffff);
    111.             adView.setSecondaryTextColor(0xffcccccc);
    112.             adView.setKeywords("Android game");
    113.             adView.setRequestInterval(15);
    114.  
    115.             main.addView(horizontalLayout);
    116.             //setContentView(main);
    117.             addContentView(main, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    118.            
    119.  
    120.         // add listener for easier debugging
    121.             adView.setAdListener( new SimpleAdListener()
    122.             {
    123.                 public void onFailedToReceiveAd(com.admob.android.ads.AdView adView)
    124.                 {
    125.                     Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    126.                     super.onFailedToReceiveAd(adView);
    127.                 }
    128.  
    129.                 public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    130.                 {
    131.                     Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    132.                     super.onFailedToReceiveRefreshedAd(adView);
    133.                 }
    134.  
    135.                 public void onReceiveAd(com.admob.android.ads.AdView adView)
    136.                 {
    137.                     Log.d("AdListener", "onReceiveAd: " + adView.toString());
    138.                     super.onReceiveAd(adView);
    139.                 }
    140.  
    141.                 public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    142.                 {
    143.                     Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    144.                     super.onReceiveRefreshedAd(adView);
    145.                 }
    146.             } );
    147.  
    148.             adView.requestFreshAd();
    149.     }
    150. }
    Now i try to rotate Admob view, But not success yet.
     
    Last edited: Apr 13, 2011
  28. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    Does the log show you an error message?
     
  29. Celulonio

    Celulonio

    Joined:
    Apr 14, 2011
    Posts:
    20
    Hi!

    I'm close. The server is receiving requests but I can't see banners on my droid...

    Do I need to make any calls from unity? I've never worked with plugins. Can anyone help me?
     
  30. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    Hi DigitalJoker,

    did you edit your AndroidManifestxml according to your settings?
     
  31. Celulonio

    Celulonio

    Joined:
    Apr 14, 2011
    Posts:
    20
    This is my AndroidManifest.xml:

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.digitaljokers.admobtest"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <uses-sdk android:minSdkVersion="6" />
    7.     <application
    8.         android:icon="@drawable/app_icon"
    9.         android:label="@string/app_name">
    10.         <activity android:name=".AdMobTestActivity"
    11.             android:label="@string/app_name">
    12.             <intent-filter>
    13.                 <action android:name="android.intent.action.MAIN" />
    14.                 <category android:name="android.intent.category.LAUNCHER" />
    15.             </intent-filter>
    16.         </activity>
    17.  
    18.         <!-- The application's publisher ID assigned by AdMob -->
    19.        <meta-data android:value="a14da439f06d14e" android:name="ADMOB_PUBLISHER_ID" />
    20.  
    21.        <!-- AdMobActivity definition -->
    22.        <activity android:name="com.admob.android.ads.AdMobActivity"
    23.                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    24.                  android:configChanges="orientation|keyboard|keyboardHidden" />
    25.  
    26.        <!-- Track Market installs -->
    27.        <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
    28.            <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
    29.            </intent-filter>
    30.        </receiver>
    31.  
    32.    </application>
    33.  
    34.    <!-- AdMob SDK requires Internet permission -->
    35.    <uses-permission android:name="android.permission.INTERNET" />
    36.  
    37. </manifest>
    Anything wrong?

    It seems that everything is ok, my server is receiving requests but I can't see the banner on my device.

    One more question. The banner can be displayed only on my device but not on unity editor, right?

    Thanks!!!!

    Apologize for my English. :eek:
     
  32. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    thats seems alright, could you post your AdMobTestActivity.java?

    Also try to activate test mode in your admob accound, the logcat should also tell you if you received an ad and what kind of app its is eg youtube secret talents
     
    Last edited: Apr 20, 2011
  33. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    syslord2k2,

    Thanks for the run down on OSX, do you know how to get this to call only when wanted using native activity? It would be great to be able to decide when to show ads...

    Regards,

    mmuller
     
  34. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    Thanks to DTreble in this Thread, i can provide you with a solution to this. Here is the changed AdMobTestActivity:

    Code (csharp):
    1.  
    2. package de.badmonkee.soccerpinball; //this has to be changed
    3.  
    4. import android.os.Bundle;
    5. import android.os.Handler;
    6. import android.util.Log;
    7. import android.view.View;
    8. import android.view.ViewGroup.LayoutParams;
    9. import android.widget.LinearLayout;
    10. import android.os.Message;
    11.  
    12. import com.unity3d.player.*;
    13.  
    14. import com.admob.android.ads.AdView;
    15. import com.admob.android.ads.SimpleAdListener;
    16. import com.admob.android.ads.AdManager;
    17.  
    18. public class AdMobTestActivity extends UnityPlayerActivity
    19. {
    20.     public AdView adView;
    21.     private boolean adVisible = true;
    22.     protected void onCreate(Bundle savedInstanceState)
    23.     {
    24.         Log.i("AdMob", "onCreate");
    25.  
    26.         // super (UnityPlayerActivity) will use setContentView() ...
    27.         super.onCreate(savedInstanceState);
    28.  
    29.         // ... so instead of using setContentView(), we call addContentView() from setupAds()
    30.         setupAds();
    31.  
    32.         // Add test devices (check the logcat for the ID string of your device..)
    33.         AdManager.setTestDevices( new String[] { "3234ac49152000ec" } ); //this has to be changed
    34.     }
    35.     private Handler handler = new Handler()
    36.     {
    37.         public void  handleMessage(Message msg)
    38.         {
    39.             switch (msg.what)
    40.             {
    41.                 case 0:
    42.                     if (adVisible)
    43.                     {
    44.                         adView.setVisibility(View.INVISIBLE);
    45.                         adVisible = false;
    46.                     }
    47.                     break;
    48.                 case 1:
    49.                     if (!adVisible)
    50.                     {
    51.                         adView.setVisibility(View.VISIBLE );
    52.                         adVisible = true;
    53.                     }
    54.                     break;
    55.                 default:
    56.                     break;
    57.             }
    58.         }
    59.     };
    60.  
    61.     public void EnableAds()
    62.     {
    63.         handler.sendEmptyMessage(1);
    64.         //adView.setVisibility(View.VISIBLE );
    65.     }
    66.  
    67.     public void DisableAds()
    68.     {
    69.         handler.sendEmptyMessage(0);
    70.         //adView.setVisibility(View.INVISIBLE );
    71.     }  
    72.     private void setupAds()
    73.     {
    74.  
    75.             LinearLayout layout = new LinearLayout(this);
    76.         layout.setOrientation(LinearLayout.VERTICAL);
    77.             addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    78.  
    79.             adView = new AdView(this);
    80.         layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    81.  
    82.             adView.setBackgroundColor(0xff000000);
    83.             adView.setPrimaryTextColor(0xffffffff);
    84.             adView.setSecondaryTextColor(0xffcccccc);
    85.             adView.setKeywords("kickoff Goal Pinball game spiel bundesliga male Flipper Bumper Parlotones kick off 3d soccer fussball football tor vuvuzela");
    86.             adView.setRequestInterval(20);
    87.  
    88.         // add listener for easier debugging
    89.             adView.setAdListener( new SimpleAdListener()
    90.             {
    91.                 public void onFailedToReceiveAd(com.admob.android.ads.AdView adView)
    92.                 {
    93.                     Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    94.                     super.onFailedToReceiveAd(adView);
    95.                 }
    96.  
    97.                 public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    98.                 {
    99.                     Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    100.                     super.onFailedToReceiveRefreshedAd(adView);
    101.                 }
    102.  
    103.                 public void onReceiveAd(com.admob.android.ads.AdView adView)
    104.                 {
    105.                     Log.d("AdListener", "onReceiveAd: " + adView.toString());
    106.                     super.onReceiveAd(adView);
    107.                 }
    108.  
    109.                 public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    110.                 {
    111.                     Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    112.                     super.onReceiveRefreshedAd(adView);
    113.                 }
    114.             } );
    115.             adView.requestFreshAd();
    116.     }
    117. }
    And here are the functions you have to define in c#:

    Code (csharp):
    1.  
    2.     public void EnableAds()
    3.     {
    4. #if UNITY_ANDROID
    5.         AndroidJNI.AttachCurrentThread();
    6.  
    7.         // first we try to find our main activity..
    8.         IntPtr cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
    9.         IntPtr fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    10.         IntPtr obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity);
    11.         //Debug.Log("obj_Activity = " + obj_Activity);
    12.  
    13.         IntPtr cls_OurAppNameActivityClass = AndroidJNI.FindClass("de/badmonkee/soccerpinball/AdMobTestActivity"); //this has to be changed
    14.  
    15.         IntPtr startAdsMethod = AndroidJNI.GetMethodID(cls_OurAppNameActivityClass, "EnableAds", "()V");
    16.         //Debug.Log("m_startAdsMethod = " + startAdsMethod);
    17.  
    18.         if (AndroidJNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != false)
    19.         {
    20.             //Debug.Log("Activity IS a OurAppNameActivity");
    21.             jvalue[] myArray = new jvalue[1];
    22.             AndroidJNI.CallVoidMethod(obj_Activity, startAdsMethod,myArray);
    23.         }
    24. #else
    25.         m_adShowing = true;
    26. #endif //UNITY_ANDROID
    27.     }
    28.  
    29.    
    30.     public void DisableAds()
    31.     {
    32. #if UNITY_ANDROID
    33.         AndroidJNI.AttachCurrentThread();
    34.  
    35.         // first we try to find our main activity..
    36.         IntPtr cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
    37.         IntPtr fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    38.         IntPtr obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity);
    39.  
    40.         IntPtr cls_OurAppNameActivityClass = AndroidJNI.FindClass("de/badmonkee/soccerpinball/AdMobTestActivity"); //this has to be changed
    41.         IntPtr stopAdsMethod = AndroidJNI.GetMethodID(cls_OurAppNameActivityClass, "DisableAds", "()V");
    42.  
    43.         if (AndroidJNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != false)
    44.         {
    45.             jvalue[] myArray = new jvalue[1];
    46.             AndroidJNI.CallVoidMethod(obj_Activity, stopAdsMethod,myArray);
    47.         }
    48. #else //UNITY_ANDROID
    49.         m_adShowing = false;
    50. #endif //UNITY_ANDROID
    51.  
    52.     }
    53.  
    I commented all things that have to be changed to get it to work.

    Please have in mind that the starting setting would be the ads to be enabled, so you would to manually have to disable the ads at start if desired.

    I recognized a bug that you can not touch to places where the ad would be even if it is hidden. This seems to be fixed in a later version of the android-admob sdk, however the latest version does not work due to recent changes in the way of calling the ads. If someone has the sdk version where this problem is fixed, feel free to post it :).
     
    Last edited: Apr 21, 2011
  35. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    syslord2k2,

    That is magic ! And a big thanks to DTreble for sharing the original code with us all :)

    I look forward to implementing this in my game tonight.

    Regards,

    mmuller
     
  36. nekokiller

    nekokiller

    Joined:
    Dec 9, 2010
    Posts:
    12

    After I type "ant create-jar"

    I got:

    BUILD FAILED
    Target "create-jar" does not exist in the project "AdMobTestActivity".


    my ant tool is apache-ant-1.8.2-bin.zip
     
    Last edited: Apr 22, 2011
  37. Celulonio

    Celulonio

    Joined:
    Apr 14, 2011
    Posts:
    20
    Sorry for not answering before. I've been on vacation ;)
    Test mode was activated. Logcat show this message:

    I/AdMobSDK(10636): No fill. Server replied that no ads are available (1697ms)
    D/AdListener(10636): onFailedToReceiveAd: com.admob.android.ads.AdView@4059e968


    This is my AdmobTestActivity.java:

    Code (csharp):
    1.  
    2. package com.digitaljokers.admobtest;
    3.  
    4. /* package */
    5. import android.os.Bundle;
    6. import android.util.Log;
    7. import android.view.View;
    8. import android.view.ViewGroup.LayoutParams;
    9. import android.widget.LinearLayout;
    10. /* */
    11.  
    12. import com.unity3d.player.*;
    13.  
    14. import com.admob.android.ads.AdView;
    15. import com.admob.android.ads.SimpleAdListener;
    16. import com.admob.android.ads.AdManager;
    17.  
    18. public class AdMobTestActivity extends UnityPlayerActivity
    19. {
    20.     /* foro */
    21.     //private AdView adView;
    22.  
    23.     protected void onCreate(Bundle savedInstanceState)
    24.     {
    25.         Log.i("AdMobTest", "onCreate");
    26.  
    27.         // super (UnityPlayerActivity) will use setContentView() ...
    28.         super.onCreate(savedInstanceState);
    29.  
    30.         // ... so instead of using setContentView(), we call addContentView() from setupAds()
    31.         setupAds();
    32.  
    33.  
    34.         // Add test devices (check the logcat for the ID string of your device..)
    35.         //AdManager.setTestDevices( new String[] { "ABCDEF1234578901234567890123456" } );
    36.         AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR });
    37.  
    38.     }
    39.    
    40. /*  package */
    41.     private void setupAds()
    42.     {
    43.         // And this is the same, but done programmatically
    44.  
    45.             LinearLayout layout = new LinearLayout(this);
    46.             layout.setOrientation(LinearLayout.HORIZONTAL);
    47.             //addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    48.  
    49.             AdView adView = new AdView(this);
    50.             layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    51.             adView.setBackgroundColor(0xff000000);
    52.             adView.setPrimaryTextColor(0xffffffff);
    53.             adView.setSecondaryTextColor(0xffcccccc);
    54. //          adView.setKeywords("Android game");
    55. //          adView.setRequestInterval(15);
    56.  
    57.         // add listener for easier debugging
    58.             adView.setAdListener( new SimpleAdListener()
    59.             {
    60.                 public void onFailedToReceiveAd(com.admob.android.ads.AdView adView)
    61.                 {
    62.                     Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    63.                     super.onFailedToReceiveAd(adView);
    64.                 }
    65.  
    66.                 public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    67.                 {
    68.                     Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    69.                     super.onFailedToReceiveRefreshedAd(adView);
    70.                 }
    71.  
    72.                 public void onReceiveAd(com.admob.android.ads.AdView adView)
    73.                 {
    74.                     Log.d("AdListener", "onReceiveAd: " + adView.toString());
    75.                     super.onReceiveAd(adView);
    76.                 }
    77.  
    78.                 public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    79.                 {
    80.                     Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    81.                     super.onReceiveRefreshedAd(adView);
    82.                 }
    83.             } );
    84.  
    85.             adView.requestFreshAd();
    86.     }
    87. /* */  
    88. }
    89.  
    90.  
     
  38. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    Shortly after release, we had to change the code to

    Code (csharp):
    1. private Handler handler = new Handler()
    2.     {
    3.          public void  handleMessage(Message msg)
    4.          {
    5.              switch (msg.what)
    6.              {
    7.              case 0:
    8.                  if (adVisible)
    9.                  {
    10.                      adView.setClickable(false);
    11.                      adView.setVisibility(View.INVISIBLE);
    12.                      adView.startAnimation( hideAnimation );
    13.                      adVisible = false;
    14.                  }
    15.                  break;
    16.              case 1:
    17.                  if (!adVisible)
    18.                  {
    19.                      adView.setClickable(true);
    20.                      adView.setVisibility(View.VISIBLE);
    21.                      adView.startAnimation( showAnimation );
    22.                      adVisible = true;
    23.                  }
    24.                  break;
    25.              default:
    26.                  break;
    27.              }
    28.          }
    29.     };
    The adView.setClickable fixes the bug.
     
  39. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    Hi DigitalJoker,

    this can happen from time to time when there is no ad available, you can increase your fillrate by activating google adsense in you admob account.

    @DJTreble: Thanks for the update
     
  40. Celulonio

    Celulonio

    Joined:
    Apr 14, 2011
    Posts:
    20
    It works! thanks, thanks! :)

    Now, I need to show an hide ads. Can i call showAd() and hideAd() (AdMobTestActiviy.java) from Unity script? or...
     
  41. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    Syslord2k2/DTreble,

    How difficult would it be to change the admob sdk to the adwhirl one? I think it would be great if android had a plugin along the lines of Prime31's adwhirl plugin for iOS ( Worth every cent of the $40 I paid for it ).
     
    Last edited: Apr 27, 2011
  42. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    If you integrated the JNI functions in your code then this should be possible, yes.

    I stopped working on adwhirl integration after admob announced that they now also support google adsense which increased our fillrate up to nearly 80% but couldn't image it to be a very complex process. We are talking more about hours of work than days here.
     
    Last edited: Apr 28, 2011
  43. Argenex

    Argenex

    Joined:
    Jan 7, 2011
    Posts:
    34
    I just wanted to respond to this post and apologize for not posting in a while, I lost my whole project and everything exploded, its taken this long to get everything back and rebuild. I will be crawling over everyone's attempts, I did manage to write down my whole process, but it was a long process and took alot of time. Simply put, it was no "plugin", it required many, many steps just to build a single instance with admob working with a unity project.

    thanks.
     
  44. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    Argenex,

    If you follow syslord2k2's step by step guide it took only four steps to have a working fully functional admob plugin in my game on the market. Which consisted of editing the files (2 of them) and then running the two terminal commands to rebuild the JAR file

    It really is quite simple and because of the need to alter the files - will never be a simple plugin and play solution (until some clever so and so works out how to script it to allow adding the com.whatever.game as a variable to an empty game object that then runs a script to rebuild your jar files and classes...)
     
  45. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    Also,

    I tried rebuilding with the latest Admob SDK and get force quits from the app now - has anyone had any success with the very latest Admb SDK pushed by Google yet?
     
  46. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    Hi mmuller,

    then new Admob SDK uses new package names for the views, in order to get it to run you would have to completely change the according calls.

    Best regards
     
  47. Kuneko

    Kuneko

    Joined:
    Mar 20, 2010
    Posts:
    8
    After following syslord2k2's guide and typing "ant create-jar", I get a build failed error (see attached image.) Everything up until this point has worked fine as far as I can tell. I haven't made any other changes to the files other than altering the package name to com.Kuneko.StuntPilot.

    What is going wrong?
     

    Attached Files:

  48. syslord2k2

    syslord2k2

    Joined:
    Apr 12, 2011
    Posts:
    23
    It seems that you are missing some libraries, please make sure that you have got unity and the android sdk installed on the machine you are compiling with and that they are properly included in your local.properties and the build.properties files.
     
  49. Kuneko

    Kuneko

    Joined:
    Mar 20, 2010
    Posts:
    8
    Thank you for the reply.

    Hmmm Unity and the android sdk are installed, I only use this computer for development/compiling etc. The two files you mentioned point to the right places but my /PlaybackEngines/AndroidPlayer/bin folder is empty, could this be the problem? If so, what files are meant to be in it?

    EDIT:

    Okay, found the two files that were missing from the folder. It now compiles correctly and builds my new .jar file. However, when I run the game on my phone it won't play. The Unity splash screen shows up and at the point it would normally show my title screen, it just exits the app. Any thoughts?

    EDIT #2

    Had a line of code to set the screen orientation on the title screen, not sure why it was there. Anyhow, removing it has fixed the problem. The game now runs on the phone again BUT there's no ads showing. Do I need to do anything else to show the ads?
     
    Last edited: May 7, 2011
  50. Kuneko

    Kuneko

    Joined:
    Mar 20, 2010
    Posts:
    8
    Still not working for me after days of trying and retrying everything step by step. Could someone have a quick look through this and tell me where I'm going wrong please?

    AndroidManifest.xml

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.Kuneko.StuntPilot"
    4.       android:versionCode="3"
    5.       android:versionName="1.02">
    6.     <uses-sdk android:minSdkVersion="6" />
    7.     <application
    8.         android:icon="@drawable/app_icon"
    9.         android:label="@string/app_name">
    10.         <activity android:name=".AdMobTestActivity"
    11.             android:label="@string/app_name">
    12.             <intent-filter>
    13.                 <action android:name="android.intent.action.MAIN" />
    14.                 <category android:name="android.intent.category.LAUNCHER" />
    15.             </intent-filter>
    16.         </activity>
    17.  
    18.         <!-- The application's publisher ID assigned by AdMob -->
    19.        <meta-data android:value=" a14dbf4fe7a1e90" android:name="ADMOB_PUBLISHER_ID" />
    20.  
    21.        <!-- AdMobActivity definition -->
    22.        <activity android:name="com.admob.android.ads.AdMobActivity"
    23.                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    24.                  android:configChanges="orientation|keyboard|keyboardHidden" />
    25.  
    26.        <!-- Track Market installs -->
    27.        <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
    28.            <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
    29.            </intent-filter>
    30.        </receiver>
    31.  
    32.    </application>
    33.  
    34.    <!-- AdMob SDK requires Internet permission -->
    35.    <uses-permission android:name="android.permission.INTERNET" />
    36.  
    37. </manifest>
    AdMobTestActivity.java

    Code (csharp):
    1. package com.Kuneko.StuntPilot;
    2.  
    3. import android.os.Bundle;
    4. import android.os.Handler;
    5. import android.util.Log;
    6. import android.view.View;
    7. import android.view.ViewGroup.LayoutParams;
    8. import android.widget.LinearLayout;
    9. import android.os.Message;
    10.  
    11. import com.unity3d.player.*;
    12.  
    13. import com.admob.android.ads.AdView;
    14. import com.admob.android.ads.SimpleAdListener;
    15. import com.admob.android.ads.AdManager;
    16.  
    17. public class AdMobTestActivity extends UnityPlayerActivity
    18. {
    19.     public AdView adView;
    20.     private boolean adVisible = true;
    21.     protected void onCreate(Bundle savedInstanceState)
    22.     {
    23.         Log.i("AdMob", "onCreate");
    24.  
    25.         // super (UnityPlayerActivity) will use setContentView() ...
    26.         super.onCreate(savedInstanceState);
    27.  
    28.         // ... so instead of using setContentView(), we call addContentView() from setupAds()
    29.         setupAds();
    30.  
    31.         // Add test devices (check the logcat for the ID string of your device..)
    32.         AdManager.setTestDevices( new String[] { "20000b42364f69e3" } );
    33.     }
    34.     private Handler handler = new Handler()
    35.     {
    36.         public void  handleMessage(Message msg)
    37.         {
    38.             switch (msg.what)
    39.             {
    40.                 case 0:
    41.                     if (adVisible)
    42.                     {
    43.                         adView.setVisibility(View.INVISIBLE);
    44.                         adVisible = false;
    45.                     }
    46.                     break;
    47.                 case 1:
    48.                     if (!adVisible)
    49.                     {
    50.                         adView.setVisibility(View.VISIBLE );
    51.                         adVisible = true;
    52.                     }
    53.                     break;
    54.                 default:
    55.                     break;
    56.             }
    57.         }
    58.     };
    59.  
    60.     public void EnableAds()
    61.     {
    62.         handler.sendEmptyMessage(1);
    63.         //adView.setVisibility(View.VISIBLE );
    64.     }
    65.  
    66.     public void DisableAds()
    67.     {
    68.         handler.sendEmptyMessage(0);
    69.         //adView.setVisibility(View.INVISIBLE );
    70.     }  
    71.     private void setupAds()
    72.     {
    73.  
    74.             LinearLayout layout = new LinearLayout(this);
    75.         layout.setOrientation(LinearLayout.VERTICAL);
    76.             addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    77.  
    78.             adView = new AdView(this);
    79.         layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    80.  
    81.             adView.setBackgroundColor(0xff000000);
    82.             adView.setPrimaryTextColor(0xffffffff);
    83.             adView.setSecondaryTextColor(0xffcccccc);
    84.             adView.setKeywords("stunt pilot free 3d flight sim kuneko plane game");
    85.             adView.setRequestInterval(30);
    86.  
    87.         // add listener for easier debugging
    88.             adView.setAdListener( new SimpleAdListener()
    89.             {
    90.                 public void onFailedToReceiveAd(com.admob.android.ads.AdView adView)
    91.                 {
    92.                     Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    93.                     super.onFailedToReceiveAd(adView);
    94.                 }
    95.  
    96.                 public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    97.                 {
    98.                     Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    99.                     super.onFailedToReceiveRefreshedAd(adView);
    100.                 }
    101.  
    102.                 public void onReceiveAd(com.admob.android.ads.AdView adView)
    103.                 {
    104.                     Log.d("AdListener", "onReceiveAd: " + adView.toString());
    105.                     super.onReceiveAd(adView);
    106.                 }
    107.  
    108.                 public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView)
    109.                 {
    110.                     Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    111.                     super.onReceiveRefreshedAd(adView);
    112.                 }
    113.             } );
    114.             adView.requestFreshAd();
    115.     }
    116. }
    I've attached screenshots of the various AdMob settings that I have. Any help would be very much appreciated. Thank you :)

    EDIT:

    Got it working, finally.

    I'll leave this post as it is for anyone else having trouble. My final problem was my AdMob Publisher ID, it had a " " (space) in front of it. Very silly mistake but there you go. Happy to have it working at last :)
     

    Attached Files:

    Last edited: May 10, 2011