您需要做的就是实现所有必需的方法,例如 onResume、onPause。
看看我的活动课:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 AdActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
requestNewRewardVideo();
}
protected void onResume() {
super.onResume();
mRewardedVideoAd.resume(this);
}
@Override
protected void onPause() {
super.onPause();
mRewardedVideoAd.pause(this);
}
@Override
public void onDestroy() {
mRewardedVideoAd.destroy(this);
super.onDestroy();
}
private void requestNewRewardVideo() {
//load rewared video
mRewardedVideoAd.loadAd("ca-app-pub-xxxxxxxxxxxxxxxxxx",
new AdRequest.Builder().build());
}
@Override
public void onRewardedVideoAdLoaded() {
mRewardedVideoAd.show();
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
//request for new reward video
requestNewRewardVideo();
}
@Override
public void onRewarded(RewardItem rewardItem) {
// reward your user here
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
}
应用程序构建.gradle
实施 'com.google.android.gms:play-services-ads:11.4.2'
单击按钮时显示广告:
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}