我正在运行Admob 6.0.1,并尝试在表面视图的顶部添加广告,我可以说下面的代码适用于Android 3.2(真实设备),并且也适用于在模拟器中的android 4设备但是,当我尝试在pre api13上测试时,例如2.3.3或更低版本的广告没有显示。现在这里是奇怪的部分,如果我将surfaceview(在xml中)的可见性更改为不可见,广告将显示!!!!这是怎么回事??这只是模拟器有缺陷还是我有一个真正的问题?
我试图让代码尽可能简单地重现错误。我添加了一个按钮,只是为了显示surfaceview确实允许其他视图更新,而不是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网上论坛上发帖,但我觉得它在一个空白中丢失了..
答案 0 :(得分:1)
事实证明这是模拟器的问题,当代码在真实设备上运行时它工作正常!疯狂模拟器...
答案 1 :(得分:0)
尝试将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>
来实现此目的。
{{1}}