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

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.

Related

How to measure size (KB) of an interstitial received from publicity SDK

First i apologyse for my poor english.
Our company is using a publicity SDK wich is responsible for showing interstitial at launch, during splashscreen. We don't have access directly to the view that is loaded. After initialization with app id in Application class the basic workflow is :
private MNGAdsFactory mngAdsBannerAdsFactory;
// init intertitial factory
mngAdsInterstitialAdsFactory = new MNGAdsFactory(this);
// set intertitial listener
mngAdsInterstitialAdsFactory.setInterstitialListener(this);
if (mngAdsInterstitialAdsFactory.createInterstitial()) {
//Wait callBack from interstitial listener
}else{
//adsFactory can not handle your request
}
And the success callback
#Override
public void interstitialDidLoad() {
Log.d(TAG, "interstitial did load");
}
Here is my problem. I want to measure the size (KB) of the interstitial displayed on screen in order to monitor some abuses.
Is there a way to do that?
Thank you in advance.

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.

Android AdView not showing

I'm having issues with ADs in my Android App.
Sometimes the AD is not shown, probably depending on the DPI or Size of the screen's device.
In my code, I implemented a check to set View.GONE in case the AD fails to load, but despite that on some devices I still have blank spaces where it's supposed to be the AD
For example, here one of my ADs
if(holder.nativeExpressAdView != null) {
AdRequest request = new AdRequest.Builder()
.build();
holder.nativeExpressAdView.loadAd(request);
Utilities.setAdListenerNative(holder.nativeExpressAdView);
}
//ADs listener to remove view if it fails load
//NATIVE ADS
public static void setAdListenerNative(final NativeExpressAdView nativeExpressAdView) {
nativeExpressAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
nativeExpressAdView.setVisibility(View.GONE);
}
});
}
First show me your log
Second Dont use "Gone" because this will change the position of the adView if you are using other sizes of Android scale you will see that.
Third use a debugger.
Use "invisible"

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.

Admob closes app after closing ad?

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;
}
}

Categories

Resources