问题
I've been looking for an example, but didn't found it.
I found one without libgdx and some why it didn't work, but app also didn't crash.
public class AndroidLauncher extends AndroidApplication implements AdHandler, RewardedVideoAdListener{
...
RewardedVideoAd rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(this);
rewardedSetup();
private void rewardedSetup(){
if (!rewardedVideoAd.isLoaded()){
rewardedVideoAd.loadAd("ca-app-pub-683510441556959xxxxxxxxx", new AdRequest.Builder().build());
}
@Override
public void onRewardedVideoAdLoaded() {
if(rewardedVideoAd.isLoaded()){
rewardedVideoAd.show();
}
}
@Override
public void onRewarded(RewardItem rewardItem) {
rewarded(true);
}
}
}
And it simply don't shows, maybe it happened to anyone ? (My true code is full of handlers and stuff, thus I didn't put it all, I guess other code has 0 effect on this problem)
回答1:
private static final String APP_ID="ca-app-pub-XXXXXX~XXXXX";
private static final String AD_UNIT_ID="ca-app-pub-XXXXX/XXXXXXX";
private RewardedVideoAd mAd;
private boolean isRewardLoaded;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
MobileAds.initialize(this, APP_ID);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
isRewardLoaded =true;
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
// call rewards method from here.
loadRewardedVideoAd(); // Load for next Reward Point
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
});
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
isRewardLoaded=false;
mAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().build());
}
@Override
protected void onResume() {
super.onResume();
mAd.resume(this);
}
@Override
protected void onPause() {
super.onPause();
mAd.pause(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
mAd.destroy(this);
}
set-up done, need two methods that is implementation of AdHandler
, these two methods will call from core
module.
public void showVideoAd(){
runOnUiThread(new Runnable() {
public void run() {
if (mAd.isLoaded()) {
mAd.show();
} else {
loadRewardedVideoAd();
}
}
});
}
public boolean hasVideoReward(){
return isRewardLoaded;
//return mAd.isLoaded(); // -> must be called on the main UI thread.
}
Call hasVideoReward()
, that tell you to show reward panel or not and showVideoAd()
method show Reward Video.
Hopefully you've these entries in your AndroidManifest.xml
file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
来源:https://stackoverflow.com/questions/44397952/rewarded-video-ad-example-in-libgdx