Download phonegap: Android:HOW TO: Add admob ads to your phonegap

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
phonegap: Android:HOW TO: Add admob ads to your
phonegap application
How to add adMob ads to your Phonegap application for Android.
If you are us ing localStorage within your PhoneGap / Cordova app, you will want to us e Option 2 for the actual Java Code.
Loading Admob on demand will clear localStorage for s ome reas on. All the other s teps are the s ame.
Follow thes e eas y s teps in your Admob Account:
Sign up for an adMob account
Create a s ite. (This needs to be an android application)
Retrieve your Publis her ID
Now back to Eclips e or your IDE of choice.
Download the adMob s dk and include it in your project. Follow the ins tructions here.
Don't forget to add "Import com.google.ads .* " in your Main Java Activity
add the following to your AndroidManifes t.xml
<us es -permis s ion android:name="android.permis s ion.INTERNET" />
<us es -permis s ion android:name="android.permis s ion.ACCESS_NETWORK_STATE" />
add the following ins ide your application s ection of your AndroidManifes t.xml
<activity android:name="com.google.ads .AdActivity" android:configChanges ="keyboard|keyboardHidden|orientation|s creenLayout|uiMode|s creenSize|s malles tScreenSize" />
The las t thing to do (before adding the Java code below) is change your project.properties to "target=android-13"
Project > Properties > Android > Project Build Target = Android API 13 or higher.
This means you will have to download s dk vers ion 13. You can leave your <target-s dk> alone ins ide your manifes t.xml
Java Code - Option 1 - Show AdMob Instantly.
Ins ert the following code in your MainActivity clas s . This will load Admob on-demand.
s uper.onCreate(s avedIns tanceState);
s uper.loadUrl("file:///android_as s et/www/index.html");
// the 2 lines above s hould already be in your Java activity if you s etup Phonegap correctly.
adView = new AdView(this , AdSize.BANNER, PUBLISHER_ID);
LinearLayout layout = s uper.root;
layout.addView(adView);
layout.s etHorizontalGravity(android.view.Gravity.CENTER_HORIZ ONTAL);
adView.loadAd(new AdReques t());
//This centers the ads in lands cape mode.
If you want the ads at the top, put this before you load webview. If you want the ads at the bottom, put it after.
Java Code - Option 2 - Delay AdMob Ads by X Seconds to prevent clearing your localStorage.....
There is a bug or glitch for localStorage with the Admob SDK and PhoneGap. If you us e the code in Option 1, your
localStorage will clear when your application loads . Not good if you depend on that feature for s toring us er data.
add "import android.os .Handler;" to the top of your Main Java Activity
Below is the full Java code for delaying your AdMob ads by 5 s econds (in millis econds ) Much thanks to this anonymous us er
at StackOverflow.
public clas s MyAppActivity extends DroidGap {
private Handler mHandler = new Handler();
private AdView adView;
/** Called when the activity is firs t created. */
@Override
public void onCreate(Bundle s avedIns tanceState) {
s uper.onCreate(s avedIns tanceState);
s uper.loadUrl("file:///android_as s et/www/index.html");
mHandler.pos tDelayed(new Runnable() {
public void run() {
doAdMob();
}
}, 5000);
}
private void doAdMob() {
// Create the adView
adView = new AdView(this , AdSize.BANNER, "YOUR PUB ID");
// Lookup your LinearLayout - get the s uper.root
LinearLayout layout = s uper.root;
// Add the adView to it
layout.addView(adView);
// This centers the ads in lands cape mode.
layout.s etHorizontalGravity(android.view.Gravity.CENTER_HORIZ ONTAL);
// Initiate a generic reques t to load it with an ad
AdReques t reques t = new AdReques t();
// and finally...
adView.loadAd(reques t);
}
}