如何在Android中整合Google AdMob激勵影片廣告?


在Android中整合Google AdMob激勵影片廣告為開發者提供了強大的應用變現策略。透過實現此功能,開發者可以在為使用者提供引人入勝體驗的同時,透過影片廣告獲得收入。AdMob激勵影片廣告允許使用者自願觀看影片以換取應用內獎勵,為使用者和開發者雙方創造雙贏局面。此整合使開發者能夠利用Google龐大的廣告網路,覆蓋廣泛的使用者群體,並最大限度地提高其應用的盈利潛力。透過遵循必要的步驟,開發者可以將AdMob激勵影片廣告無縫地整合到他們的Android應用中,並開闢新的收入來源。

Google AdMob

Google AdMob是由Google開發的廣泛的移動廣告平臺。它使開發者能夠透過展示各種型別的廣告(如橫幅廣告、插頁式廣告和激勵影片廣告)來實現其移動應用的盈利。憑藉其龐大的廣告客戶和釋出商網路,AdMob使開發者能夠接觸到全球受眾,並最大限度地提高其應用的收入潛力。

該廣告管理平臺為開發者提供了使用者友好的介面和強大的工具。它使他們能夠最佳化其廣告策略,增強其應用的獲利能力,並有效地跟蹤其效能。

開發者可以透過使用Google AdMob有效地從他們的Android和iOS應用中獲得收入。此平臺使他們能夠向用戶提供相關且引人入勝的廣告體驗,確保盈利的同時保持受眾的積極參與。

方法

有多種方法可以將Google AdMob激勵影片廣告整合到Android應用中。以下是一些常見的方法:

  • AdMob原生高階廣告

  • Google移動廣告SDK

  • AdMob中介

AdMob原生高階廣告

此方法允許開發者透過利用原生高階廣告功能將Google AdMob激勵影片廣告整合到他們的Android應用中。使用此方法,開發者可以靈活地自定義激勵影片廣告的外觀,使其與應用的設計和佈局無縫融合,從而提供更一致的使用者體驗。

演算法

  • 初始化AdMob SDK並配置激勵影片廣告單元ID。

  • 為激勵影片廣告檢視建立自定義佈局,使其與應用設計相匹配。

  • 載入激勵影片廣告並設定廣告生命週期事件的事件監聽器。

  • 當用戶觸發獎勵操作時,顯示激勵影片廣告並等待其完成。

  • 在廣告成功完成時,向用戶提供指定的獎勵。

示例

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   private Button btnShowAd;
   private RewardedAd rewardedAd;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Load the rewarded ad
      AdRequest adRequest = new AdRequest.Builder().build();
      RewardedAd.load(this, "YOUR_AD_UNIT_ID", adRequest, new RewardedAdLoadCallback() {
         @Override
         public void onAdLoaded(@NonNull RewardedAd ad) {
            rewardedAd = ad;
         }

         @Override
         public void onAdFailedToLoad(@NonNull LoadAdError error) {
            rewardedAd = null;
         }
      });

      btnShowAd = findViewById(R.id.btnShowAd);
      btnShowAd.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            showRewardedVideoAd();
         }
      });
   }

   // Show the Rewarded Video Ad
   private void showRewardedVideoAd() {
      if (rewardedAd != null) {
         rewardedAd.show(this, new RewardedAdCallback() {
            @Override
            public void onRewardedAdOpened() {
               // Ad opened
            }

            @Override
            public void onRewardedAdClosed() {
               // Ad closed
            }

            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
               // User earned a reward
            }

            @Override
            public void onRewardedAdFailedToShow(AdError error) {
               // Ad failed to show
            }
         });
      }
   }

   @Override
   protected void onResume() {
      super.onResume();
      if (rewardedAd != null) {
         rewardedAd.resume(this);
      }
   }

   @Override
   protected void onPause() {
      super.onPause();
      if (rewardedAd != null) {
         rewardedAd.pause(this);
      }
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      if (rewardedAd != null) {
         rewardedAd = null;
      }
   }
} 

//activity_main.xml
<!-- 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"
   android:padding="16dp">

   <Button
      android:id="@+id/btnShowAd"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Show Rewarded Video Ad"
      android:layout_centerInParent="true" />

</RelativeLayout>

輸出

Google移動廣告SDK

將Google移動廣告SDK整合到Android應用中,使開發者能夠顯示激勵影片廣告。透過遵循SDK的文件並整合所需的程式碼,開發者可以輕鬆地整合激勵影片廣告並管理其在應用中的顯示,從而利用Google龐大的廣告網路。

