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.
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();
}
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));
}
}
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With