我正在尝试设置 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 中的代码片段开始
<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();
}
}
让我们看看还有什么要告诉你的?噢,是的!我还已经在清单中设置了权限。权限 = 互联网、ACCESS_NETWORK_STATE、ACCESS_WIFI_STATE、READ_PHONE_STATE。
我转到项目属性并添加了 google-play-services_lib
这就是我的整个设置!我尝试了很多方法,因为我对可能出现的问题有一些“感觉”。首先,我认为也许不能同时请求测试横幅广告和测试广告插页式广告。所以我将插页式代码移至 onStart()。但后来,我不确定这是否必要、有益还是更糟(而且它不起作用!)。我的第二个想法是一切正常,一旦我发布我的应用程序,一切都会好起来的。但无论如何,我的日志不适用于 AdListener。如果我能做到这一点,我想我可以更有信心地开始验证正在发生的事情。
预先感谢您的帮助。
Log SS