Forcing admob to refresh ad every second in Android - android

I have built an android app which has a fixed screen that will remain focused for more than a minute. Kind of a story in the page.
I have integrated Admob adrequest into the application. But it is the single ad that comes on load of the screen and remains till I quit.
Can I do something so that the Ad will change every 1 sec.

You can control the refresh frequency by using the admob account online or you can also set the refresh time in seconds using the API :
AdView.loadAd(new AdRequest());
Even though a call to the API will ask admob server to fetch a new ad, but that doesn't guarantee that a new ad will be fetched. Moreover, 1 second seems to be too short a interval to be refreshing your ads. If I was a user of your app, I would rather un-install it. :) But yes, that's my personal take on it..

I think too that the 1 sec, is very-very short time, for ad change, generate more data for the user (what csan be very annoying), but you know. :) You can set it on the admob's page, when you logged in. Choose your application, then Manage Settings. Here app settings. But it only allow at least 12 seconds, what confirms my thinking. And I think if you use what Anand said, you even won't got ads in every sec.
Hope it helps.

declare adView in activity as data member , then create Timer Task as bellow in your constructor of the activity
adView = (AdView) findViewById(R.id.adView);
TimerTask tt = new TimerTask() {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
adView.loadAd(new AdRequest());
}
});
}
};
Timer t = new Timer();
t.scheduleAtFixedRate(tt, 0, 1000 * 60);

Related

Can I request new ad from onAdFailedToLoad?

During testing of my application, I noticed that I very frequently get ERROR_CODE_NO_FILL for Interstitial Ad. I think that the same situation with real ads might be one of the causes of low income from the application, so I want to increase the rate of showed ads. As the solution, I decided to make requests until I will get some app, so it might take from 5 to 50 requests until I will get out of onAdFailedToLoad. Is it a legitimate way to do this? Won't I get banned by AdMob with such way of getting more ads?
That's what I do in setAdListener in onCreate:
#Override
public void onAdFailedToLoad(int errorCode) {
requestNewInterstitial();
}
And requestNewInterstitial():
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
No, you won't get banned...
You can do this, but don't show the Ad as soon it is loaded, this will be a bombarding of Ads & user won't like that...

High ram usage with admob

I am trying to place ads inside my app. According to Admob Documentation I have to initialize Mobile Ads SDK
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
This causes spike in high ram usage in code.
But if i remove this line then ram usage drops & and this line of code doesn't seem to have any affect on servering ads inside the app.
Also when requesting ad from admob ram usage again spike up and causes 3-4 GC events on app startup. I believe this is memory leak.
Here's how i am requesting ad in onCreate method
AdRequest request = null;
if (BuildConfig.DEBUG) {
//Facebook Audience Network
List<String> testDeviceId = new ArrayList<>();
testDeviceId.add("TESTID");//Redmi Note 3
testDeviceId.add("TESTID");//Moto G 1st Gen
AdSettings.addTestDevices(testDeviceId);
//Google Ad-mob
request = new AdRequest.Builder()
.addTestDevice("TESTID")//Redmi Note 3
.addTestDevice("TESTID")//Mot G 1st Gen
.build();
} else {
request = new AdRequest.Builder()
.build();
}
AdView mAdView = findViewById(R.id.adView);
mAdView.loadAd(request);
When loading this banner ads several GC event are kicked in. If i don't load ads GC event are never kicked in.
Is this behavior normal with admob? How can i resolve this?
Google AdView has WebView with a lot of animation inside. It will heats up all mobile CPU.
AdView take 30% of CPU.
Solution : You can also add custom listeners to destroy after some time and recreate in order to handle it even better. Serverside there is also a parameter telling the app ad how soon should ask for a new ad, I am not sure if it exist in all cases but it is there for DFP accounts.
here is the easiest way i would suggest
new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
if (!isBeingDestroyed) {
final AdRequest adRequest = new AdRequest();
final AdView adView = (AdView) findViewById(R.id.ad);
adView.loadAd(adRequest);
}
}).sendEmptyMessageDelayed(0, 1000);
Here is the link that provide complete solution for this.
Hope it will help you.
Yes, that behaviour is normal. AdView is a dynamic WebView which consumes about 50mb RAM. Most of the memory leaks occur when you rotate screen and instance of previous Activity is attached to an element like listener or thread. Here are some examples. To check whether your app leaks or not you can use LeakCanary or Android Studio.
To check leaks in Android Studio
Start Memory Profiler
Select Memory and "Dump Java Heap"
Export file as .hprof file
Drag .hprof file to Android Studio and look for Analyzer Tasks and push run button to check if your activity leaks.
Your app is still within an acceptable limit of RAM usage for most devices.
You can put android:largeHeap="true" in your AndroidManifest.xml file, so that your users won't get affected.
You are absolutely right. MobileAds.initilize() can be removed, and the application will work, BUT. When you request the first load of ads, the initilize() method will be called under the hood.
I recommend that you call this method as soon as possible. For example, in your Application class. Also, starting from version Admob 21.0.0, you can optimize the initialization process. Just add the following lines to your manifest
<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
android:value="true"/>
<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
android:value="true"/>
i dont know if this will help but if you care about ram usage and you use ads in many activities you could start the ad mob from application class and that case the ad will only initialized once

Admob banner shows only on first launch

After following the official Admob guide for Unity, I successfully got a banner to show up on my game. The problem is, it only shows up the very first time I build the game. If I quit the game and launch it again, it doesn't show. Deleting the game and building from Unity again is the only thing that shows banners.
My code is as follows:
void Start() {
RequestBanner();
}
private void RequestBanner() {
string id = "ca-app-pub-numbers...";
view = new BannerView(id, AdSize.SmartBanner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
view.LoadAd(request);
}
I have tried calling view.Destroy() on application quit or in a coroutine, as well as view.Show() and view.Hide() at various locations with no luck.
How can I make admob show banner consistently?

Android Interstitial ad repeating

In my app I put Interstitial full page ad. And it is working fine on most phones but when I checked in my friends Galaxy Note 1 the Interstitial ad is repeating and reating. I don't know why it is happening in one mobile phone only.. I checked with more than three other mobile phoe including Samsung Galaxy S2, S3 etc... but in those phones there was no problem and I tested in Emulator also.. there was no problem once i closed the Ad it will not repeat. The problem is with this particular phone , if anybody knows the reason please help... Is this the problem with memory or somethiing ..
I am giving the code below.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.tabpmnth_xm);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxx8x/x2xxxxxxxx");
// Create ad request.
AdRequest aadRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("xxxxxxxxxxxxxxxxxxxx")
.build();
// Begin loading your interstitial.
interstitial.loadAd(aadRequest);
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
}
});
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
Sorry Jocheved for the late replay. If this problem is not happening in every phone it could possibly the problem is in that user's mobile settings. Just check whether in his/her " settings > Developer Options > Do not keep activities " selected or not. If it is selected then the app will destroy every activities as soon as the user left the current activity. So when an interstitial is displayed , the current activity will be normally in onPause. but it will be destroyed and when the user close the interstitial and come back again oncreate will run and the ad will display. To avoid this situation you can inform the user to check whether the option is selected or not.. or otherwise u may need to find some other solution and solve it through programming. thanks..
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/i6zOe2Hy-xc
Check out above link where Google admob engineer has explained why this issue is arising for you. Don't need to include displayInterstitial() inside onAdLoaded(). Either you want to display your interstitial after some time, I use onCountdownTimer() myself to achieve this. For e.g. showing an Ad after 5 seconds.
new CountDownTimer(5000, 5) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
displayInterstitial();
}
}.start();

onFailedToRecieveAd(Invalid Ad Request) message with AdMob

I've been trying to get AdMob to work for some time on my app. I keep getting onFailedToRecieveAd(Invalid Ad Request) message in the log. I've paired down my test application to this:
AdView adView;
LinearLayout ll;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adView = new AdView(this, AdSize.BANNER, "pub-2...............");//inserted my 16 digit pub id here
adView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(adView);
setContentView(ll);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("3...............");// 16 digits, tried other strings as
// follows:
//for addTestDevice I've tried several numbers, including the 16 digit device
// number given me by my "device id" application, the "0123456789ABCDEF" device number
// given by my console and device windows, the "CECE.........................." 32 digit
// device number my logcat file told me to use in a logcat message,
// "AdRequest.TEST_EMULATOR"
// which an admob example in the docs said to use, "9774d56d682e549c" which another
// admob docs example said to use.
adView.loadAd(adRequest);
I've also tried adView.loadAd(new AdRequest()); using no device id as in another one of the google admob example apps.
nothing has worked to show anything, it's not even creating space for the ad, just the onFailedToRecieveAd(Invalid Ad Request) message in the logcat
I've also included the necessary permissions and "com.google.ads.AdActivity" in the manifest.
Wow, after days of frustration and posts to various forums I've found the answer.
You have to go to your account on the Admob website to set up your specific app for the admob ads and get a new longer publisher number. My publisher number only started with 'pub-......' where my new number is longer and starts with 'ca-app-pub-.......'. I was curious about this from the start when I saw the 'ca-app-pub' preface in an example banner ap.
Nowhere on the Google Admob "Google Mobile Ads SDK" development site in the "banner ads 1" instructions (https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals?hl=en_US#android) does it mention having to go back to your admob account to set up your specific app for ads and get a new publisher number.
The stupid mistakes are the hardest to fix.
Be carefull with the id, there are 2 codes: the editor number (like this: pub-xxxxxxxxxxxxxxxx) and the other is the banner id (like this: ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx) (this one you need to use)
You need to use the last one, if you use the first doesnt work:)
It is not unusual to get a failed to receive ad. This is a normal message essentially saying that there are no ads to serve for your app at this point in time.
It means that your Admob integration is working, you are getting a response back from the server. As your app sends more requests it will be more likely to receive ad impressions.
Have you tried requesting an ad without setting the test device ID?
adView.loadAd(new AdRequest());
Try adding the following lines of code to get more info about the failure:
// Set AdListener
adView.setAdListener(new AdListener() {
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
System.err.println("Ad failed: " + ad.toString() + error.toString());
}
#Override
public void onReceiveAd(Ad ad) {
System.out.println("Ad received: " + ad.toString());
}
});

Categories

Resources