【发布时间】:2013-12-31 19:55:58
【问题描述】:
我正在尝试设置 adMob 广告。我有两个问题:
1) 我是否正确使用了 adListener 接口? 2) 为什么我看不到插页式广告?
让我们从 adListener 开始。据我从说明中了解, adListener 是一个接口。所以我创建了一个界面。
import com.google.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public interface AdListener {
public void onReceiveAd(AdView ad);
public void onFailedToReceiveAd(AdView ad, AdRequest.ErrorCode error);
public void onPresentScreen(AdView ad);
public void onDismissScreen(AdView ad);
public void onLeaveApplication(AdView ad);
}
然后在我的主类中我实现了 AdListener,一旦我这样做了,我就会被提示添加未实现的方法(来自接口的所有方法)。
public class MainActivity extends Activity implements AdListener {
// other code for the body (here). Just showing the methods and implement
@Override
public void onReceiveAd(AdView ad) {
Log.d(TAG, "onReceiveAd");
Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailedToReceiveAd(AdView ad, ErrorCode error) {
Log.d(TAG, "onFailedToReceiveAd");
Toast.makeText(this, "onFailedToReceiveAd", Toast.LENGTH_SHORT).show();
}
@Override
public void onPresentScreen(AdView ad) {
Log.d(TAG, "onPresentScreen");
}
@Override
public void onDismissScreen(AdView ad) {
Log.d(TAG, "onDismissScreen");
}
@Override
public void onLeaveApplication(AdView ad) {
Log.d(TAG, "onLeaveApplication");
}
}
我认为这不起作用的原因是我没有收到我的日志。我加了祝酒词来双重确认我没有遗漏任何东西。我认为我还缺少另一个步骤。
现在是广告部分。我认为我很好地遵循了指示,并且我的横幅确实有效。但我的广告插页式广告不起作用。这就是我为横幅和插页式广告准备的。
从我的横幅的 xml 中的代码 sn-p 开始
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="I_ADDED_CORRECT_ADUNIT_ID"
ads:adSize="BANNER" />
在我的主要活动中,我有以下代码。同样,我想看测试广告:
// setup ad banner and interstitial
final TelephonyManager tm = (TelephonyManager)getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String testDeviceId = tm.getDeviceId();
Log.d(TAG, "testDeviceId retrieved(" + testDeviceId + ")");
// create interstitial
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("I_ADDED_CORRECT_ADUNIT_ID");
// ad banner
adView = (AdView)this.findViewById(R.id.adView);
// request test interstitial ads
AdRequest adRequestInterstitial = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(testDeviceId)
.build();
// request test banner ads
AdRequest adRequestBanner = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(testDeviceId)
.build();
//load interstitial ads
interstitial.loadAd(adRequestInterstitial);
displayInterstitial();
// load banner ads
adView.loadAd(adRequestBanner);
//NOTE: the above code is all in onCreate().
//diaplayInterstitial is outside of onCreate()
public void displayInterstitial() {
if (interstitial.isLoaded()) {
Log.d(TAG, "displayInterstitial()");
interstitial.show();
}
}
让我们看看还有什么要告诉你的?哇,是的!我也已经在清单中设置了权限。权限 = INTERNET、ACCESS_NETWORK_STATE、ACCESS_WIFI_STATE、READ_PHONE_STATE。
我去了项目属性并添加了 google-play-services_lib
这是我的全部设置!我尝试了很多事情,因为我对可能出现的问题有一些“感觉”。首先,我认为可能无法同时请求测试广告横幅和测试广告插页式广告。所以我将插页式代码移至 onStart()。但是后来,我不确定这是否是必要的、有益的还是更糟的(而且它没有用!)。我的第二个想法是一切正常,一旦我发布了我的应用程序,一切都会好起来的。但无论如何,我的日志不适用于 AdListener。如果我能做到这一点,我想我可以更有信心地开始验证正在发生的事情。
提前感谢您的帮助。
日志 SS
【问题讨论】: