Yomob广告在cocos2dx安卓平台的Demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yomob广告在cocos2dx安卓平台的Demo相关的知识,希望对你有一定的参考价值。

Yomob 也是一个广告平台:他的使用方式比较简单哈,这里只说说他的步骤

按照官方文档说明配置环境,我使用的是eclipse,在eclipse上自己将文件copy到项目proj.android下,覆盖掉源目录

将项目下的libs目录下的所有的jar文件,选中右键build Path

技术分享

 

添加如下代码到Manifest.xml文件的<application>标签下:

        <!-- Inmobi -->
        <activity 
            android:name="com.inmobi.rendering.InMobiAdActivity"
            android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:hardwareAccelerated="true" />
        <receiver
            android:name="com.inmobi.commons.core.utilities.uid.ImIdShareBroadCastReceiver"
            android:enabled="true"
            android:exported="true" >
                <intent-filter>
                    <action android:name="com.inmobi.share.id" />
                </intent-filter>
         </receiver>

        <!-- TG CP -->
        <activity android:name="com.soulgame.sgsdk.adsdk.TGCPADActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

        <!-- DianJoy -->
        <service 
            android:name="com.dianjoy.video.DianViewService" 
            android:process=":playgame" />
        <activity
            android:name="com.dianjoy.video.DianViewActivity"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"/>
        <activity 
            android:name="com.dianjoy.video.DianViewAdBrowser" 
            android:configChanges="orientation|screenSize"/>

        <!-- AYang -->
        <activity
            android:name="com.afk.client.ads.AdActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="sensor"
            android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" />


        <activity
            android:name="com.afk.permission.RequestPermissionActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="sensor"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

        <service
            android:name="com.afk.client.ads.DownloadService"
            android:exported="false" />

        <!-- Vungle -->
        <activity
            android:name="com.vungle.publisher.VideoFullScreenAdActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
        <activity android:name="com.vungle.publisher.MraidFullScreenAdActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>

        <!-- Domob -->
        <activity 
            android:name="com.pad.android_independent_video_sdk.view.DvxVideoActivity"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:configChanges="orientation|keyboard|screenSize|screenLayout"/>
        <service android:name="com.pad.android_independent_video_sdk.IndependentService"/>

需要说明的是以上是国内安卓的sdk;其他的sdk请按照官方的文档配置。

直接添加权限到Manifest.xml下的<manifest.xml>的标签下:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <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_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />

添加官方给的TGSDKCocos2dxHelper.h和TGSDKCocos2dxHelper.cpp到Classes文件下,记得要在Android.mk文件里面添加他的路径

然后就可以直接在cocos2dx的C++代码里面调用使用了,还有一点需要使用说明的,AppActivity.java 里面需要加入以下代码,否则会报Activity为null。

/****************************************************************************
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 java.util.Map;

import org.cocos2dx.lib.Cocos2dxActivity;

import com.soulgame.sgsdk.tgsdklib.TGSDK;
import com.soulgame.sgsdk.tgsdklib.TGSDKServiceResultCallBack;
import com.soulgame.sgsdk.tgsdklib.ad.ITGADListener;
import com.soulgame.sgsdk.tgsdklib.ad.ITGPreloadListener;
import com.soulgame.sgsdk.tgsdklib.ad.ITGRewardVideoADListener;
import com.soulgame.sgsdk.tgsdklib.cocos2dx.TGSDKCocos2dxHelper;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;


public class AppActivity extends Cocos2dxActivity {
    
    private static AppActivity _appActiviy;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//横屏:根据传感器横向切换
        TGSDK.setDebugModel(true);
        TGSDKCocos2dxHelper.setup(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        TGSDK.onStart(this);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        TGSDK.onStop(this);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        TGSDK.onPause(this);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        TGSDK.onResume(this);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        TGSDK.onDestroy(this);
    }
    
    @Override
    protected void onActivityResult(int reqCode, int resCode, Intent data) {
        super.onActivityResult(reqCode, resCode, data);
        TGSDK.onActivityResult(this, reqCode, resCode, data);
    }

}

 

Yomob的调用代码很简单:

    yomob::TGSDKCocos2dxHelper::setDebugModel(true);
    yomob::TGSDKCocos2dxHelper::initialize("p196wHvufx005lPj2P63");//参数需要自己申请生成,需要特定的包名才能正常显示广告哦,而且他们的资源有时候没有广告,所以要注意下方log信息。
    yomob::TGSDKCocos2dxHelper::preload();

    if (yomob::TGSDKCocos2dxHelper::couldShowAd("vVxf8P1riB0IVfalxPy")) {
            yomob::TGSDKCocos2dxHelper::showAd("vVxf8P1riB0IVfalxPy");

    }

这几行代码官方都有说明,在你需要调用的地方调用即可。

技术分享

 

技术分享

 

 

以下给出GitHub Demo·的源代码地址:https://github.com/DiaoYung/YomobDemo;诸位可以在github上下载源码实践。然后运用到自己的项目中。

项目正在上传,请稍后!

 

以上是关于Yomob广告在cocos2dx安卓平台的Demo的主要内容,如果未能解决你的问题,请参考以下文章

cocos2dx 在Mac平台下利用Eclipse打包安卓apk安装包详细教程

做安卓平台游戏开发需要学啥?

KeyMob移动广告聚合平台专注于IOS安卓手机广告

Cocos2dx开发游戏移植到Android平台

Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解

cocos2dx-3.1 接入多盟广告sdk+Android