cocos2d-x 植入广告(一) 百度横屏、插屏广告

前端之家收集整理的这篇文章主要介绍了cocos2d-x 植入广告(一) 百度横屏、插屏广告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天尝试了一下 有米、芒果 平台的广告植入,觉得和之前植入百度平台广告的方式不太一样。回想自己在这上面也算是花费了不少时间,现在就把植入广告的过程记录下来,算是对植入广告方式的总结。


【准备工作】
1.首先要将应用上传百度开发者平台,因为后面要用到 API KEY 和 APP ID。

2.下载SDK包(需要集成什么服务就勾选什么),这里我勾选了 广告联盟 和 自动更新



解释下,之前没有集成自动更新功能,结果提交之后没能审核通过。。。。所以,就把自动更新也下载下来了。


【阅读开发手册、查看demo】

因为百度只提供了Android的demo,所以使用cocos2d-x植入广告还是挺麻烦的。正因为这样,一定要好好阅读开发手册和demo。好了,不废话了,开工!!!


【步骤1 导入相关demo】

1自动更新&APPX (之后会通过这个demo来完成自己的广告植入代码)



2 BDIntegrationSDK_LibProject 这个是将来要引用的



导入之后,将IsLibrary 项目 打勾




3.为自己的工程引入BDIntegrationSDK_LibProject


具体的方式可参考 百度提供的开发手册,篇幅有限,这里就不一一上图了。


【步骤2 设置APPID 和 APPKEY 以及其他配置】

集成不同的功能需要获得不同的权限,这里,我只集成了自动更新&APPX ,在自己项目的AndroidManifest.xml中做如下配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.richard.SouthChinaSeaWar" 	 <!-- 我的包名 -->
      android:versionCode="2"
      android:versionName="2.0"
      android:installLocation="auto">

    <uses-sdk android:minSdkVersion="9"/>
    <uses-feature android:glEsVersion="0x00020000" />
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <application android:label="@string/app_name"
                 android:icon="@drawable/icon">
					 
        <!-- Tell Cocos2dxActivity the name of our .so -->
        <Meta-data android:name="android.app.lib_name"
	              android:value="cocos2dcpp" />
        
	<!-- BDAPPID 和 BDAPPKEY 一定要换成自己的 --> 
        <Meta-data android:name="BDAPPID" android:value="7071438"/>
        <Meta-data android:name="BDAPPKEY" android:value="IcdAOe74YsFWW7wGpau4C6dbb7Zya7nT"/>

        <activity android:name="org.cocos2dx.cpp.AppActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:configChanges="orientation">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
         <!-- 移动云AppX SDK -->
        <activity android:name="com.baidu.appx.ui.BDInnerBrowser" />

        <receiver android:name="com.baidu.appx.app_download.CompleteReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />

                <data android:scheme="package" />
                <!-- 注意!! 这句必须要加,否则接收不到安装完成广播 -->
            </intent-filter>
        </receiver>
        
        <!-- 自动更新SDK -->
           <activity android:name="com.baidu.autoupdatesdk.ConfirmDialoigActivity"
            android:screenOrientation="sensor"
            android:exported="false"
            android:theme="@style/bdp_update_dialog_style_fullscreen" />
        
        <receiver android:name="com.baidu.autoupdatesdk.receiver.BDBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.baidu.autoupdatesdk.ACTION_NEW_UPDATE"/>
                <action android:name="com.baidu.autoupdatesdk.ACTION_DOWNLOAD_COMPLETE"/>
                <action android:name="com.baidu.autoupdatesdk.ACTION_NEW_AS"/>
                <action android:name="com.baidu.autoupdatesdk.ACTION_AS_DOWNLOAD_COMPLETE"/>
            </intent-filter>
        </receiver>
        
    </application>

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

    <uses-permission android:name="android.permission.INTERNET"/>
    
     <!-- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> -->
    <!-- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> -->
</manifest> 



说个比较BT的事儿,在配置好 之后,有个地方一直报错
 <!-- 自动更新SDK -->
           <activity android:name="com.baidu.autoupdatesdk.ConfirmDialoigActivity"
            android:screenOrientation="sensor"
            android:exported="false"
            android:theme="@style/<u>bdp_update_dialog_style_fullscreen</u>" />


没错!就是加了下划线的部分。在styles.xml里面加了以下内容之后OK了

<style name="bdp_update_dialog_style" parent="android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item> 
        <item name="android:windowNoTitle">true</item><!--除去title-->
        <item name="android:windowContentOverlay">@null</item> 
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    
    <style name="bdp_update_dialog_style_fullscreen" parent="@style/bdp_update_dialog_style">
        <item name="android:windowFullscreen">true</item>
    </style>

注意,包名,APPID 和 APPKEY一定要设成自己的


【步骤3 引入相关jar,并完成java代码

1.把BDIntegrationSDK_LibProject目录下的libs文件夹中的4个jar文件引入自己项目的libs文件夹中(直接拉过来就好了)



2.接下来完成java部分的代码

当前自己项目的src目录下只有一个AppActivity.java文件,并且里面没什么内容。。。好了,我们再添加一个文件,姑且命名为ShowAdHelper.java,这两个文件是我参考自动更新&APPX 这个demo写的,比较乱,不过能用(*^__^*)


下面就是这两个文件内容


AppActivity.java

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted,free of charge,to any person obtaining a copy
of this software and associated documentation files (the "Software"),to deal
in the Software without restriction,including without limitation the rights
to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
copies of the Software,and to permit persons to whom the Software is
furnished to do so,subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR
IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER
LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RelativeLayout;

import com.baidu.appx.BDBannerAd;
import com.baidu.appx.BDInterstitialAd;
import com.baidu.appx.BDBannerAd.BannerAdListener;
import com.baidu.appx.BDInterstitialAd.InterstitialAdListener;
import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.UICheckUpdateCallback;

public class AppActivity extends Cocos2dxActivity {
	private static String TAG = "AppX_Interstitial";
	
	private static String TAG1 = "AppX_BannerAd";

	static BDBannerAd bannerAdView;
	BDInterstitialAd appxInterstitialAdView;
	
	private ProgressDialog dialog;
	
	//插屏广告
	public BDInterstitialAd getAd1(){
		final BDInterstitialAd appxInterstitialAdView1 = new BDInterstitialAd(this,"IcdAOe74YsFWW7wGpau4C6dbb7Zya7nT","4sw7cIpn9nBuwSX19wm8Ti6S");

		// 设置插屏广告行为监听器
		appxInterstitialAdView1.setAdListener(new InterstitialAdListener() {

			@Override
			public void onAdvertisementDataDidLoadFailure() {
				Log.e(TAG,"load failure");
				appxInterstitialAdView=getAd1();
			}

			@Override
			public void onAdvertisementDataDidLoadSuccess() {
				Log.e(TAG,"load success");
			}

			@Override
			public void onAdvertisementViewDidClick() {
				Log.e(TAG,"on click");
			}

			@Override
			public void onAdvertisementViewDidHide() {
				Log.e(TAG,"on hide");
			}

			@Override
			public void onAdvertisementViewDidShow() {
				Log.e(TAG,"on show");
			}

			@Override
			public void onAdvertisementViewWillStartNewIntent() {
				Log.e(TAG,"leave");
			}

		});

		// 加载广告
		appxInterstitialAdView1.loadAd();
		return appxInterstitialAdView1;
	   }
	
	
	public void getAd2(){
		bannerAdView = new BDBannerAd(this,"SvCiMQ1tehVx0hD6n94bTEhZ");

		// 设置横幅广告展示尺寸,如不设置,默认为SIZE_FLEXIBLE;
		bannerAdView.setAdSize(BDBannerAd.SIZE_FLEXIBLE);

		// 设置横幅广告行为监听器
		bannerAdView.setAdListener(new BannerAdListener() {

			@Override
			public void onAdvertisementDataDidLoadFailure() {
				Log.e(TAG1,"load failure");
			}

			@Override
			public void onAdvertisementDataDidLoadSuccess() {
				Log.e(TAG1,"load success");
			}

			@Override
			public void onAdvertisementViewDidClick() {
				Log.e(TAG1,"on click");
			}

			@Override
			public void onAdvertisementViewDidShow() {
				Log.e(TAG1,"on show");
			}

			@Override
			public void onAdvertisementViewWillStartNewIntent() {
				Log.e(TAG1,"leave app");
			}
		});
		
		RelativeLayout appxBannerContainer = new RelativeLayout(this);
		RelativeLayout.LayoutParams parentLayputParams = new RelativeLayout.LayoutParams(
		RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

		this.addContentView(bannerAdView,parentLayputParams);
	   }
	
	private Handler aHandler = new Handler()
	   {
	   	@Override
	       public void handleMessage(Message msg) {
	   		switch(msg.what)
	   	    {
	   		case 1:	//插屏广告
	   				if(appxInterstitialAdView.isLoaded()){
	   					appxInterstitialAdView.showAd();
	   					Log.d("LC","show");
	   				}else{
	   					appxInterstitialAdView.loadAd();
	   				}
	   				Log.d("LC","not show");
	   			break;
	   		case 2:	//banner广告
	   			getAd2();
   				Log.d("banner","show");
   			break;
	   		case 3:	//banner广告删除
	   			if(bannerAdView!=null){
	   				bannerAdView.destroy();
	   			}
   			break;
	   		case 4:	//自动更新
	   			myUpdate();
   			break;
	   	    }
	   	}
	   };
	   
		
	   
   public void myUpdate() {
	   dialog.show();
	   BDAutoUpdateSDK.uiUpdateAction(this,new MyUICheckUpdateCallback());
	   Log.e("Game Update","myUpdate success");
	}   
   
   
	
	private class MyUICheckUpdateCallback implements UICheckUpdateCallback {

		@Override
		public void onCheckComplete() {
			dialog.dismiss();
		}

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		appxInterstitialAdView=getAd1();
		
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(true);
		
		ShowAdHelper.init(aHandler);  
	}
}



ShowAdHelper.java

package org.cocos2dx.cpp;

import android.os.Handler;
import android.os.Message;

public class ShowAdHelper {
	private static Handler mHandler;
	
