admob 广告代码参考 安卓 奖励视频
来源:互联网 发布:传奇霸业魔麒麟数据 编辑:程序博客网 时间:2024/05/22 12:35
Initialize rewarded video ads
package ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardedVideoAd;
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
}
...
}
MobileAds.getRewardedVideoAdInstance()
. If your ad placement is configured for medation, this context is passed to mediation adapters, and several adapters require an Activity context to load ads.A RewardedVideoAd
object can be retrieved using MobileAds.getRewardedVideoAdInstance()
.
To be notified of rewarded video lifecycle events, you must implementRewardedVideoAdListener
. The rewarded ad listener is set using thesetRewardedVideoAdListener()
method. This listener is automatically notified of events from all the networks you're mediating. For example, you are notified of a user being rewarded for watching a video through the onRewarded()
method on RewardedVideoAdListener
.
Request rewarded video ad
Note: Make all calls to the Mobile Ads SDK on the main thread.@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
Adhering to the singleton design of RewardedVideoAd
, requests to load an ad should be mRewardedVideoade to the shared instance.
It is highly recommended to call loadAd()
as early as possible (for example, in the onCreate()
method of your Activity) to allow videos to be preloaded.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for Android rewarded video:
ca-app-pub-3940256099942544/5224354917
It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
Set up event notifications
The SDK provides the RewardedVideoAdListener
interface, which has methods corresponding to the events in a rewarded video ad's lifecycle. Have your app define a class that implements this interface and pass it to setRewardedVideoAdListener
prior to loading an ad.
The code example in Initialize rewarded video ads already shows how to declare that your class implements RewardedVideoAdListener
and set the listener on the RewardedVideoAd
object. Here is a sample implementation of the listener methods:
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + 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();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
Display an ad
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
We recommend that you ensure a rewarded video ad has completed loading before attempting to display it. The isLoaded()
method indicates if the rewarded video ad request has been successfully fulfilled.
Forward lifecycle events
To forward the parent Activity's lifecycle events to the RewardedVideoAd
object, call the resume()
, pause()
, and destroy()
methods in the parent Activity's onResume()
, onPause()
, and onDestroy()
methods respectively.
Here is an example of Activity lifecycle event forwarding:
@Override
public void onResume() {
mRewardedVideoAd.resume(this);
super.onResume();
}
@Override
public void onPause() {
mRewardedVideoAd.pause(this);
super.onPause();
}
@Override
public void onDestroy() {
mRewardedVideoAd.destroy(this);
super.onDestroy();
}
Additional resources
Samples on GitHub
- Rewarded Video sample app: Java | Kotlin
Mobile Ads Garage video tutorials
- Rewarded Video Implementation
- Rewarded Video in Unity
Codelab
- Add Rewarded Video Ads to your Android App
Next steps
- Create your own rewarded video ad unit in the AdMob UI.
- Try another ad format:
- Interstitials
- Banners
- Native Express
- admob 广告代码参考 安卓 奖励视频
- admob 广告代码参考 iOS 奖励视频
- admob 广告代码参考 安卓 弹窗广告
- admob广告代码参考 安卓 banner广告
- admob 广告代码参考 iOS banner广告
- 移动广告工厂:Unity中的AdMob奖励视频
- admob 广告代码参考 iOS 弹窗广告
- 如何测试您的AdMob奖励广告?
- AdMob奖励广告的入门指南
- U3D Admob广告代码
- android安卓 谷歌google admob广告添加
- Unity3d在IOS与安卓系统接入Admob广告教程
- 安卓APP无法显示admob谷歌广告,显示Received error HTTP response code: 400
- 游戏中集成unity admob视频广告中文教程
- AdMob通过奖励视频实现用户体验优先的三种方式
- Android Studio工程中添加Admob广告条代码详解
- Android Studio工程中添加Admob广告条代码详解
- android(安卓)代码规范参考指南
- 查找算法-顺序查找(顺序表的普通实现和优化实现)
- 获取字符串中每一个字母出现的次数
- 订单列表页
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- Cause: org.xml.sax.SAXParseException
- admob 广告代码参考 安卓 奖励视频
- ubuntu安装teamviewer
- 购物车详情界面点击加入购物车(四)
- XMONE
- 据结构实验之查找四:二分查找
- Delphi整理三(窗体和基本组件)
- iOS开发网络篇 一一 NSURLSession发送GET、POST请求
- gouwuche的presenter
- [恒泰佰酷] Django 在后台添加 list中添加 链接 changelist view