演算法

  • 將Google移動廣告SDK匯入Android專案。

  • 在專案清單中設定必要的許可權和依賴項。

  • 初始化移動廣告SDK並配置激勵影片廣告單元ID。

  • 載入激勵影片廣告並設定事件監聽器以跟蹤廣告生命週期事件。

  • 當用戶觸發獎勵操作時,檢查廣告是否已載入並顯示激勵影片廣告。

  • 處理廣告完成事件並向用戶提供指定的獎勵。

示例

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;

public class MainActivity extends AppCompatActivity {

   private RewardedAd rewardedAd;
   private Button showAdButton;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Initialize the Mobile Ads SDK
      MobileAds.initialize(this, initializationStatus -> {
      });

      // Load the rewarded video ad
      loadRewardedAd();

      showAdButton = findViewById(R.id.showAdButton);
      showAdButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // Show the rewarded video ad
            showRewardedAd();
         }
      });
   }

   private void loadRewardedAd() {
      AdRequest adRequest = new AdRequest.Builder().build();

      RewardedAd.load(this, "YOUR_AD_UNIT_ID",
            adRequest, new RewardedAdLoadCallback() {
               @Override
               public void onAdLoaded(RewardedAd ad) {
                  rewardedAd = ad;

                  rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                     @Override
                     public void onAdShowedFullScreenContent() {
                        // Ad showed fullscreen content
                     }

                     @Override
                     public void onAdFailedToShowFullScreenContent(AdError adError) {
                        // Ad failed to show fullscreen content
                     }

                     @Override
                     public void onAdDismissedFullScreenContent() {
                        // Ad dismissed fullscreen content
                        // Preload the next rewarded video ad
                        loadRewardedAd();
                     }
                  });
               }

               @Override
               public void onAdFailedToLoad(LoadAdError loadAdError) {
                  // Rewarded video ad failed to load
                  Log.e("MainActivity", "Rewarded video ad failed to load: " + loadAdError.getMessage());
               }
            });
   }

   private void showRewardedAd() {
      if (rewardedAd != null) {
         rewardedAd.show(this, new OnUserEarnedRewardListener() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
               String rewardType = rewardItem.getType();
               int rewardAmount = rewardItem.getAmount();
               Toast.makeText(MainActivity.this, "Earned " + rewardAmount + " " + rewardType, Toast.LENGTH_SHORT).show();
            }
         });
      } else {
         Log.e("MainActivity", "Rewarded video ad is not ready yet.");
      }
   }
}

輸出

AdMob中介

AdMob中介使開發者能夠從多個廣告網路顯示激勵影片廣告。透過整合AdMob中介,開發者可以使用AdMob作為主要廣告網路,同時透過介面卡整合其他網路。此方法透過使廣告來源多樣化並利用不同網路的優勢來最大限度地提高收入潛力。

演算法

  • 將AdMob SDK整合到Android專案中並配置中介設定。

  • 為需要中介的其他廣告網路實現介面卡。

  • 初始化中介網路SDK並配置其各自的廣告單元ID。

  • 使用中介配置載入激勵影片廣告。

  • 當用戶觸發獎勵操作時,檢查廣告是否已載入並顯示激勵影片廣告。

  • 處理廣告完成事件並向用戶提供指定的獎勵。

示例

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;

public class AdMobMediationExample {
   private RewardedVideoAd rewardedVideoAd;

   public void initializeAdMob() {
      MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
      rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
      rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
         @Override
         public void onRewardedVideoAdLoaded() {
            // Rewarded video ad loaded successfully
         }

         // Implement other listener methods for ad events
      });
   }

   public void loadRewardedVideoAd() {
      // Load rewarded video ad from AdMob and other mediated networks
   }

   public void showRewardedVideoAd() {
      if (rewardedVideoAd.isLoaded()) {
         rewardedVideoAd.show();
      } else {
         // Rewarded video ad not loaded
      }
   }

   // Other methods for handling rewards and ad events
}

// Main program
public class Main {
   public static void main(String[] args) {
      AdMobMediationExample example = new AdMobMediationExample();
      example.initializeAdMob();
      example.loadRewardedVideoAd();
      example.showRewardedVideoAd();
   }
}

輸出

結論

在本教程中,將Google AdMob激勵影片廣告整合到Android應用中,為開發者提供了一種強大的變現策略,使他們能夠在為使用者提供引人入勝的廣告體驗的同時獲得收入。

無論是透過AdMob原生高階廣告、Google移動廣告SDK還是AdMob中介,開發者都可以利用Google龐大的廣告網路並自定義其廣告實現,以增強其應用的變現潛力。透過無縫地整合激勵影片廣告,開發者可以在其Android應用中在使用者滿意度和財務成功之間取得平衡。

更新於:2023年7月27日

瀏覽量:558

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.