简体   繁体   中英

Admob + surfaceview don't play well

I'm running Admob 6.0.1 and trying to add the advert over the top of the surface view, I can say that the code below works great with a Android 3.2 (a real device), and also works fine with a android 4 device in the emulator but, when I try to test on pre api13, eg 2.3.3 or below the advert doesn't show. Now here's the weird part, if I change the visibility of the surfaceview (in xml) to invisible the advert will show!!!! what's going on?? Is this just the emulator being buggy or do I have a real problem?

I've tried to keep the code as simple as possible to reproduce the error. I've added a button just to show the surfaceview does allow other views to update, just not the Admob view...

   package com.google.example.ads.fundamentals;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;

    import com.google.ads.AdView;

    public class BannerSample extends Activity {
    /** The view to show the ad. */
    private AdView adView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        adView = (AdView) findViewById(R.id.adView);
    }

    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

    }

    class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private final TutorialThread _thread;

    public Panel(Context context, AttributeSet att) {
        super(context, att);
        getHolder().addCallback(this);
        _thread = new TutorialThread(getHolder(), this);

    }

    @Override
    public void onDraw(Canvas canvas) {

        canvas.drawColor(Color.BLUE);
        Log.d("Hello", "drawing stuff");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Log.d("HELLO", "surface changed" + holder.getSurfaceFrame().width()
                + "Height: " + holder.getSurfaceFrame().height());

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or
        // else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
    }

    class TutorialThread extends Thread {
    private final SurfaceHolder _surfaceHolder;
    private final Panel _panel;
    private boolean _run = false;

    public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
    }

and the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <com.google.example.ads.fundamentals.Panel
        android:id="@+id/mainPanel" android:layout_width="match_parent"
        android:layout_height="match_parent" android:visibility="invisible" />
    <com.google.ads.AdView android:id="@+id/adView"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_gravity="top" ads:adSize="BANNER"         ads:adUnitId="xxxxxxxxx"
        ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR" />

    <Button android:id="@+id/mybutton" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_below="@id/adView"
        android:text="hello" />
</RelativeLayout>

And the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.google.example.ads.fundamentals"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".BannerSample"
                              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>

        <activity android:name="com.google.ads.AdActivity"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

Any ideas or help would be so great, I've tried to post on Google Groups for admob but I think it got lost in a void..

As it turns out it was a problem with the emulator, When the code was run on a real device it worked fine! crazy emulator...

Try placing the AdView outside of the SurfaceView instead of on top of it. You can do this by aligning the AdView ad the top, and setting the SurfaceView below it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <com.google.example.ads.fundamentals.Panel
        android:id="@+id/mainPanel" android:layout_width="match_parent"
        android:layout_height="match_parent" android:visibility="invisible"
        android:layout_below="@+id/adView" />
   <com.google.ads.AdView android:id="@id/adView"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_gravity="top" ads:adSize="BANNER" ads:adUnitId="xxxxxxxxx"
        ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR"
        android:layout_alignParentTop="true" />
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM