插頁式廣告
本篇筆記的重點在 app 一啟動即顯示插頁式廣告
===== 2023.04.03 =====
但這種做法違反 AdMob 政策!所以參考程式碼就好 😒
先以 Android Studio 精靈建立新專案
進行修改
修改後的 activity_main.xml
修改後的 MainActivity.java
string.xml 內容
第一次啟動時,廣告會比較慢出現,之後就不會了;不過還是可以看得到 MainActivity,再找時間研究如何真正 app 的第一個畫面就是廣告。
相關筆記 ----
【Android Studio】置入 AdMob 播廣告 - 橫幅廣告(banner)篇
【Android】app 置入 Admob 廣告賺錢
先以 Android Studio 精靈建立新專案
進行修改
修改後的 activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.adtest.MainActivity">
<!-- view for AdMob Interstitial Ad -->
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/interstitial_ad_sample"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
修改後的 MainActivity.java
package com.test.adtest;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
mInterstitialAd.show();
}
});
}
private void requestNewInterstitial()
{
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
}
string.xml 內容
<resources>
<string name="app_name">Adtest</string>
<string name="interstitial_ad_sample">Interstitial Ad Sample</string>
<!-- -
This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id.
For more information, see https://support.google.com/admob/answer/3052638
<!- -->
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>
第一次啟動時,廣告會比較慢出現,之後就不會了;不過還是可以看得到 MainActivity,再找時間研究如何真正 app 的第一個畫面就是廣告。
相關筆記 ----
【Android Studio】置入 AdMob 播廣告 - 橫幅廣告(banner)篇
【Android】app 置入 Admob 廣告賺錢