Add ads to AdMob in Android and Cocos2d-x

Source: Internet
Author: User

1. Add AdMob ads to Android

Download the Google-play-services_lib Development Library from the Android SDK, which contains the jar files we need to import the downloaded file into our Eclipse project, which is typically in your Android SDK Extras \google directory, select Google-play-services_lib to import Eclipse. Then create a new Android project named: testads, select this project in Eclipse right-click into the Properties property and import the Google-play-services_lib library, as

It is important to note here that your engineering documents must be in the same partition as Google-play-services_lib, that is, if your project file is in the E-drive, then your google-play-services_lib must be in the E-drive, or it will go wrong. As for this is why, I am not too clear. Next we'll be able to add ads to our code. First add the required permissions in Androidmanifest.xml:

?
1 2 <uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Next, add the following to the Androidmanifest.xml Application tab:

1 <  android:name= "Com.google.android.gms.version"2            android:value = "@integer/google_play_services_version" />

Then add the Activity statement:

1  <  android:name= "Com.google.android.gms.ads.AdActivity"2             android: Configchanges= "Keyboard|keyboardhidden|orientation|screenlayout|uimode|screensize|smallestscreensize"  />

The next work is very simple, directly in the code to write it, this code is an example, where I put the code directly out of the

1 Package com.example.testads;2 3 import com.google.android.gms.ads.AdSize;4 import com.google.android.gms.ads.AdRequest;5 import Com.google.android.gms.ads.AdView;6 7 import android.app.Activity;8 import Android.os.Bundle;9 import Android.view.LayoutInflater;Ten import Android.view.Menu; One import Android.view.MenuItem; A import Android.view.View; - import Android.view.ViewGroup; - import Android.view.ViewGroup.LayoutParams; the import android.widget.LinearLayout; - import android.widget.RelativeLayout; - import Android.os.Build; -  + Public class Mainactivity extends Activity { -      + private AdView AdView; A  at  - @Override - protected void OnCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); -          - AdView = new AdView (this); in Adview.setadunitid ("ca-app-pub-1391338311745569/9247870934"); - adview.setadsize (adsize.banner); to          + relativelayout layout = new Relativelayout (this);  - Addcontentview (Layout, new Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent)); the Layout.addview (adView); * relativelayout.layoutparams View_para = new Relativelayout.layoutparams (AdSize.BANNER.getWidthInPixels (this), AdSize.BANNER.getHeightInPixels (this)); $ View_para.addrule (Relativelayout.align_parent_right, relativelayout.true);Panax Notoginseng View_para.addrule (relativelayout.align_parent_top,relativelayout.true); - Adview.setlayoutparams (View_para); the          + adrequest rdrequest = new Adrequest.builder (). build (); A Adview.loadad (rdrequest); the          +     } -  $ @Override $ protected void Onresume () { - //TODO auto-generated method stub - Super.onresume (); the          - Adview.resume ();Wuyi     } the      - @Override Wu protected void OnDestroy () { - //TODO auto-generated method stub About Super.ondestroy (); $ Adview.destroy (); -     } -      -}

Finally, just go ahead and post it.

It is important to note that this line of code:

1  Adview.setadunitid ("ca-app-pub-1391338311745569/9247870934");

The value in this is to apply for AdMob's account yourself, put in how to apply for Google for yourself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.