我使用下面的代码在 Android 中添加 Admob。但不能工作只得到黑屏。
主布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout1);
adView = new AdView(this, AdSize.SMART_BANNER, "xxxxxxxxxxxxx");
layout.addView(adView);
setContentView(layout);
new Thread(){
@SuppressWarnings("deprecation")
public void run() {
Looper.prepare();
AdRequest re = new AdRequest();
re.setTesting(true);
adView.loadAd(re);
};
}.start();
}
清单.xml
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
日志猫:
07-16 12:32:09.839: I/Ads(1298): To get test ads on this device, call adRequest.addTestDevice("DB5C6285034D8192153E5D66D726E418");
07-16 12:32:10.629: I/Ads(1298): adRequestUrlHtml: <html><head><script src="http://www.gstatic.com/afma/sdk-core-v40.js"></script><script>AFMA_buildAdURL({"preqs":0,"u_sd":1,"slotname":"S5830c763db75","u_w":320,"msid":"syncfusion.test.testAdmobDemo","js":"afma-sdk-a-v4.1.0","isu":"DB5C6285034D8192153E5D66D726E418","format":"320x50_mb","net":"wi","app_name":"1.android.syncfusion.test.testAdmobDemo","hl":"en","u_h":480,"u_audio":3,"u_so":"p"});</script></head><body></body></html>