2015-08-15 297 views
0

我從github上下載了cordova示例項目。我在android studio中提取並測試並在星系s4上進行調試。當應用程序打開時會帶來警告「admob plugin not loaded」。我已經與互聯網插件在我的項目安裝AdMob的插件,一起科爾多瓦的admob插件錯誤

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" /> 
    <title>Hello World</title> 
    <script type="text/javascript" src="cordova.js"></script> 
    <style type="text/css"> 
     html, body { width:100%; height:100%; margin:0; padding:0; overflow:hidden; background-color:white; } 
     div#fullpage { width:100%; height:100%; margin:0; padding:0; border:0px solid red; text-align:center; vertical-align:middle; } 
     button { font-size: 22px; } 
    </style> 
</head> 
<body onload="onDocLoad()" onresize="onResize()"> 
<script> 
    function onDocLoad() { 
     if((/(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent))) { 
      document.addEventListener('deviceready', initApp, false); 
     } else { 
      initApp(); 
     } 
    } 

    function initApp() { 
     initAd(); 

     // display the banner at startup 
     window.plugins.AdMob.createBannerView(); 
    } 
    function initAd(){ 
     if (window.plugins && window.plugins.AdMob) { 
      var ad_units = { 
       ios : { 
        banner: 'ca-app-pub-6869992474017983/4806197152', 
        interstitial: 'ca-app-pub-6869992474017983/7563979554' 
       }, 
       android : { 
        banner: 'ca-app-pub-6869992474017983/9375997553', 
        interstitial: 'ca-app-pub-6869992474017983/1657046752' 
       }, 
       wp8 : { 
        banner: 'ca-app-pub-6869992474017983/8878394753', 
        interstitial: 'ca-app-pub-6869992474017983/1355127956' 
       } 
      }; 
      var admobid = ""; 
      if(/(android)/i.test(navigator.userAgent)) { 
       admobid = ad_units.android; 
      } else if(/(iphone|ipad)/i.test(navigator.userAgent)) { 
       admobid = ad_units.ios; 
      } else { 
       admobid = ad_units.wp8; 
      } 

      window.plugins.AdMob.setOptions({ 
       publisherId: admobid.banner, 
       interstitialAdId: admobid.interstitial, 
       bannerAtTop: false, // set to true, to put banner at top 
       overlap: false, // set to true, to allow banner overlap webview 
       offsetTopBar: false, // set to true to avoid ios7 status bar overlap 
       isTesting: false, // receiving test ad 
       autoShow: true // auto show interstitial ad when loaded 
      }); 

      registerAdEvents(); 

     } else { 
      alert('admob plugin not ready'); 
     } 
    } 
    // optional, in case respond to events 
    function registerAdEvents() { 
     document.addEventListener('onReceiveAd', function(){}); 
     document.addEventListener('onFailedToReceiveAd', function(data){}); 
     document.addEventListener('onPresentAd', function(){}); 
     document.addEventListener('onDismissAd', function(){ }); 
     document.addEventListener('onLeaveToAd', function(){ }); 
     document.addEventListener('onReceiveInterstitialAd', function(){ }); 
     document.addEventListener('onPresentInterstitialAd', function(){ }); 
     document.addEventListener('onDismissInterstitialAd', function(){ }); 
    } 
    function onResize() { 
     var msg = 'web view: ' + window.innerWidth + ' x ' + window.innerHeight; 
     document.getElementById('sizeinfo').innerHTML = msg; 
    } 
    </script> 
<div id="fullpage"> 
    <p>Demo for AdMob Plugin</p> 
    <p><button onclick="window.plugins.AdMob.createBannerView();">create Ad</button> <button onclick="window.plugins.AdMob.destroyBannerView();">remove Ad</button></p> 
    <p><button onclick="window.plugins.AdMob.showAd(true,function(){},function(e){alert(JSON.stringify(e));});">show Ad</button> <button onclick="window.plugins.AdMob.showAd(false);">hide Ad</button></p> 
    <p><button onclick="window.plugins.AdMob.createInterstitialView();">create Interstitial Ad</button></p> 
    <p><button onclick="window.plugins.AdMob.showInterstitialAd(true,function(){},function(e){alert(JSON.stringify(e));});">show Interstitial Ad</button></p> 
    <div id="sizeinfo">width * height</div> 
    <div>Try rotate screen to test the orientation change</div> 

</div> 

</body> 
</html> 

回答

0

既然你不指定實際的AdMob插件使用,我可以回答使用其他插件(https://github.com/sunnycupertino/cordova-plugin-admob-simple)已經做了你需要什麼:

添加插件

cordova plugin add cordova-plugin-admob-simple 

集成如下:

- 添加下面的JavaScript功能,把你的Ø wn廣告代碼,如果你想玩變量。

- 從onDeviceReady()和showBannerFunc()以及showInterstitialFunc()中調用initAd()來顯示廣告。

//initialize the goodies 
function initAd(){ 
     if (window.plugins && window.plugins.AdMob) { 
      var ad_units = { 
       ios : { 
        banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',  //PUT ADMOB ADCODE HERE 
        interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx' //PUT ADMOB ADCODE HERE 
       }, 
       android : { 
        banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',  //PUT ADMOB ADCODE HERE 
        interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx' //PUT ADMOB ADCODE HERE 
       } 
      }; 
      var admobid = (/(android)/i.test(navigator.userAgent)) ? ad_units.android : ad_units.ios; 

      window.plugins.AdMob.setOptions({ 
       publisherId: admobid.banner, 
       interstitialAdId: admobid.interstitial, 
       adSize: window.plugins.AdMob.AD_SIZE.SMART_BANNER, //use SMART_BANNER, BANNER, IAB_MRECT, IAB_BANNER, IAB_LEADERBOARD 
       bannerAtTop: false, // set to true, to put banner at top 
       overlap: true, // banner will overlap webview 
       offsetTopBar: false, // set to true to avoid ios7 status bar overlap 
       isTesting: false, // receiving test ad 
       autoShow: false // auto show interstitial ad when loaded 
      }); 

      registerAdEvents(); 
      window.plugins.AdMob.createInterstitialView(); //get the interstitials ready to be shown 
      window.plugins.AdMob.requestInterstitialAd(); 

     } else { 
      //alert('admob plugin not ready'); 
     } 
} 
//functions to allow you to know when ads are shown, etc. 
function registerAdEvents() { 
     document.addEventListener('onReceiveAd', function(){}); 
     document.addEventListener('onFailedToReceiveAd', function(data){}); 
     document.addEventListener('onPresentAd', function(){}); 
     document.addEventListener('onDismissAd', function(){ }); 
     document.addEventListener('onLeaveToAd', function(){ }); 
     document.addEventListener('onReceiveInterstitialAd', function(){ }); 
     document.addEventListener('onPresentInterstitialAd', function(){ }); 
     document.addEventListener('onDismissInterstitialAd', function(){ 
      window.plugins.AdMob.createInterstitialView();   //REMOVE THESE 2 LINES IF USING AUTOSHOW 
      window.plugins.AdMob.requestInterstitialAd();   //get the next one ready only after the current one is closed 
     }); 
    } 

//display the banner 
function showBannerFunc(){ 
    window.plugins.AdMob.createBannerView(); 
} 
//display the interstitial 
function showInterstitialFunc(){ 
    window.plugins.AdMob.showInterstitialAd(); 
} 
相關問題