Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ad is not visible. Not refreshing ad. after screen off

Tags:

android

admob

I noticed I get the following messages after the screen has been off:

I/Ads﹕ Ad is not visible. Not refreshing ad.
I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.

I have the following code inside onResume:

    if (adView != null) {
        adView.resume();
    }

I stepped the code and it does get called just fine. It also works just fine if I get out of the activity and back in, the only issue appears to be when coming back after the screen goes off.

Anyone else with this issue?

Thanks.

EDIT: The code I use to create the adview and request

    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(id);
    LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);
    layout.addView(adView);
    createAdRequest();

The code of createAdRequest():

    AdRequest.Builder builder = new AdRequest.Builder();
    builder.addKeyword(somekeyword);
    AdRequest adRequest = builder.build();
    adView.loadAd(adRequest);

I have noticed something else that is odd, I sometimes call createAdRequest again if I notice the keyword has changed, after I make that call the ads seem to stop refreshing as well but this time I don't see any visibility messages, I just don't see anymore requests until I call createAdRequest again.

like image 678
casolorz Avatar asked Mar 23 '14 16:03

casolorz


Video Answer


4 Answers

Did you add code to your onPause and onDestroy as well?

@Override
protected void onPause() {
    adView.pause();
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    adView.resume();
}
like image 183
Amy McBlane Avatar answered Nov 16 '22 00:11

Amy McBlane


Ok. I am nothing if not persistent. I tested with this and as a caveat I have been using these flags the whole time. I haven't removed the flags but now with this broadcast receiver it works properly and seems to isolate the issue even more. I will explain.

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON | // In onCreate
                     LayoutParams.FLAG_DISMISS_KEYGUARD |
                     LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                     LayoutParams.FLAG_TURN_SCREEN_ON);

First of all I already had the receiver for another purpose. And as you can see my flags turn my screen back on but I also have a mechanism that will start the activity for me if needed(Which will need to be tweaked now because of this bug). So the fact that this works as is with all of my other mentioned mechanisms seems to imply a timing issue where AdView checks for the screens status once then doesn't check after it is back on. This was my original suspicion but as you can see the onRecieve method returns void so I can't take responsibility. Not sure if I can completely blame the Author of AdView either since I have seen some weird behavior from this intent before. It might even be a problem with the Activitys' ability to handle the intent too.

Anyway sorry if I am rambling the take away here is that I implemented the receiver to finish the activity when the screen is turned off. And I don't personally worry about starting it back up but if you need to then I added a comment to the code for that too.

[Edit] I will make this a little clearer for the most likely scenario of the Activity context. I was using a Service so that is why mine looked different. I haven't tested this one but it should work the same except I am not sure how you would check if the activity can be finished. Not even sure if that matters. My tested working code will be in my edits though if you need them as a reference.

[Edit] I did end up using this in the Activity context and it worked as expected. If the KeyGuard is not secure. Read comments for full story.

class MyActivity extends Activity {

    ScreenReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstance){
        receiver = new ScreenReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy(){
        unregisterReceiver(receiver);
    }

    private class ScreenReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(getClass().getSimpleName(), "Got " + intent.getAction());
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                MyActivity.this.finish();
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // context.startActivity(new Intent(context, MyActivity.class));
            }
        }
    }
}
like image 20
Brent Avatar answered Nov 15 '22 22:11

Brent


I had this problem. In my case the size of the banner (adSize) was greater than the available screen area. Make sure you do not have conflicting layouts.

like image 31
CJD Avatar answered Nov 16 '22 00:11

CJD


Admob will not refresh unless the ad is visible to user. Obviously when your phone turns off no one can see ads, thus there is no reason for Admob to request another ad.

I personally faced the same problem a couple of times. Once I was placing AdView bellow a ListView. The AdView was not visible until the list was scrolled to the bottom, therefore Admob was constantly logging "Ad is not visible. Not refreshing ad."

Another time, I had no internet connection and the same message was continuously being logged.

like image 41
Darush Avatar answered Nov 15 '22 22:11

Darush