Ads on Phone 8

Yeah already tried all that. Theres something else that Google is doing behind the scenes which you cant remove. If you find a way to remove the interstitial let me know. :slight_smile: AFAIK There is no function or method that allows it so as soon as AdRequest is created thats when the slowdown starts. Doesnt even need to load the ad. Anyway After removing some of the banners it is better . 

@Spacewolf

Here is the code sample I promised.

Actually it is an app template, and contains a PDF document of the entire process as well as explaining some of my source code for creating a template very easy and sample like

http://www.larrymeadows.me/books/ComposerFrameWorkV3.zip

The PDF doc points to a different download location but my blog kept rewriting the URL and would not let it download.

If you have problems or it removes the .zip extension on the end, just add it back and hit enter. It did download in my testing.

I plan to use it for a meetup demo.

Enjoy Larry

@Rob,

Would you share a sample when you get the Google and AdMob code working?

thanks

Larry

Hello Larry, it’s late here and I going away for a few days so just quickly, what have you demonstrated in your example? Just so I don’t repeat anything. Or I could just post up everything I’ve done? All I know is that you can’t have more than one or two banners plus an interstitial as they all have some heavy stuff going on and the interstitial conflicts with any other runtime stuff u have going on enter frames. This isn’t a problem if u just have one of each anyway. I tried to make a banner rotator. I can post that plus an example of launching a interstitial from corona withiut changing any code in VS.

Sorry, i wrote my post and then took the family out to the movies.

My example is a small app template that uses the composer api.

It also shows how to implement the windows ad api and send data to and from C# to LUA and vise versa.

If you want you can  post up everything you have done, I’ll also review it and mess around with the code to see if I can help out with a few things. I have a friend that work locally here at Microsoft, and I know at a couple MVP’s that may have an Idea or two on some of the issues.

Not sure how fast they will respond to my inquiries but I can show them what is going on and see if they have any ideas on what to look at.

Larry

@doubleslashdesign

Thank you for posting this! I am looking forward to trying it out and integrating a banner into my app. I’ll let you know how it goes :slight_smile:

Ok well Im leaving in a couple of hours. Ill paste up what I have done. This is for Interstitials. I also practiced creating my own CS and including it into project as Im new to Visual Studio. So this is a longer way round it but it allows to only need to change the ID from LUA and create my own custom class which I can add my own utilities and reuse that across future projects. (all about work flow me). I maybe have some unnecessary code or might have it incorrect but it works. Jy first Corona projects a few years ago wasnt exactly efficient :slight_smile:

Add the googleAds.dll as reference downloaded with the SDK.

I right click on solution and choose add class. I then select c# class. I name this to whatever I want before or after form Visual Studio . In this example its AdModule.cs. At the top I make sure I add using “GoogleAds” so the top will be something like this:

///////////////

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Diagnostics;

using GoogleAds;

////////////////

I then declare these in the class:

   private InterstitialAd interstitialAd;     

   private string ID;

In Admodule.cs I have this function

///////////////////////////

   public void createInterstertial(string adUnitID)

        {

           ID = adUnitID;

           interstitialAd = new InterstitialAd(ID);

           AdRequest adRequest = new AdRequest();

           

            // Enable test ads.

           //  adRequest.ForceTesting = true;

            interstitialAd.ReceivedAd += OnAdReceived;

            interstitialAd.LoadAd(adRequest);

            interstitialAd.FailedToReceiveAd += OnFailedToReceiveAd;

           interstitialAd.DismissingOverlay += OnDismissingOverlay;

         //   adRequest = null;

           // System.GC.Collect();

            

        }

/////////////////////////

(note the bottom two lines dont do anything. I wish they did but they dont need to be there. I only tried to null the adRequest as thats the bit that slows down any heavy Runtime EnterFrame methods while running banners at the same time.) 

I then add the call backs: Note the dismisss function is useless as theres nothing to remove in Googles ad plugin just yet.

\\\\\\\\\\\\\\\\\\\\\\\\\\\

 private void OnAdReceived(object sender, AdEventArgs e)

        {

            System.Diagnostics.Debug.WriteLine(“Ad received successfully”);

            interstitialAd.ShowAd();

            

        }

        private void OnDismissingOverlay(object sender, AdEventArgs e)

        {

            Debug.WriteLine(“Dismissing interstitial.”);

         

          //  interstitialAd = new InterstitialAd(ID);

          //  AdRequest adRequest = new AdRequest();

            //interstitialAd.stopLoading();

           // adRequest = null;

           //interstitialAd = null;

            interstitialAd.ReceivedAd += null;

            interstitialAd.DismissingOverlay += null;

            interstitialAd.FailedToReceiveAd += null;

            interstitialAd = null;

           

            System.GC.Collect();

            

            

        }

        private void OnFailedToReceiveAd(object sender, AdErrorEventArgs errorCode)

        {

            Debug.WriteLine("Failed to receive interstitial with error " + errorCode.ErrorCode);

        }

\\\\\\\\\\\\\\\\\\\\\\\\\\

Then in mainPage.cs I add this to the top to reference my custom class:

 AdModules testClass = new AdModules();

\\\\\\\\\

Then obviously  the lua interop bit:

\\\\\\\\\

fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;

\\\\\\\

Then the function:

\\\

   private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)

        {

            e.CoronaRuntimeEnvironment.AddEventListener(“admobInterstertial”, CoronaAdMobInterstertial);

}

\\\\\\\\\\\\\\

Then this is the code to launch m adModule.cs code:

\\\\\\\\\\\

  private CoronaLabs.Corona.WinRT.ICoronaBoxedData CoronaAdMobInterstertial(

         CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)

        {

            var adUnitID = e.Properties.Get(“adUnitID”) as CoronaLabs.Corona.WinRT.CoronaBoxedString;

            if (adUnitID == null)

            {

                return CoronaLabs.Corona.WinRT.CoronaBoxedString.From(“missing ad unit ID”);

            }

            testClass.createInterstertial(adUnitID.ToString());

            return null;

        }

\\\\\\\\\\\\\\\\\

Finally in LUA:

local adMobIntertstertialID=“123456789bum”;

local status=false;

function M.showAdMobInterstertial()

if status==false then

status=true;

Runtime:dispatchEvent({name=“admobInterstertial”,adUnitID=adMobIntertstertialID});

timer.performWithDelay(10000,function()status=false;end);

end

end

\\\

I made this timer function as a bodge job to block double triggering of ads, or forcing them not to come up until after a certain amount as it crashes if tries to load two before its finished.

So a bit of a rush so hope this is good enough.

DOing banners is just the same as Windows ads really:

Add this in the mainPage.xaml. dont forget the using googleAds. bit and reference the DLL

\\\\\

  <GoogleAds:AdView 

                    x:Name=“ad4”

                    HorizontalAlignment=“Center” VerticalAlignment=“Top” 

                    Height=“80” Format=“Banner”

                    AdUnitID=“123456789bum”

                     CharacterSpacing=“1”

                    Width=“480”/>

\\\

Google dont ask you to put a name in but doing this allows you hide or unhide them FOr instance in my main.cs is this:

\\\\\\\\\\

    private CoronaLabs.Corona.WinRT.ICoronaBoxedData CoronaHideAd4(

CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)

        {

            ad4.Visibility = System.Windows.Visibility.Collapsed;

            ad4.IsEnabled = false;

            return null;

        }

        private CoronaLabs.Corona.WinRT.ICoronaBoxedData CoronaUnHideAd4(

        CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)

        {

            ad4.Visibility = System.Windows.Visibility.Visible;

            ad4.IsEnabled = true;

            return null;

        }

\\\

        private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)

        {

            e.CoronaRuntimeEnvironment.AddEventListener(“unHideAd4”, CoronaUnHideAd14);

  e.CoronaRuntimeEnvironment.AddEventListener(“hideAd4”, CoronaHideAd4);

}

\\\\\\\\\\\\

No in LUA I can hide or unhide then (and or any others with names:

\\\

Runtime:dispatchEvent({name="unHideAd4});

Runtime:dispatchEvent({name="hideAd4});

Hope thats enough. Gotta go now :slight_smile:

Oh and in my dismiss function there’s a load of stuff in there that you can remove. It was me trying different ways to clear it from memory. You could use this to reload a new add request for later.

Rob,

Regarding your dismiss code where you do this…

private void OnDismissingOverlay(object sender, AdEventArgs e) { interstitialAd.ReceivedAd += null; interstitialAd.DismissingOverlay += null; interstitialAd.FailedToReceiveAd += null; interstitialAd = null; System.GC.Collect(); }

The “+= null;” part will *not* remove your event handler from the object.  The “+=” operator adds an event handler, and since what you are adding is “null”, then the “+=” operation will end up doing nothing.  To remove event handlers, you must do a “-=” operation with the same method or delegate that you original gave the event, like this…

private void OnDismissingOverlay(object sender, AdEventArgs e) { interstitialAd.ReceivedAd -= OnAdReceived; interstitialAd.DismissingOverlay -= OnDismissingOverlay; interstitialAd.FailedToReceiveAd -= OnFailedToReceiveAd; interstitialAd = null; System.GC.Collect(); }

The reason is because a .NET class’ event field can support multiple event handlers.  You can only remove your event handler by its reference.  Microsoft documents this via the link below.

   http://msdn.microsoft.com/en-us/library/ms366768.aspx

That said, I think what you did was relatively harmless.  However, if you were adding an event handler to a “static” event, then the consequence is that it’ll end up keeping your event handler object alive for the life-time of your application, which is the garbage collected language equivalent of a “memory leak”.  Just something to be aware of.

When implementing my google ads interstitial ad, I first tried reusing the InterstitialAd object and just requesting a new ad with it every time. This will only display an ad the first time. Any subsequent ads will NOT display on the screen. So just make sure every time you want to load and show an ad, you create a new InterstitialAd object! 

Just a gotcha I ran into and wasted some time figuring out why it didn’t work.

@Rob, @spacewolf,

hi, I am trying to implement robs solution but come a little unstuck I have never looked at c# ever.

Anyway, would it be possible to create 2 small projects just showing the use each ad type (banner/ interstitials) please?

I am getting unstuck when adding code to the mainpage.cs( should it be mainpage.xaml.cs ?), to be precise where everything goes.

This would be excellent is possible, if not then some formatting and better instructions for non c~ users ? kind of llike a idiots guide :slight_smile:

thank you for your great work on this

Hi Bubble. I too had never used c# (well a little with Unity) or Visual Studio.

I looked at the Lua/Interop guide and although it looks very daunting. Once you just copy the code and modify, plus some experimenting of your own, in an hour or so you will understand it. Its easier than it looks so domt be afraid to jump into it. You will really benefit from this. Within a couple of weeks I was making my own classes and incorporating that using what Corona had Joshua had provided and some more experimenting it doesnt take long. There are still some odd bits and pieces that I am getting to grips with mind you. Most of the time you will be using just the xaml and main cs

There are three ways for ads that work so far. Google banner, Windows Banner and Windows Interstitial. The easiest ones to work with are the banners and It really is like drag and drop. You dont need to know about Lua Interop for this unless you want to hide them at runtime

The guides that Windows and Google are really straightforward. If you follow them you cant go wrong. But you must take these steps:

Download the SDK

Get the DLL out and put into your project folder (right click properties of DLL file and “unlock” fist as it cant be used.

Add as reference in VS by right clicking references/add/browse to your sdk

Make sure you check the device capabilities in the manifest  and add whats required by the guide

Now if using Interstertials or any code thats triggered/created by your code you do this:

Add “using” and then the DLL file at top of your main.cs (or whatever CS will be doing the code)

Then copy code into relevant places inside your class bit. Then if you want to trigger this at runtime you want put the code into a new void function and trigger it with Lua Interop - easier than it looks trust me. I would just get it working first though by the guides

IF using banners then these arent created by code normaly so you add them into the designer view as an object. You just either copy the xaml code in including the reference to the SDK (which goes near the Corona one)

Or even easier you can go to the tool box and drag drop the view controller/or ad control (I cant remember depending on Google or WIndows ads) onto your designer view which is the view you see with Corona on a picture of the device. This will automatically add all the info into the xaml where you can edit or add details for the ads. Job done!

I would really read up a bit on whats and where things go in c#. I only know as much as what I need and it took a day or two to get my head around. Its really worth it. If I want to do something in c# I look it up and trigger it with Corona. 

Hi Rob,

thanks for the tips.

Obviously working in VS2013, I cant seem to find the adview in the toolbox.

I am following this https://developers.google.com/mobile-ads-sdk/docs/admob/wp/quick-start

when i try to use the code iam getting undeclard prefix errors for googleads.

Perhaps Iam following the wrong stuff ?

thanks

ha,

got it, didnt add the xmls:googleads

Hi guys,

Do you still need some help? I’ve thought about writing a little guide for setting up ads in wp8…

@Spacewolf

that would be great. if you want you can piggy back on my app and document. the PDF is in the zip file, but i can shoot you the Word docx file if you want.

http://www.larrymeadows.me/books/ComposerFrameWorkV3.zip

@spacewolf,

tis would be very useful, It took me a few hours to get to grips with it.

Just need to sort out interstitials now.

If it could be done without a framework also it would be most helpful. I dont use composer or SB I prefer to take care of stuff myself, but whatever you can offer would be cool.

Are interstitial ads full page ads ? so then they are different than the normal windows 8.1 ads?

@Spacewolf

Are you going to do a tutorial on adding interstitial ads?

Larry

Yeah I will, just haven’t found the time yet. Hoping to put something together today :slight_smile: