Admob closes app after closing ad? - android

I have one Activity in my app. Now I wanted to add an Admob interstitial banner. Unfortunately the old activity is also closed when the user closes the ad.
I added something like this:
interstitial = new InterstitialAd(this, "*************");
// Create ad request
AdRequest adRequest = new AdRequest();
// and begin loading your interstitial
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
And started the ad by using
if (interstitial.isReady()){
interstitial.show();
}
The representation of the app is working fine.
What can I do to solve the problem?

There is an attribute in your AndroidManifest.xml file, like android:noHistory="true" You must delete this. it solves the problem

Do like this
private boolean isAddShown = false;
make this isAddShown = true when the add is visible
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(!isAddShown){
super.onBackPressed();
}else{
isAddShown = false;
}
}

Related

Android Full Screen Ad (Interstitial) Won't Show When Screen is Locked

I'm new to Android development and I'm creating an Alarm app that includes ads. A logical place I wanted to include a full screen ad was immediately after the user dismisses an alarm.
The problem I'm encountering is that the user's lock screen is typically active when the alarm goes off. My activity and UI are displaying fine because I'm using the WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED flag but when I call interstialAd.show() the lock screen pops up and the ad is hidden behind it. This is a pretty awful user experience because there is no real indication that an Ad has been loaded until the next time he/she decides to unlock their phone at which point it simply appears out of nowhere.
My code:
I initialize the ad in my activity's onCreate() method:
if (BuildConfig.FLAVOR.equals("free")) {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.alarm_dismissed_interstitial_ad_id));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// ad closed or finished so we're done here.
Log.d(TAG, "onAdClosed: ad closed... finishing activity");
if (Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else finish();
}
});
if (!mAdLoadStarted) {
requestNewInterstitial();
}
}
requestNewInterstitial():
private void requestNewInterstitial(){
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitialAd.loadAd(adRequest);
mAdLoadStarted = true;
}
I call the show method once the user has dismissed the alarm:
if (BuildConfig.FLAVOR.equals("free")) {
// The dismiss sequence has finished so we can display the full screen ad.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d(TAG, "onDismissFinished: Ad wasn't displayed because it didn't load in time.");
}
}else{
// No ads for the paid version, we're done here
if (Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else finish();
}
I would guess that interstialAd.show() creates a new window/activity without the FLAG_SHOW_WHEN_LOCKED.
You have to display the ad using a window that has this flag set too or in the same window you were displaying your content to the user.

Interstitial Ads not showing admob plugin for unity

I am trying to put an interstitial ad after every 5 games the user plays. What I've done is created an empty object with the following script:
using UnityEngine;
using GoogleMobileAds.Api;
public class AdBetweenGames : MonoBehaviour {
InterstitialAd interstitial;
void Start()
{
if(PlayerPrefs.GetInt("games-played", -1) % 5 == 0)
{
this.ShowInterstitial();
}
}
private void RequestInterstitial()
{
string adUnitId = "UNITID";
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
public void ShowInterstitial()
{
RequestInterstitial();
Debug.Log("Interstatial ad called.");
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
I am sure the code here is called properly based on the debug log. I've also tried doing banner ads and they show without a problem after 1-2 seconds. I am making an android game.
Also I was thinking, could it be that the ad needs some extra time to load and I am clicking try again too early. But then what would a solution be?
The interstitial ad of ad mob need time to load try it in update and wait for at least 4 sec and call ShowInterstitial function inside a bool set its value true in start and false it after calling the function to show ad once.

Getting an Interstitial Ad to Display from a Fragment

i've been searching for hours now, and i can't get a simple interstitial ad to display. the layout is within a fragment and i'm sure that's part of the problem, but i can't find a work around. any help is greatly appreciated - most of what i see is displaying the ad within an activity.
I want the interstitial to appear before the user clicks on a button in an app. i'm sure the issue is with the (this) but I can't find anything.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("example ad unit");
mapFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Constants.MAPS_INTENT_URI));
startActivity(intent);
}
});
Yes the problem is with the this. Since you are calling it in a Fragment, it won't work. Because this is the constructor from the docs:
public InterstitialAd (Context context)
The constructor requires the context. So change your code like this:
mInterstitialAd = new InterstitialAd(getContext());
To avoid NullPointerException, you can use requireContext()
mInterstitialAd = new InterstitialAd(requireContext());

How to stop Banner Ad loading?

Some user intentionally try to click banner ads many times.Due to this we face problem of account suspension or termination. Does anyone know how to stop the ad being loading if it cross some limit(for example 3).
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
if(currentbannerclick>3)
{
// some code to not load the ad.
}
LinearLayout id=container
AdView id=adView
if(currentbannerclick>3)
container.removeView(adView);
Thank you everyone for your answer.
You can identify if Ad is clicked using Activity life-cycle callbacks. you can then find out how much time user clicked your Ad and call adView.loadAd(adRequest); only if the user clicked your Ad less than your threshold.
You can attach an AdListener to your AdView and increase your click counter in the onAdLoaded or onAdOpened methods. More info here: https://developers.google.com/android/reference/com/google/android/gms/ads/AdListener#public-methods
This should work:
private void loadAd() {
// This is a one element array because it needs to be declared final
// TODO: you should probably load the default value from somewhere because of activity restarts
final int[] currentBannerClick = {0};
final AdView adView = (AdView) findViewById(R.id.adView);
adView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
super.onAdOpened();
currentBannerClick[0]++;
if (currentBannerClick[0] > 3) {
adView.setVisibility(View.INVISIBLE);
adView.destroy();
}
// TODO: save currentBannerClick[0] somewhere, see previous TODO comment
}
});
if (currentBannerClick[0] <= 3) {
AdRequest adRequest = new AdRequest.Builder().addTestDevice(YOUR_DEVICE_ID).build();
adView.setVisibility(View.VISIBLE);
adView.loadAd(adRequest);
} else {
adView.setVisibility(View.INVISIBLE);
}
}
You can also limit number of Ads displayed for user in AdMob System. You can set limit of 3 ads per user per minut, hour or day.

AdMob interstitial doesn't show on Android

I have an Android app and I'm trying to add AdMob interstitials into it. I create them this way
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.ad));
interstitial.loadAd(AdUtils.createRequest());
and when the moment is appropriate, I show them like this:
if(interstitial.isLoaded()) {
interstitial.show();
}
When I test this code on the emulator, everything is fine. But when I run it on the real device, it often (about a half of all shows) displays only a black screen with the close button and no advertisement image at all. Is there any solution to this issue?
You can try as below.
Declare this as a variable.
private InterstitialAd interstitial;
then use this part in your onCreate method
interstitial = new InterstitialAd(this, "your id");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
and at last set this outside your onCreate method so basically set below code in your activity..
#Override
public void onReceiveAd(Ad ad) {
// TODO Auto-generated method stub
if (ad == interstitial) {
interstitial.show();
}
}
Hope this will help you.

Categories

Resources