gpt4 book ai didi

java - 如何使用 admob 在 android 中集成奖励视频广告?

转载 作者:行者123 更新时间:2023-11-29 19:20:53 26 4
gpt4 key购买 nike

我想使用 admob 在我的 android 项目中添加奖励视频广告,我通过奖励视频将我的 android 应用程序货币化。我点击了这个链接 integrate rewarded video ad

这是mainActivity代码:

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);

}

private void loadRewardedVideoAd() {
mAd.loadAd(String.valueOf(R.string.ad_key), new AdRequest.Builder().addTestDevice("device id").build());
Toast.makeText(this, "called", Toast.LENGTH_SHORT).show();

}
public void load(View view){
loadRewardedVideoAd();
}


@Override
public void onRewardedVideoAdLoaded() {
if (mAd.isLoaded()) {
Toast.makeText(this, "is Loaded", Toast.LENGTH_SHORT).show();
mAd.show();
}else{
Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();

}
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}
@Override
public void onResume() {
mAd.resume(this);
super.onResume();
}

@Override
public void onPause() {
mAd.pause(this);
super.onPause();
}

@Override
public void onDestroy() {
mAd.destroy(this);
super.onDestroy();
}
}

这是我的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

当我运行我的代码时它显示

02-28 18:30:19.573 21418-21429/mainscreen.udeveloper.com.rewardedvideo W/Ads: There was a problem getting an ad response. ErrorCode: 1

02-28 18:30:19.583 21418-21418/mainscreen.udeveloper.com.rewardedvideo W/Ads: Failed to load ad: 1

我在谷歌上搜索了很多奖励视频广告教程,但我没有找到任何教程,请帮助我。如果我应该使用另一个奖励视频广告网络,请指导我好的教程。

最佳答案

Use this Id for Testing : - ca-app-pub-4824494878097656/8403117409

package com.safal.bhatia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

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

public class Demo extends AppCompatActivity implements RewardedVideoAdListener {

private RewardedVideoAd mRewardedVideoAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
rewardAds();
}
private void rewardAds() {
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-4824494878097656/8403117409",//use this id for testing
new AdRequest.Builder().build());

}

@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "Download to Earn" + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();

// Reward the user.
}

@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}

@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();`enter code here`
}

}

关于java - 如何使用 admob 在 android 中集成奖励视频广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com