	public static void init(Handler handler)
	{
		ShowAdHelper.mHandler = handler;
	}
	
	public static void showMyAd(int adTag)
	{
		Message msg = mHandler.obtainMessage();
		msg.what = adTag;
		msg.sendToTarget();
	}
}

Java 部分的工作到这里就结束了,百度提供的demo主要是用于Android的,所以cocos2d-x的话,就必须多一个文件 ShowAdHelper.java方便C++通过jni调用 AppActivity.java中的内容


【步骤4 C++层的代码

到这里就比较简单了,建议新建一个类(这里建AdHelp.h 和 AdHelp.cpp),通过一些static函数调用广告的显示关闭


cocos官方文档对 jni 以及C++通过jni调用Java代码都有介绍,有兴趣深入了解的可查看官网文档


AdHelp.h

#ifndef __AdHelp__H__
#define __AdHelp__H__

#include <cocos2d.h>
using namespace cocos2d;

#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "platform/android/jni/JniHelper.h"
#include <jni.h>

#endif

class AdHelp
{
public:
	AdHelp();
	virtual ~AdHelp();
	//通过JNI调用JAVA静态函数,实现展示AD
	static void showAd(int adTag);

};

#endif



AdHelp.cpp
#include "AdHelp.h"

AdHelp::AdHelp()
{

}

AdHelp::~AdHelp()
{

}

void AdHelp::showAd(int adTag)
{

	//判断当前是否为Android平台 JniMethodInfo showAd;
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo minfo;
	bool isHave = JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/cpp/ShowAdHelper","showMyAd","(I)V");
	if (!isHave)
	{
		CCLog("jni:showAdStatic is null");
	}
	else
	{
		//调用函数
		minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,adTag);
	}
#endif
}



然后在合适的地方调用AdHelp::showAd(2)即可显示横屏广告,其他操作类似。


【步骤5 完工,看一下成果】

横屏广告 插屏广告


植入广告之后再次上传

百度手机助手

91助手

原文链接:https://www.f2er.com/cocos2dx/340787.html

猜你在找的Cocos2d-x相关文章