Embedded advertising in Cocos2d-x games (Baidu banners)

Source: Internet
Author: User

Reprinted please indicate the source:Http://blog.csdn.net/oyangyufu/article/details/36716955

DEMO:Http://download.csdn.net/detail/oyangyufu/7589981

The previous article wrote cocos2d-x embedded mogo banner ad (Click here to jump), this article writes how to embed Baidu banner ad in The cocos2d-x.

Baidu banner advertising test:

1. Import cocos2d-x project to eclipse

2, registered login Baidu Mobile Alliance (http://munion.baidu.com/), download the promotion SDK android version, download the name: Baidu_MobAds_SDK.zip

3. decompress the package and obtain the jar package Baidu_MobAds_SDK.jar. Right-click the libs directory and choose Build Path> Add to Build Path.

4. Add User Permissions in AndroidManifest. xml:

    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.RECORD_AUDIO" />    <uses-permission android:name="android.permission.GET_TASKS" />    <uses-permission android:name="android.permission.CAMERA" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

5. Add APPSID and billing name and declare AppActivity

<! -- Declare Baidu's Activity --> <meta-data android: name = "BaiduMobAd_APP_ID" android: value = "debug"/> <meta-data android: name = "BaiduMobAd_APP_SEC" android: value = "debug"/> <activity android: name = "com. baidu. mobads. appActivity "android: configChanges =" keyboard | keyboardHidden | orientation "/> <! -- Baidu ad end -->

Note: The value of APPSID and billing name is debug. After debugging is successful, you can upload the application to the Consortium for review, after the application is approved, the ads are displayed and billed.

6. Add a banner advertisement to the main Activity class.

Package com.cocos2dx.org; import org. cocos2dx. lib. cocos2dxActivity; import org. cocos2dx. lib. cocos2dxGLSurfaceView; import org. json. JSONObject; import com. baidu. mobads. adSettings; import com. baidu. mobads. adView; import com. baidu. mobads. adViewListener; import android. content. context; import android. content. sharedPreferences; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. view; import android. view. viewGroup; import android. widget. relativeLayout; public class Test extends Cocos2dxActivity {public static SharedPreferences sharedPreferences; public static SharedPreferences. editor editor; private static Handler handler; private static RelativeLayout bannerLayout; private AdView adView; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); bannerLayout = new RelativeLayout (this); RelativeLayout. layoutParams parentLayputParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. MATCH_PARENT, RelativeLayout. layoutParams. WRAP_CONTENT); this. addContentView (bannerLayout, parentLayputParams); adView = new AdView (this); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case 0: if (bannerLayout. getChildCount () = 0) {// sets the listener adView. setListener (new AdViewListener () {public void onAdSwitch () {Log. w ("", "onAdSwitch");} public void onAdShow (JSONObject info) {Log. w ("", "onAdShow" + info. toString ();} public void onAdReady (AdView adView) {Log. w ("", "onAdReady" + adView);} public void onAdFailed (String reason) {Log. w ("", "onAdFailed" + reason);} public void onAdClick (JSONObject info) {Log. w ("", "onAdClick" + info. toString ();} public void onVideoStart () {Log. w ("", "onVideoStart");} public void onVideoFinish () {Log. w ("", "onVideoFinish") ;}@ Overridepublic void onVideoClickAd () {Log. w ("", "onVideoFinish") ;}@ Overridepublic void onVideoClickClose () {Log. w ("", "onVideoFinish") ;}@ Overridepublic void onVideoClickReplay () {Log. w ("", "onVideoFinish") ;}@ Overridepublic void onVideoError () {Log. w ("", "onVideoFinish") ;}}); // display the advertisement RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. MATCH_PARENT, RelativeLayout. layoutParams. WRAP_CONTENT); layoutParams. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM, RelativeLayout. TRUE); bannerLayout. addView (adView, layoutParams);} break ;}};} public jsononcreateview () {javasglsurfaceview = new Cocos2dxGLSurfaceView (this); // HelloWorld shocould create stencel buffer surglfaceview. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} // The static function cpp calls the public static void showBannerStatic () {Message msg = handler. obtainMessage (); msg. what = 0; handler. sendMessage (msg);} static {System. loadLibrary ("cocos2dcpp ");}}


 

 

7. Call the showBannerStatic () function through JNI in the CPP program.

8. Click show banner to display the advertisement as follows:


 

Related Article

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.