今天要來筆記關於在Android應用程式內插入廣告的方法
其實我覺得很多人的夢想大概跟我一樣,都會希望自己可以「可以躺著賺錢」吧(笑)
是的,沒錯;我的夢想是從印鈔機攔截鈔票放入自己口袋
所以~開始寫部落格、做Youtube影片、學股票投資等等等,或者做各式各樣的努力
那這篇,我就是要來寫我們身為手機應用程式開發者,如何將Google廣告插入到你的手機應用程式中,藉以此賺取App下載收入
來,看一下範例吧
好,那這次沒有Github歐~XD
開始!
1. 申請Google AdMob
首先,Google的廣告插件是源自於Google 提供的一款名為AdMob的服務
->https://apps.admob.com/ (請記得申請一組Google帳號...)
申請好後,來到主頁,點擊應用程式
再來,建立第一個應用程式
首先,他會引導你新增應用程式..
填入暱稱即可,你也可以填入com.example.yourapp這類的,方面識別本欄廣告所對應的App專案
按下「新增應用程式」
okay,接下來你會跳轉到「應用程式總覽」,這裡他有寫須審核不用理他,過段時間他就會審核完成了
那接下來按下「新增廣告單元」
這一次的示範,我們用最基本的「橫幅廣告」,選取他
輸入廣告單元名稱(隨意設定即可),並按下「建立廣告單元」
最後會來到這個畫面
請把以上1、2都記下來
1 的部分是App本身用的編號,要寫在AndroidMinifast.xml裡面的
2 的部分是要寫在AdView內的,總之目前就先把這兩個記下來,稍後我會以1、2代替
到這邊,在Google這方面的東西都已經設定好了
接著我們來到專案的部分..
2. 寫入AdView
接下來我們要來仔入相關函式庫以及載入橫幅(adView)
首先載入函式庫,請來到build.gradle(:app)的部分
然後,請在dependencies內加入庫
implementation 'com.google.android.gms:play-services-ads:20.4.0'
再來,請來到AndroidManifest.xml,填入以下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.noahliu.googleadsexample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.GoogleAdsExample"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-6102762083747878~4333788331"/> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
注意,在綠底白字的部分要填入的是上述的1的部分呦
再來,請來到activity_main.xml
填入以下
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" app:adSize="BANNER" app:adUnitId="ca-app-pub-6102762083747878/2034912147" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <!-- app:adUnitId="ca-app-pub-8691515013768572/4466662034"正式用--> <!--ads:adUnitId = "ca-app-pub-6102762083747878/2034912147"測試用--> </androidx.constraintlayout.widget.ConstraintLayout>
同樣,綠底白字請填入2的內容
最後,來到MainActivity,填入以下內容即可載入廣告
public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName()+"My"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { Map<String, AdapterStatus> map = initializationStatus.getAdapterStatusMap(); for (Map.Entry<String, AdapterStatus> entry : map.entrySet()) { AdapterStatus adapterStatus = entry.getValue(); AdapterStatus.State state = adapterStatus.getInitializationState(); Log.d(TAG, "key = " + entry.getKey() + ", state = " + state.name()); } } }); AdView adview = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adview.loadAd(adRequest); } }
OK,到這邊按下執行,就可以看到廣告投放的畫面囉:D
3. 獲取廣告回傳事件
OK,做到這邊也差不多完成了
那麼有些商家會在點擊廣告後可能發個Toast感謝你,或者點完廣告後接下來就不在出現等等的相關操作
那麼這部分又是怎麼做到的呢?
基本上就是的AdLinstener回調事件
那麼,來加個程式吧
public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName()+"My"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { Map<String, AdapterStatus> map = initializationStatus.getAdapterStatusMap(); for (Map.Entry<String, AdapterStatus> entry : map.entrySet()) { AdapterStatus adapterStatus = entry.getValue(); AdapterStatus.State state = adapterStatus.getInitializationState(); Log.d(TAG, "key = " + entry.getKey() + ", state = " + state.name()); } } }); AdView adview = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adview.loadAd(adRequest); adview.setAdListener(new AdListener() { @Override public void onAdClosed() { super.onAdClosed(); Log.d(TAG, "onAdClosed: 廣告已關閉(關閉瀏覽器)"); } @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { super.onAdFailedToLoad(loadAdError); Log.d(TAG, "onAdFailedToLoad: 廣告載入失敗,原因: "+loadAdError.getResponseInfo()); } @Override public void onAdOpened() { super.onAdOpened(); Log.d(TAG, "onAdOpened: 廣告已開啟(開啟瀏覽器)"); } @Override public void onAdLoaded() { Log.d(TAG, "onAdLoaded: 廣告載入中"); super.onAdLoaded(); } @Override public void onAdClicked() { super.onAdClicked(); Log.d(TAG, "onAdClicked: 廣告被點選"); } @Override public void onAdImpression() { super.onAdImpression(); Log.d(TAG, "onAdImpression: 廣告已顯示"); } }); } }
在這裏,我已經將回調事件以中文告訴你了,便不再多講
其中有個載入失敗的回調,這個通常是在手機沒網路的情況下會載入失敗
而載入失敗的話,將會回調如下訊息
此時雖然不會閃退,但是此時畫面上就會沒有廣告,相對地你也賺不到收益喔!
ok,撰寫至此,想必也是做出來了
但實際上,Google官方其實有幫你預備超級完整的教學XD
->https://developers.google.com/admob/android/quick-start?hl=zh_cn
以及今天介紹的橫幅廣告
->https://developers.google.com/admob/android/banner?hl=zh_cn
我當時自己也是靠著這兩篇就完成了,完全沒有看其他的參考文件
所以我感覺這部分要麻就是很簡單,要麻就是因為他們寫得太清楚了所以一次搞定XD
那麼本文就寫到這,希望大家也能靠著寫APP躺賺囉:D