RewardedAd

public abstract class RewardedAd


這個類別用於請求及顯示獎勵廣告。

public class MainActivity extends AppCompatActivity {

    private RewardedAd rewardedAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FullScreenContentCallback fullScreenContentCallback =
            new FullScreenContentCallback() {
                @Override
                public void onAdShowedFullScreenContent() {
                    // Code to be invoked when the ad showed full screen content.
                }

                @Override
                public void onAdDismissedFullScreenContent() {
                    rewardedAd = null;
                    // Code to be invoked when the ad dismissed full screen content.
                }
            };

        RewardedAd.load(
            this,
            "adUnitId",
            new AdRequest.Builder().build(),
            new RewardedAdLoadCallback() {
                @Override
                public void onAdLoaded(RewardedAd ad) {
                    findViewById(R.id.display_button).setVisibility(View.VISIBLE);
                    rewardedAd = ad;
                    rewardedAd.setFullScreenContentCallback(fullScreenContentCallback);
                }
            });
    }

    public void onDisplayButtonClicked(View view) {
        if (rewardedAd != null) {
            rewardedAd.show(
                this,
                new OnUserEarnedRewardListener() {
                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                        Toast.makeText(
                            MainActivity.this,
                            "onRewarded! currency: "
                                + rewardItem.getType() + "    amount: "
                                + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
                    }
                });
        }
    }
}

摘要

公用建構函式

公用方法

abstract Bundle

取得這個 RewardedAd 的所有可用廣告中繼資料。

abstract @NonNull String

傳回廣告單元 ID。

abstract @Nullable FullScreenContentCallback

取得這個 RewardedAdFullScreenContentCallback

abstract @Nullable OnAdMetadataChangedListener

取得這個 RewardedAdOnAdMetadataChangedListener

abstract @Nullable OnPaidEventListener

取得這個 RewardedAdOnPaidEventListener

abstract long

取得這個 RewardedAd 的刊登位置 ID。

abstract @NonNull ResponseInfo

傳回已載入廣告的 ResponseInfo 物件。

abstract @NonNull RewardItem

取得已載入獎勵廣告的獎勵項目。

static boolean
isAdAvailable(@NonNull Context context, @NonNull String adUnitId)

這個方法已淘汰。

請改用 isAdAvailable

static void
load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdManagerAdRequest adManagerAdRequest,
    @NonNull RewardedAdLoadCallback loadCallback
)

載入 RewardedAd

static void
load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdRequest adRequest,
    @NonNull RewardedAdLoadCallback loadCallback
)

載入 RewardedAd

static @Nullable RewardedAd
pollAd(@NonNull Context context, @NonNull String adUnitId)

這個方法已淘汰。

請改用 pollAd

abstract void

註冊回呼,在廣告顯示及關閉全螢幕內容時叫用。

abstract void
setImmersiveMode(boolean immersiveModeEnabled)

設定旗標,控制是否要在沉浸模式中顯示這個獎勵廣告物件。

abstract void

為這個 RewardedAd 設定 OnAdMetadataChangedListener

abstract void

註冊回呼,在預估這則廣告已賺取收益時叫用。

abstract void
setPlacementId(long placementId)

設定獎勵廣告的刊登位置 ID。

abstract void

為這個 RewardedAd 設定 ServerSideVerificationOptions

abstract void
show(
    @NonNull Activity activity,
    @NonNull OnUserEarnedRewardListener onUserEarnedRewardListener
)

顯示獎勵廣告。

公用建構函式

RewardedAd

public RewardedAd()

公用方法

getAdMetadata

public abstract Bundle getAdMetadata()

取得這個 RewardedAd 的所有可用廣告中繼資料。如果載入的廣告沒有中繼資料,廣告中繼資料就會空白。廣告中繼資料可能會在載入後更新。使用 onAdMetadataChanged 監聽變更。

getAdUnitId

public abstract @NonNull String getAdUnitId()

傳回廣告單元 ID。

getFullScreenContentCallback

public abstract @Nullable FullScreenContentCallback getFullScreenContentCallback()

