在phonegap应用程序中添加admob需要帮助

时间:2014-11-12 11:29:55

标签: cordova admob

我有基于网络的应用程序使用html和css。我使用手机间隙构建并且运行良好,当我尝试在我的应用程序中集成admob时,它显示清单文件中缺少AdActivity。我搜索过并且没有'找到了答案。亲切的帮助!

这是我的mainactivity.java文件:

public class MainActivity extends DroidGap {
private Handler mHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");

mHandler.postDelayed(new Runnable() {
public void run() {
doAdmob();
}
}, 1000);
}
private void doAdmob() {   
// Create the adView
AdView adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-000000000000000000000000");
adView.setAdSize(AdSize.BANNER);
// Lookup your LinearLayout - get the super.root
LinearLayout layout = super.root;
// Add the adView to it
layout.addView(adView);
// This centers the ads in landscape mode.        
layout.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL); 
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}  
}    

这是我的android清单文件:          

<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="20" />

<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>

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


<application 
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name" 
android:configChanges="orientation|keyboardHidden">
<intent-filter>
</intent-filter>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        />


</application>


请帮助...........

1 个答案:

答案 0 :(得分:1)

在phonegap中,您可以尝试在每个特定平台上构建应用程序,然后通过修改代码来集成admob,这似乎是您正在尝试执行的操作。您必须为要支持的每个平台执行此操作,并且您还必须为每个平台编写本机代码。 我建议您使用插件,您已经使用该插件完成了本机和清单集成。

如果你想修改本机代码,你可以see here如何在android和ios中集成admob。

如果您决定使用插件,我建议我保留一个插件:admob-google-cordova

整合非常容易:

cordova plugin add com.admob.google

或者如果你想使用phonegap cli:

phonegap local plugin add https://github.com/appfeel/admob-google-cordova.git

并使用它,在javascript:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
  document.removeEventListener('deviceready', onDeviceReady, false);

  // Set AdMobAds options:
  admob.setOptions({
    publisherId:          "ca-app-pub-8440343014846849/3119840614",
    interstitialAdId:     "ca-app-pub-8440343014846849/4596573817"
  });

  // Start showing banners (atomatic when autoShowBanner is set to true)
  admob.createBannerView();

  // Request interstitial (will present automatically when autoShowInterstitial is set to true)
  admob.requestIntertitial();
}

(将代码替换为您的发布商ID&#39; s希望它有所帮助!

修改

最简单快捷的方法是从命令行使用cordova plugin add com.admob.google。无论如何,我在这里留下了在Eclipse中添加支持的说明:

  • Github或通过命令行:git clone https://github.com/appfeel/admob-google-cordova.git
  • 下载插件
  • 解压缩donwloaded插件
  • src/android/*.java复制到YourProjectFolder/platforms/android/src/com/appfeel/cordova/admob/
  • src/android/res/values/admob.xml复制到YourProjectFolder/platforms/android/res/values/android.xml
  • Add support for android-support-v4
  • Add support for google-play-services
  • 打开您的清单文件,并在<application...>标记下添加以下内容:

代码:

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />