Interstitial Add is not getting loaded in android

2019-08-19 06:41发布

问题:

After debugging i found out that my onLoaded() return is false and only a blank white screen appears. No add is getting loaded! even if i wait for infinite time the add unit id is picked from

https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial (for sample add) Here is my MainActivity class

    package com.example.jatinarora.threestickhockey;
    import android.app.ActionBar;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.media.MediaPlayer;
    import android.os.SystemClock;
    import android.support.v4.view.MotionEventCompat;
    import android.support.v4.view.VelocityTrackerCompat;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.VelocityTracker;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Toast;
    import com.google.android.gms.ads.AdListener;  
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends ActionBarActivity {

public InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    requestNewInterstitial();
    float count123 = 1 ;
    // i tried setting the loop to infinity to wait for the advertisment to load!
    while(count123 >=0) {
        if (interstitial!= null && interstitial.isLoaded()) {
            interstitial.show();
            count123 = -1;
        }
        }
        }

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitial.loadAd(adRequest);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
protected void onResume() {
    super.onResume();
    mView.running = true;
}
protected void onPause()
{
    mView.resume = 1;
    mView.running = false;
    super.onPause();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

In my activity_main.xml I didn't add AdView because in all the tutorials and links i have visited,I haven't seen any for interstitial , It was used for banner

Here is my activity_main.xml

    <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:label="@string/app_name"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp" tools:context=".MainActivity">
<com.example.jatinarora.threestickhockey.mView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
   </RelativeLayout>

Here is my manifestFile

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.jatinarora.threestickhockey" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    <activity android:name="com.google.android.gms.ads.AdActivity"
               android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
  </application>
</manifest>

Any suggestion ,mistake why the add is not showing up or any alternate solution will be helpful a lot!

回答1:

[The cause of the problem]

i can't give a precise reason to the problem, but, Running loops on the main thread introduces lags, and that is the reason why the add is not loading because it times out so fast and there is no room for it,cause you got alot of process hogging work on that thread, that might also be the reason for the white screen for 3-4 seconds .. but aside from that, the add not showing up in general probably might be your internet connection or .......

so Sir, forget about the loop, check this answer , it wil show you how to show the add when it's done loading - it has onAdLoaded() which is triggered when interstitial.isLoaded() is true, instead of looping for it all the time.. and the rest you leave it to internet and google, the add will show up when it shows up, you can add breakpoint to know how it gets down