如何在Android中集成Admob Rewarded Ads?

时间:2016-10-06 12:35:09

标签: android admob

我正在努力与admob rewarded ads整合。我试过谷歌教程,但无法实现我想要的。

请建议我使用tutorial(更喜欢视频教程)将admob rewarded广告整合到Android中。

3 个答案:

答案 0 :(得分:3)

    public class YourActivity extends AppCompatActivity implements  RewardedVideoAdListener     


    RewardedVideoAd mAd = MobileAds.getRewardedVideoAdInstance(this);
    mAd.setRewardedVideoAdListener(this);
    loadRewardedVideo();

    private void loadRewardedVideo() {
    mAd.loadAd(getString("YOUR_AD_UNIT_ID"),
            new AdRequest.Builder()
                    .build());
    }

以下是听众:

@Override
public void onRewardedVideoAdLoaded() {
    Log.i(TAG, "Rewarded: onRewardedVideoAdLoaded");
    try {
        if (mAd.isLoaded()) {
            mAd.show();
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

@Override
public void onRewardedVideoAdOpened() {
    Log.i(TAG, "Rewarded: onRewardedVideoAdOpened");
}

@Override
public void onRewardedVideoStarted() {
    Log.i(TAG, "Rewarded: onRewardedVideoStarted");
}

@Override
public void onRewardedVideoAdClosed() {
    Log.i(TAG, "Rewarded: onRewardedVideoAdClosed");
}

@Override
public void onRewarded(RewardItem rewardItem) {
    Log.i(TAG, "Rewarded:  onRewarded! currency: " + rewardItem.getType() + "  amount: " +
            rewardItem.getAmount());

}

@Override
public void onRewardedVideoAdLeftApplication() {
    Log.i(TAG, "Rewarded: onRewardedVideoAdLeftApplication ");
}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {
    Log.i(TAG, "Rewarded: onRewardedVideoAdFailedToLoad: " + i);

}

答案 1 :(得分:2)

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mAd;
@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.
    mAd = MobileAds.getRewardedVideoAdInstance(this);
    mAd.setRewardedVideoAdListener(this);
    loadRewardedVideoAd();

    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mAd.isLoaded()) {
                mAd.show();
            }
        }
    });

}

@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();
}

private void loadRewardedVideoAd() {
    mAd.loadAd("ca-app-pub-3940256099942544/5224354917",
       new AdRequest.Builder().build());
}

@Override
public void onRewardedVideoAdLoaded() {

}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {

}

@Override
public void onRewarded(RewardItem rewardItem) {

}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

答案 2 :(得分:-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();
    }

}