This is my reward video code. What i want to do is a button with every click loads a reward video. The problems that faced me are, first the reward video loading takes much time to load and second the reward video sometimes is requested only once so what is the right way to start a reward video on every click with no delay?
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(MainActivity.this);
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
#Override
public void onRewarded(RewardItem reward) {
}
#Override
public void onRewardedVideoAdLeftApplication() {}
#Override
public void onRewardedVideoAdClosed() {}
#Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {}
#Override
public void onRewardedVideoAdOpened() {}
#Override
public void onRewardedVideoStarted() {
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
}
});
and this is my button on click code
if (rewardedVideoAd.isLoaded()){
rewardedVideoAd.show();
}
i can shows video reward ads (only test ads) using below code.
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getResources().getString(R.string.app_id));
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
//getResources().getString(R.string.Video_reward_ad_unit_id)
mRewardedVideoAd.setRewardedVideoAdListener(this);
findViewById(R.id.btVideo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.show();
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
#Override
protected void onStart() {
loadTestRewardedVideoAd();
MobileAds.setAppMuted(true);
super.onStart();
}
private void loadTestRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
Log.i("onRewardedVideoAdLoaded", "Ad Loaded");
}
#Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "Ad OPEPED Now ", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewarded(RewardItem reward) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
Log.i("onRewardedVideoAdLeft", "END");
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
Log.i("onRewardedVideoAdLeft", "END");
}
but this is only works with test device not on real device.
Related
I am new in android studio, I have this code for showing interstitial.
Now I want to add timer before showing interstitial ad according admob policy.
but no idea how to add this. can someone help me out to do this?
private void goMainScreen(int position) {
if (mInterstitialAd != null) {
mInterstitialAd.show(this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdClicked() {
super.onAdClicked();
}
#Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(MainActivity.this, SignsActivity.class));
Pref.getInstance(MainActivity.this).setIndex(position);
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
startActivity(new Intent(MainActivity.this, SignsActivity.class));
Pref.getInstance(MainActivity.this).setIndex(position);
}
#Override
public void onAdImpression() {
super.onAdImpression();
}
#Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
}
});
} else {
startActivity(new Intent(MainActivity.this, SignsActivity.class).putExtra("index", position));
Pref.getInstance(MainActivity.this).setIndex(position);
}
adPosition = adPosition + 1;
}
I'm trying to load interstitial ad on the tablet, with the test ad id. On output ad is not showing, but I'm getting the callback on "onAdLoaded".
This is my code:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_activity);
mPublisherInterstitialAd = new PublisherInterstitialAd(this);
mPublisherInterstitialAd.setAdUnitId("/6499/example/interstitial");
mPublisherInterstitialAd.loadAd(newPublisherAdRequest.Builder().build());
mPublisherInterstitialAd.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
Log.d("AD ","LOADED");
}
#Override
public void onAdFailedToLoad(int i) {
Log.d("AD ","FAILED");
}
});
}
Try this
public void inrequestadd() {
mInterstitial = new InterstitialAd(MainActivity.this);
mInterstitial.setAdUnitId("Your ID");
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
}
#Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
}
Here is how I handle this kind of code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAds();
}
private void initAds() {
MobileAds.initialize(this, "your app id");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("your ad unit ID");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
Also in the manifest file:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="your app id"/>
Next, depending on the logic of your app, you would have something like:
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
System.out.println("The interstitial wasn't loaded yet. Do some other action");
}
Every time when i run the code its call onRewardedVideoAdFailedToLoad(int i) function. I am using codelabs example.
Sorry for bad English and thanks for helping me.
here is my code. I used many tutorial .
Is there anything I did it in the wrong way?
Or I just missed some steps?
public class Rewardedvideo extends AppCompatActivity implements
RewardedVideoAdListener {
RewardedVideoAd mAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rewardedvideo);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-
3940256099942544~3347511713");
mAd=MobileAds.getRewardedVideoAdInstance(Rewardedvideo.this);
mAd.setRewardedVideoAdListener(this);
loadrewardedvideoAd();
}
public void loadrewardedvideoAd(){
if(!mAd.isLoaded()){
mAd.loadAd("ca-app-pub-3940256099942544/5224354917",new
AdRequest.Builder().build());
}
}
public void startvideo(){
if(mAd.isLoaded()){
mAd.show();
}
else {
mAd.loadAd("ca-app-pub-3940256099942544/5224354917",new
AdRequest.Builder().build());
mAd.show();
}
}
#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();
startvideo();
}
#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 onResume() {
mAd.resume(this);
super.onResume();
}
#Override
public void onPause() {
mAd.pause(this);
super.onPause();
}
#Override
public void onDestroy() {
mAd.destroy(this);
super.onDestroy();
}
}
here is my code. I used many tutorial .
Is there anything I did it in the wrong way?
Or I just missed some steps?
I think this is not get video source.
try again after few time
Example : What I want When you open the application, the ad appears within 5 seconds , if different , an exhibition will be canceled and the application Proceed paragraph the main activity.the code I am using is this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitialAd = new InterstitialAd(Main.this);
interstitialAd.setAdUnitId(getString(R.string.adMobInter));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.adMob_test))
.build();
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
onMain();
}
#Override
public void onAdClosed() {
onMain();
}
});
}
public void displayInterstitial() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
public void onMain() {
setContentView(R.layout.main);
}
I want to add an interstitial ad on my game just before the game starts.
When you run the app, you have 3 diferent game modes so when you select one, the game starts on that mode. The idea is to show the interstitial after you press the button, just before the game starts.
I have followed the guide on the android developers page, here, but this doesn't fit exactly what I need. I have modified a bit to fit with my code, but the interstitial is not shown before starting the game, it is shown when you finish the game and returning to mainActivity.
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1Player = (ImageButton) findViewById(R.id.OnePlayerImgBtn);
btnVersus = (ImageButton) findViewById(R.id.VersusImgBtn);
btnLocalMultiP = (ImageButton) findViewById(R.id.LCLMultiPlayerImgBtn);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.ad_intersticial_1_id));
requestNewInterstitial();
btn1Player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(false,true,MODE_SINGLE);
}
});
btnVersus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(false,true,MODE_VERSUS);
}
});
btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(true,true,MODE_LOCALMULTI);
}
});
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
public void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
String singlemode ="Off";
String versus ="Off";
String multi ="Off";
btn1Player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String singlemode ="On";
}
});
btnVersus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String versus ="On";
}
});
btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String multi="On";
}
});
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
if singlemode.equals("On");
{startGame(false,true,MODE_SINGLE);}
if versus.equals("On");
{startGame(false,true,MODE_VERSUS);}
if multimode.equals("On");
{startGame(true,true,MODE_LOCALMULTI);}
requestNewInterstitial();
}
});