【问题标题】:Admob + surfaceview don't play wellAdmob + surfaceview 打得不好
【发布时间】:2012-06-24 13:40:41
【问题描述】:

我正在运行 Admob 6.0.1 并尝试在表面视图的顶部添加广告,我可以说下面的代码适用于 Android 3.2(真实设备),也适用于模拟器中的 android 4 设备但是,当我尝试在 pre api13 上进行测试时,例如 2.3.3 或更低版本的广告不显示。现在这是奇怪的部分,如果我将surfaceview(在xml中)的可见性更改为不可见,广告将显示!!!这是怎么回事??这只是模拟器有问题还是我真的有问题?

我已尝试使代码尽可能简单以重现错误。我添加了一个按钮,只是为了显示表面视图确实允许更新其他视图,而不是 Admob 视图...

   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);
                }
            }
        }
    }
    }

和 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>

还有清单:

<?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>

任何想法或帮助都会非常棒,我曾尝试在 Google 网上论坛上发布 admob,但我认为它迷失了。

【问题讨论】:

    标签: java android emulation admob surfaceview


    【解决方案1】:

    事实证明这是模拟器的问题,当代码在真实设备上运行时,它运行良好!疯狂的模拟器……

    【讨论】:

      【解决方案2】:

      尝试将AdView 放在SurfaceView 之外,而不是放在它上面。为此,您可以将 AdView 广告对齐顶部,并将 SurfaceView 设置在其下方。

      <?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>
      

      【讨论】:

      • 有没有人使用运行 pre android 3.2 的 SurfaceView 进行调解?如果我使用普通的 Admob 集成,(无中介)广告会显示,但是当我将发布者 ID 交换为中介 ID 时:广告将不会加载,但如果我随后删除表面视图,它们会再次加载!我已经设法让这个类似的代码与 inmobi、tapfortap、普通 admob 一起工作,但是当我尝试使用中介时,它在 3.2 之前的版本中失败了,ics 工作正常。所以我真的认为这是中介框架中的一个错误,我不认为这很简单,因为我在其他类似的集成中没有遇到任何问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多