取得這個 RewardedAdFullScreenContentCallback

getOnAdMetadataChangedListener

public abstract @Nullable OnAdMetadataChangedListener getOnAdMetadataChangedListener()

取得這個 RewardedAdOnAdMetadataChangedListener

getOnPaidEventListener

public abstract @Nullable OnPaidEventListener getOnPaidEventListener()

取得這個 RewardedAdOnPaidEventListener

getPlacementId

public abstract long getPlacementId()

取得這個 RewardedAd 的刊登位置 ID。

getResponseInfo

public abstract @NonNull ResponseInfo getResponseInfo()

傳回已載入廣告的 ResponseInfo 物件。

getRewardItem

public abstract @NonNull RewardItem getRewardItem()

取得已載入獎勵廣告的獎勵項目。

isAdAvailable

public static boolean isAdAvailable(@NonNull Context context, @NonNull String adUnitId)

如果從 startPreload 載入可用的獎勵廣告,就會傳回 true

load

public static void load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdManagerAdRequest adManagerAdRequest,
    @NonNull RewardedAdLoadCallback loadCallback
)

載入 RewardedAd

參數
@NonNull Context context

背景資訊。

@NonNull String adUnitId

廣告單元 ID。

@NonNull AdManagerAdRequest adManagerAdRequest

包含指定目標資訊的 Google Ad Manager 廣告請求。

@NonNull RewardedAdLoadCallback loadCallback

獎勵廣告載入完成時要叫用的回呼。

load

public static void load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdRequest adRequest,
    @NonNull RewardedAdLoadCallback loadCallback
)

載入 RewardedAd

參數
@NonNull Context context

背景資訊。

@NonNull String adUnitId

廣告單元 ID。

@NonNull AdRequest adRequest

含有指定目標資訊的廣告請求。

@NonNull RewardedAdLoadCallback loadCallback

獎勵廣告載入完成時要叫用的回呼。

pollAd

public static @Nullable RewardedAd pollAd(@NonNull Context context, @NonNull String adUnitId)

startPreload 擷取下一個載入的獎勵廣告,如果沒有廣告,則擷取 null

setFullScreenContentCallback

public abstract void setFullScreenContentCallback(
    @Nullable FullScreenContentCallback fullScreenContentCallback
)

註冊回呼,在廣告顯示及關閉全螢幕內容時叫用。

setImmersiveMode

public abstract void setImmersiveMode(boolean immersiveModeEnabled)

設定旗標,控制是否要在沉浸模式中顯示這個獎勵廣告物件。請在 show 之前呼叫這個方法。在 show 期間,如果這個旗標已開啟且支援沉浸模式,系統會為有獎廣告開啟 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 和 SYSTEM_UI_FLAG_HIDE_NAVIGATION。

setOnAdMetadataChangedListener

public abstract void setOnAdMetadataChangedListener(
    @Nullable OnAdMetadataChangedListener listener
)

為這個 RewardedAd 設定 OnAdMetadataChangedListener

setOnPaidEventListener

public abstract void setOnPaidEventListener(@Nullable OnPaidEventListener listener)

註冊回呼,在預估這則廣告已賺取收益時叫用。

setPlacementId

public abstract void setPlacementId(long placementId)

設定獎勵廣告的刊登位置 ID。

為確保報表會納入這個刊登位置 ID,請在顯示廣告前呼叫這個方法。

參數
long placementId

AdMob 使用者介面為已設定的刊登位置提供的長整數。

setServerSideVerificationOptions

public abstract void setServerSideVerificationOptions(
    @Nullable ServerSideVerificationOptions options
)

為這個 RewardedAd 設定 ServerSideVerificationOptions

顯示

public abstract void show(
    @NonNull Activity activity,
    @NonNull OnUserEarnedRewardListener onUserEarnedRewardListener
)

顯示獎勵廣告。

參數
@NonNull Activity activity

顯示獎勵廣告的活動。

@NonNull OnUserEarnedRewardListener onUserEarnedRewardListener

使用者獲得獎勵時,系統會通知這個監聽器。