Điều kiện tiên quyết
- SDK quảng cáo trên thiết bị di động của Google phiên bản 19.7.0 trở lên.
- Làm theo Hướng dẫn bắt đầu sử dụng để nhập SDK quảng cáo trên thiết bị di động của Google và cập nhật tệp kê khai Android của bạn.
Luôn kiểm tra bằng quảng cáo thử nghiệm
Khi tạo và thử nghiệm ứng dụng, hãy đảm bảo rằng bạn sử dụng quảng cáo thử nghiệm thay vì quảng cáo đang chạy thực tế. Chúng tôi sẽ tạm ngưng tài khoản của bạn nếu bạn không thực hiện việc này.
Cách dễ nhất để tải quảng cáo thử nghiệm là sử dụng mã đơn vị quảng cáo thử nghiệm dành riêng cho quảng cáo có tặng thưởng trên Android:
ca-app-pub-3940256099942544/5224354917
Mã này được thiết lập đặc biệt để trả về quảng cáo thử nghiệm cho mọi yêu cầu, và bạn có thể sử dụng mã này trong ứng dụng của mình khi lập trình, thử nghiệm và gỡ lỗi. Bạn chỉ cần nhớ thay thế mã này bằng mã đơn vị quảng cáo của mình trước khi xuất bản ứng dụng.
Để biết thêm thông tin về cách hoạt động của quảng cáo thử nghiệm bằng SDK quảng cáo trên thiết bị di động, hãy xem bài viết Quảng cáo thử nghiệm.
Tải một đối tượng quảng cáo có tặng thưởng
Bạn có thể tải một quảng cáo có tặng thưởng bằng cách sử dụng phương thức
load()
tĩnh trên lớp
RewardedAd
và chuyển vào RewardedAdLoadCallback
. Điều này thường
được thực hiện trong phương thức onCreate()
của Activity
.
Xin lưu ý rằng giống như các lệnh gọi lại tải định dạng khác, RewardedAdLoadCallback
tận dụng LoadAdError
để cung cấp thông tin chi tiết về lỗi có độ trung thực cao hơn.
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 } }) } }
Đặt lệnh FullScreenContentCallback
FullScreenContentCallback
xử lý các sự kiện liên quan đến việc hiển thị
RewardedAd
của bạn. Trước khi bạn hiển thị RewardedAd
, hãy nhớ đặt
lệnh gọi lại như sau:
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.") } }
Hiển thị quảng cáo
Khi hiển thị một quảng cáo có tặng thưởng, bạn sẽ sử dụng đối tượng OnUserEarnedRewardListener
để xử lý các sự kiện tặng thưởng.
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.") }
Câu hỏi thường gặp
- Lệnh gọi khởi chạy có thời gian chờ không?
- Sau 10 giây, SDK quảng cáo trên thiết bị di động của Google sẽ gọi
OnInitializationCompleteListener
ngay cả khi mạng dàn xếp chưa hoàn thành quy trình khởi chạy. - Điều gì sẽ xảy ra nếu một số mạng dàn xếp chưa sẵn sàng hoạt động khi tôi nhận được lệnh gọi lại khởi chạy?
Phương pháp hay nhất là tải một quảng cáo bên trong lệnh gọi lại của
OnInitializationCompleteListener
. Ngay cả khi mạng dàn xếp chưa sẵn sàng hoạt động, SDK quảng cáo trên thiết bị di động của Google sẽ vẫn yêu cầu mạng đó cung cấp một quảng cáo. Vì vậy, nếu kết thúc quá trình khởi chạy sau khi hết thời gian chờ, mạng dàn xếp vẫn có thể thực hiện các yêu cầu quảng cáo tiếp theo trong phiên hoạt động đó.Bạn có thể tiếp tục kiểm tra trạng thái khởi động của tất cả bộ chuyển đổi trong toàn bộ phiên hoạt động trong ứng dụng bằng cách gọi
MobileAds.getInitializationStatus()
.- Làm cách nào để biết lý do khiến một mạng dàn xếp cụ thể chưa sẵn sàng hoạt động?
AdapterStatus.getDescription()
cho biết lý do khiến một bộ chuyển đổi chưa sẵn sàng thực hiện các yêu cầu quảng cáo.