我有一个简单的hello world应用程序,它有一个并不总是显示的AdMob横幅。当我总是让横幅工作时,我会将功能添加到我的应用程序中。我将横幅配置为显示具有特定设备ID的测试广告。当我打开我的应用程序时,有时会出现测试横幅,有时候不会出现,我等待的时间并不重要。顺便说一句,我总是在手机上启用Wi-Fi。为什么横幅总是不显示?如果您发现它很有用,有时当广告未显示并退出应用时,此跟踪会显示在我的logcat上:
05-04 11:01:44.463: W/Ads(32406): Interrupted during GADSignals creation.
05-04 11:01:44.463: W/Ads(32406): java.lang.InterruptedException
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:975)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1282)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:207)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.k.a(SourceFile:52)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.k.a(SourceFile:98)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.request.c.a(SourceFile:114)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.util.b.run(SourceFile:19)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.util.l.call(SourceFile:43)
05-04 11:01:44.463: W/Ads(32406): at com.google.android.gms.ads.internal.util.n.run(SourceFile:75)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-04 11:01:44.463: W/Ads(32406): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-04 11:01:44.463: W/Ads(32406): at java.lang.Thread.run(Thread.java:856)
我非常害怕的是,有一刻我的应用程序崩溃了几次,但我无法重复错误。从那以后,它再次没有崩溃,但我知道它会这样做,我知道原因是横幅,因为在我开始在我的应用程序中使用AdMob之前,它们从未因未知原因而崩溃。
这是我的xml用户界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/adsLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/test_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
这是我从主Activity
用来管理广告的课程:
package bembibre.impossiblepuzzle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MyAdManager {
private AdView adView;
private String adsBlockId;
private Context context;
private LinearLayout adContainer;
public MyAdManager(String adsBlockId, Activity activity, int adContainerId) {
this.adsBlockId = adsBlockId;
this.context = activity;
this.adContainer = (LinearLayout) activity.findViewById(adContainerId);
}
private void initializeAdvertisement(AdRequest adRequest) {
// Crear adView.
this.adView = new AdView(this.context);
this.adView.setAdUnitId(this.adsBlockId);
this.adView.setAdSize(AdSize.BANNER);
// Buscar LinearLayout suponiendo que se le ha asignado
// el atributo android:id="@+id/mainLayout".
// Añadirle adView.
adContainer.addView(this.adView);
// Cargar adView con la solicitud de anuncio.
adView.loadAd(adRequest);
}
public void initializeTestAdvertisement(String deviceId) {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(deviceId) // Mi teléfono de prueba
.build();
this.initializeAdvertisement(adRequest);
}
public void initializeRealAdvertisement() {
// Iniciar una solicitud genérica.
AdRequest adRequest = new AdRequest.Builder().build();
this.initializeAdvertisement(adRequest);
}
public void hideAd() {
this.adContainer.setVisibility(View.GONE);
}
public void pause() {
if (this.adView != null) {
this.adView.pause();
}
}
public void resume() {
if (this.adView != null) {
this.adView.resume();
}
}
public void destroy() {
if (this.adView != null) {
this.adView.destroy();
}
}
}
最后,这是我的应用程序的唯一Activity
,它只加载广告横幅,然后在屏幕上绘制一个方形图形用于测试目的,因为这个应用程序将是一个益智游戏:
package bembibre.impossiblepuzzle;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import bembibre.impossiblepuzzle.logic.models.Board;
import bembibre.impossiblepuzzle.logic.models.Square;
import bembibre.impossiblepuzzle.ui.ImpossiblePuzzleView;
public class MainActivity extends Activity {
private static final String ADS_ID = "ca-app-pub-****************/**********";
private MyAdManager myAdManager;
private Board board;
private void loadAdvertisement() {
this.myAdManager = new MyAdManager(MainActivity.ADS_ID, this, R.id.adsLayout);
this.myAdManager
.initializeTestAdvertisement("9CC8187AB379FB0C135EB41EC66D8E5D");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.loadAdvertisement();
this.board = new Board();
ViewGroup viewGroup = (ViewGroup) this.findViewById(R.id.test_layout);
ImpossiblePuzzleView squareView = new ImpossiblePuzzleView(this);
viewGroup.addView(squareView);
squareView.setSquare(new Square(1, 2, 3, 2));
}
@Override
protected void onResume() {
super.onResume();
this.myAdManager.resume();
}
@Override
protected void onPause() {
super.onPause();
this.myAdManager.pause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.myAdManager.destroy();
}
}
请告诉我我做错了什么。我在谷歌播放中使用这个广告模型有几个应用程序,我不希望他们不显示广告或崩溃,因为我的用户会生气...我不知道为什么横幅有时出现和应用程序的工作原理好吧,有时候它没有出现,甚至应用程序崩溃而没有做任何事情。
编辑:我已经从中下载了一个示例AdMob项目 https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals?hl=es#play 并且错误仍在继续,所以一定不是我的错。此外,我认为当我在设备上连续打开几个不同的带有测试广告的应用程序时会发生此错误。这是因为我已经开发了几个应用程序,有时我会连续测试所有这些应用程序。这与广告没有显示的问题有关吗?