简体   繁体   English

如何在android中使用带有phonegap的Admob

[英]How to use Admob with phonegap in android

I am facing trouble in displaying Ads in my Phonegap App, as getting the error message - " you must have adactivity declared in androidmanifest.xml with configchanges " 我在我的Phonegap应用程序中显示广告时遇到了麻烦,因为收到错误消息 - “ 你必须在androidmanifest.xml中使用configchanges声明adactivity

Here is my manifest.xml file. 这是我的manifest.xml文件。

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.MyApp"
  android:versionCode="5"
  android:versionName="1.3.1" >

 <supports-screens
  android:largestWidthLimitDp="320"
  android:smallScreens="true"
  android:normalScreens="true"
  android:largeScreens="true"        
  android:xlargeScreens="true"/>


 <uses-sdk

    android:minSdkVersion="15"
    android:targetSdkVersion="17" />

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.INTERNET"/>

 <application
    android:debuggable="false"
    android:allowBackup="true"
    android:icon="@drawable/desktop_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

   <activity
      android:name="com.MyApp.Activity"
      android:label="@string/app_name" 
      android:configChanges="orientation|keyboardHidden|screenSize|locale">

    <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <!--      AdMobActivity definition -->

    <activity android:name="com.google.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"/>

  </application>


</manifest>

The library that I have used for using Admob is GoogleAdMobAdsSdk-6.4.1.jar 我用于使用Admob的库是GoogleAdMobAdsSdk-6.4.1.jar

The similar code is working in normal activity (without using phonegap), but facing trouble while embedding admob in phonegap. 类似的代码在正常活动中工作(不使用phonegap),但在将restob嵌入phonegap时遇到麻烦。

old version admob sdk is not supported by android .you can try use other plugins. android不支持旧版本admob sdk。你可以尝试使用其他插件。 ref: https://github.com/admob-google/admob-cordova 参考: https//github.com/admob-google/admob-cordova

1.install admob phonegap plugin 1.install admob phonegap插件

use the cordova command 使用cordova命令
download the plugin ,then install with local location 下载插件,然后使用本地位置安装

cordova plugin add c:\phonegap-admob-plugin 

or install cordova plugin online 或在线安装cordova插件

cordova plugin add com.admob.plugin

use the phonegap command 使用phonegap命令
download the plugin ,then install with local location 下载插件,然后使用本地位置安装

phonegap plugin add c:\phonegap-admob-plugin 

use phonegap builder ,add config 使用phonegap builder,添加配置

<gap:plugin name="com.admob.plugin" version="1.0.0" source="plugins.cordova.io"/>

2.init admob phonegap plugin 2.init admob phonegap插件

init plugin after deviceready event 在deviceready事件之后的init插件

admob.initAdmob("admob banner ID","admob interstitial ID");//admob id format ca-app-pub-xxxxxxxxxxxxxxxxxxx/xxxxxxxxxx

3.show banner at relation position 3.在关系位置展示横幅

there are some banner size in admob.BannerSize,and you can create your own banner size that is available in admob platform. admob.BannerSize中有一些横幅大小,您可以在admob平台中创建自己的横幅大小。 admob.Position hold all relation position const . admob.Position保持所有关系位置const。

admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_APP);//show banner at the top of app 

and more you can set more param for admob such as test mode and is your app made for child. 还有更多你可以为测试模式设置更多的参与者,你的应用程序是为孩子设计的。 var admobParam=new admob.Params(); //admobParam.extra={'keyword':"admob phonegame"}; //admobParam.isForChild=true; admobParam.isTesting=true; admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_CENTER,admobParam);

4.show banner at absolute position 4.在绝对位置展示横幅

you can put admob banner at absolute position as easy as relation position. 你可以把adob横幅放在绝对位置,就像关系位置一样简单。

admob.showBannerAbsolute(admob.BannerSize.BANNER,0,70);//show banner at absolute position x 0,y 70

5.show admob Interstitial 5.show admob非页内广告

show admob Interstitial in phonegap,cordova or xdk application is the same step. show admob在phonegap,cordova或xdk应用程序中的插页式广告是同一步骤。 cache Interstitial ,and then show it in onInterstitialReceive function or show it when your game over. 缓存非页内广告,然后在onInterstitialReceive函数中显示它或在游戏结束时显示它。 ``` ```

document.addEventListener(admob.Event.onInterstitialReceive, onInterstitialReceive, false);//show in ad receive event fun need add receive listener
 admob.cacheInterstitial();// load admob Interstitial
 function onInterstitialReceive(message) {//show in ad receive event fun
     admob.showInterstitial();
 }
 function onGameOver(){//call this fun to show when game over
        admob.isInterstitialReady(function(isReady){
            if(isReady){
                admob.showInterstitial();
            }
        });
  }

``` ```

6.handle admob ad event 6.handle admob广告活动

you can handler all native event of admob ,as onInterstitialReceive 你可以处理admob的所有本机事件,如onInterstitialReceive
all event type is in admob.Event 所有事件类型都在admob.Event中
``` ```

function onAdmobEvent (message) {
    //do some on admob event
}
document.addEventListener(admob.Event.onAdmobBannerDismiss, onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobBannerFailedReceive), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobBannerLeaveApplication), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobBannerPresent), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobBannerReceive), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobInterstitialDismiss), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobInterstitialFailedReceive), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobInterstitialLeaveApplication), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobInterstitialPresent), onAdmobEvent, false);
document.addEventListener(admob.Event.onAdmobInterstitialReceive), onAdmobEvent, false);

``` ```

7.more function 7.更多功能

1.hide admob banner 1.hide admob banner

admob.hideBanner()

2.test if Interstitial has loaded success ``` 2.测试插页式广告是否已成功加载```

admob.isInterstitialReady(function(isReady){
    if(isReady){
        alert("admob Interstitial loaded");
    }
});

``` ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM