几天来,我一直在尝试在我的 PhoneGap 项目中添加 AdMob。但我找不到在我的应用程序上显示添加的方法。我遵循了一些教程,但似乎没有任何帮助。我正在使用 Google 提供的横幅测试广告单元ca-app-pub-3940256099942544/6300978111
。我通过从Adobe PhoneGap Build 构建apk 来测试应用程序,然后通过在我的 Android 手机中安装 apk 来运行应用程序。这就是我所做的:
在我的项目中添加 AdMob 插件:
phonegap plugin add cordova-admob
然后在我的 index.js 文件中
代码 #1
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
admob.setOptions({
publisherId: "ca-app-pub-3940256099942544/6300978111",
isTesting: false,
autoShowBanner: true,
});
admob.createBannerView();
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
if (parentElement != null) {
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
}
console.log('Received Event: ' + id);
}
};
代码 #2
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
alert("device ready");
app.adSetter();
},
adSetter: function(){
alert(navigator.userAgent);
var admobid = {};
if( /(android)/i.test(navigator.userAgent) ) {
admobid = {
banner: 'ca-app-pub-3940256099942544/6300978111'
};
}
if(AdMob) AdMob.createBanner( {
adId:admobid.banner,
position:AdMob.AD_POSITION.BOTTOM_CENTER,
autoShow:true
} );
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
if (parentElement != null) {
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
}
console.log('Received Event: ' + id);
}
};
以上都没有奏效。我究竟做错了什么?
我需要做什么才能在我的 PhoneGap 项目中添加 AdMob 并在我的应用中显示添加?
提前致谢。:)