Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
作者: 魏蘶
(感謝台灣 Microsoft 資深平台架構經理 Herman Wu 的大力協助)
(感謝 Corona SDK 台灣區大使 Bob Yeh 大力協助)
1.如果還沒有admob帳號的話,要到admob的網站申請帳號。之後幫自己的程式申請的一個廣告ID。上面步驟都做完後,到Google Mobile Ads SDK網站,下載給Windows Phone8的SDK。
2.下載後的壓縮檔解壓縮後,找到其中一個名為「GoogleAds.dll」的檔。把這個檔存在容易找到的路徑。比方我在C槽下面陸續開了很多個資料夾,把「GoogleAds.dll」存在 C:\dev\mobile\libraries\windows-phone\admob中。
3.回到Visual Studio,按下右邊欄Properties後,左邊應用程式選項中,確認「目標Windows Phone OS版本」選的是Windows Phone 8.0。因為Google Admob廣告目前只有支援Windows Phone 8.0。
4.在右邊欄選取WMAppManifest.xml之後,在左邊視窗上方選取 [功能]。把這個頁面中,下列的選項打勾
ID_CAP_MEDIALIB_AUDIO
ID_CAP_MEDIALIB_PLAYBACK
ID_CAP_NETWORKING
ID_CAP_SENSORS
ID_CAP_WEBBROWSERCOMPONENT
5.在右邊欄的 [參考] 上面按下右鍵。選擇 [加入參考]。
6.選擇在step2下載存檔的「GoogleAds.dll」檔案。
7.在右邊欄選擇 [MainPage.xaml] > [MainPage.xaml.cs]。雙擊打開檔案,就可以在左邊的視窗看到MainPage.xaml.cs的程式碼。如上圖,請先在上面一大串using的程式碼下面,新增下面三行
using CoronaLabs.Corona.WinRT;
using System.Diagnostics;
using GoogleAds;
8.在同一檔案 [MainPage.xaml.cs] 的下面,找到MainPage這個Class。在MainPage()函式之前,如上圖寫上下面的程式碼:
(*注意 >> “把你的廣告ID寫在這邊”這行要換成你的廣告ID )
AdView bannerAd = new AdView
{
Format = AdFormats.Banner,
AdUnitId = “把你的廣告ID寫在這邊”,
};
9.如上圖,在MainPage()函式結束之前,在MainPage()函式中加入下面的程式碼:
fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;
bannerAd.ReceivedAd += OnAdReceived;
bannerAd.FailedToReceiveAd += OnFailedToReceiveAd;
bannerAd.VerticalAlignment = VerticalAlignment.Bottom;
bannerAd.Visibility = Visibility.Collapsed; //Visible or Collapsed
fCoronaPanel.Children.Add(bannerAd);
AdRequest adRequest = new AdRequest();
//adRequest.ForceTesting = true;
bannerAd.LoadAd(adRequest);
10.在MainPage()函式結束之後,MainPage這個Class結束之前,[也就是說在MainPage()函式的外面,MainPage類別的裡面,]如圖寫下下面的函式:
private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
{
// Register bridges with corona.
e.CoronaRuntimeEnvironment.AddEventListener(“ShowAd”, Native_ShowAd);
e.CoronaRuntimeEnvironment.AddEventListener(“HideAd”, Native_HideAd);
}
public ICoronaBoxedData Native_ShowAd(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e)
{
Debug.WriteLine(“Loading google interstitial ad…”);
bannerAd.Visibility = Visibility.Visible;
return CoronaBoxedBoolean.True;
}
public ICoronaBoxedData Native_HideAd(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e)
{
bannerAd.Visibility = Visibility.Collapsed;
return CoronaBoxedBoolean.True;
}
private void OnAdReceived(object sender, AdEventArgs e)
{
Debug.WriteLine(“Received ad successfully”);
}
private void OnFailedToReceiveAd(object sender, AdErrorEventArgs errorCode)
{
Debug.WriteLine(“Failed to receive ad with error ” + errorCode.ErrorCode);
}
11.寫完上面的程式碼就OK了,在程式裡要秀出廣告的時候,(請在main.lua或是任何一個場景的程式碼中,)用下面的程式碼秀出廣告:
Runtime:dispatchEvent({name = “ShowAd”})
12.程式裡要隱藏廣告的時候,(請在main.lua或是任何一個場景的程式碼中,)用下面的程式碼隱藏廣告:
Runtime:dispatchEvent({name = “HideAd”})
( [這邊] 是我的MainPage.xaml.cs提供給大家參考。請不要直接拷貝。全部拷貝複製會錯。)
以上就是今天分享的內容。在這篇文章中,我們學到怎麼樣在手機程式中放入 Admob的廣告。這應該是寫App最重要的事情吧?用上面的方法,我已經把很多我的 App 放到 Windows Phone市集了 ,歡迎大家加入我的行列,一起來開發吧!
﹝相關文章﹞
[Corona SDK] 用 CoronaCards 來開發 Windows Phone程式教學 Part 1
[Corona SDK] 用 CoronaCards 來開發 Windows Phone程式教學 Part 2