獎勵廣告

必要條件

  • Google Mobile Ads SDK 19.7.0 以上版本。
  • 按照入門指南匯入 Google Mobile Ads SDK,並更新您的 Android 資訊清單。

一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非實際上線廣告。否則我們可能會將您的帳戶停權。

如要載入測試廣告,最簡單的方式就是使用 Android 獎勵廣告專屬的測試廣告單元 ID:

ca-app-pub-3940256099942544/5224354917

這種代碼經過特別設定,可針對每個請求傳回測試廣告,您可以在編寫、測試及偵錯時,在自己的應用程式中免費使用此功能。只要先以自己的廣告單元 ID 取代,再發布應用程式。

如要進一步瞭解 Mobile Ads SDK 的測試廣告運作方式,請參閱「測試廣告」一文。

載入獎勵廣告物件

系統會在載入 RewardedAd 類別的靜態 load() 方法並傳入 RewardedAdLoadCallback 時,載入獎勵廣告。此操作通常在 ActivityonCreate() 方法中完成。請注意,如同其他格式的載入回呼,RewardedAdLoadCallback 都會使用 LoadAdError 以提供更高的擬真錯誤詳細資料。

Java

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
  private RewardedAd mRewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    AdRequest adRequest = new AdRequest.Builder().build();

    RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
      adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // Handle the error.
          Log.d(TAG, loadAdError.toString());
          mRewardedAd = null;
        }

        @Override
        public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
          mRewardedAd = rewardedAd;
          Log.d(TAG, "Ad was loaded.");
        }
    });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private var mRewardedAd: RewardedAd? = null
  private final var TAG = "MainActivity"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    var adRequest = AdRequest.Builder().build()

    RewardedAd.load(this,"ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.toString())
        mRewardedAd = null
      }

      override fun onAdLoaded(rewardedAd: RewardedAd) {
        Log.d(TAG, "Ad was loaded.")
        mRewardedAd = rewardedAd
       }
    })
  }
}

設定 FullScreenContentCallback

FullScreenContentCallback 會處理與顯示 RewardedAd 相關的事件。在顯示 RewardedAd 之前,請務必按照下列方式設定回呼:

Java

mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
  @Override
  public void onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.");
  }

   @Override
  public void onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.");
    mRewardedAd = null;
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.");
    mRewardedAd = null;
  }

  @Override
  public void onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.");
  }

  @Override
  public void onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.");
  }
});

Kotlin

mRewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.")
  }

  override fun onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.")
    mRewardedAd = null
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.")
    mRewardedAd = null
  }

  override fun onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.")
  }

  override fun onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.")
  }
}

顯示廣告

顯示獎勵廣告時,您會使用 OnUserEarnedRewardListener 物件來處理獎勵事件。

Java

if (mRewardedAd != null) {
  Activity activityContext = MainActivity.this;
  mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

Kotlin

if (mRewardedAd != null) {
  mRewardedAd?.show(this, OnUserEarnedRewardListener() {
    fun onUserEarnedReward(rewardItem: RewardItem) {
      var rewardAmount = rewardItem.amount
      var rewardType = rewardItem.type
      Log.d(TAG, "User earned the reward.")
    }
  })
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.")
}

常見問題

初始化呼叫是否逾時?
10 秒後,Google 行動廣告 SDK 會叫用 OnInitializationCompleteListener,即使中介服務聯播網尚未完成初始化也一樣。
收到初始化回呼時,如果某些中介服務聯播網尚未準備就緒,該怎麼辦?

最佳做法是在 OnInitializationCompleteListener 的回呼中載入廣告。即使中介服務聯播網尚未就緒,Google Mobile Ads SDK 仍會請求該聯播網請求廣告。因此,如果中介服務聯播網在逾時時間結束後完成初始化,該工作階段仍可繼續處理該工作階段中的後續廣告請求。

您可以呼叫 MobileAds.getInitializationStatus(),繼續在整個應用程式工作階段中輪詢所有轉接程式的初始化狀態。

如何找出特定中介服務聯播網尚未準備就緒的原因?

AdapterStatus.getDescription() 說明為什麼轉接程式還無法處理廣告請求。

GitHub 範例