有米广告平台介绍:有米SDK
关于有米的一些信息设置,官网表述的已经非常清楚,此处不再赘述,本教程是针对Android机器。
直接上代码
1】C++部分
本教程基于Cocos2d-x的HelloWorld空程序,所以代码非常简单,不需修改C++部分代码
2】Java代码
1、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 net.youmi.android.AdManager; import net.youmi.android.banner.AdSize; import net.youmi.android.banner.AdView; import net.youmi.android.banner.AdViewListener; import net.youmi.android.spot.SpotDialogListener; import net.youmi.android.spot.SpotManager; import org.cocos2dx.lib.Cocos2dxActivity; import org.cocos2dx.lib.Cocos2dxEditText; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; import org.cocos2dx.lib.Cocos2dxRenderer; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import com.yxk.youmi.R; public class AppActivity extends Cocos2dxActivity { // SpotManager.getInstance(sActivity).showSpotAds(sActivity); private Cocos2dxGLSurfaceView mGLView;//add private static Context mContext;//add protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; //add LayoutParams frameLayout_paras = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT); FrameLayout frameLayout = new FrameLayout(this); frameLayout.setLayoutParams(frameLayout_paras); ViewGroup.LayoutParams edittext_layout_params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); Cocos2dxEditText editText = new Cocos2dxEditText(this); editText.setLayoutParams(edittext_layout_params); frameLayout.addView(editText); mGLView = new Cocos2dxGLSurfaceView(this); frameLayout.addView(mGLView); mGLView.setEGLContextClientVersion(2); mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); setContentView(frameLayout); LinearLayout adMiniLayout = new LinearLayout(this); adMiniLayout.setOrientation(LinearLayout.VERTICAL); FrameLayout.LayoutParams lp_mini = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); //设置adMiniLayout的悬浮位置,具体的位置开发者根据需要设置 lp_mini.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;//这里确定广告的位置 this.addContentView(adMiniLayout,lp_mini); LinearLayout miniLayout = new LinearLayout(this); miniLayout.setGravity(Gravity.CENTER_HORIZONTAL); adMiniLayout.addView(miniLayout); AdManager.getInstance(this).init("937f81391fe278d0","11f447b520039ceb",false); AdView adView = new AdView(this,AdSize.FIT_SCREEN); miniLayout.addView(adView); // 监听广告条接口 adView.setAdListener(new AdViewListener() { @Override public void onSwitchedAd(AdView arg0) { Log.i("YoumiAdDemo","广告条切换"); } @Override public void onReceivedAd(AdView arg0) { Log.i("YoumiAdDemo","请求广告成功"); } @Override public void onFailedToReceivedAd(AdView arg0) { Log.i("YoumiAdDemo","请求广告失败"); } }); SpotManager.getInstance(this).loadSpotAds(); SpotManager.getInstance(this).setShowInterval(20);// 设置20秒的显示时间间隔 SpotManager.getInstance(this).setSpotOrientation( SpotManager.ORIENTATION_PORTRAIT); } /* Button spotBtn = (Button) findViewById(R.id.showSpot); spotBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 展示插播广告,可以不调用loadSpot独立使用 SpotManager.getInstance(AppActivity.this).showSpotAds( AppActivity.this,new SpotDialogListener() { @Override public void onShowSuccess() { Log.i("YoumiAdDemo","展示成功"); } @Override public void onShowFailed() { Log.i("YoumiAdDemo","展示失败"); } @Override public void onSpotClosed() { Log.e("YoumiAdDemo","插屏关闭"); } }); // // // 可以根据需要设置Theme,如下调用,如果无特殊需求,直接调用上方的接口即可 // SpotManager.getInstance(YoumiAdDemo.this).showSpotAds(YoumiAdDemo.this,// android.R.style.Theme_Translucent_NoTitleBar); // // } }); Button diyBtn = (Button) findViewById(R.id.diy); diyBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(AppActivity.this,DiyDemo.class); startActivity(intent); } }); } */ // setContentView(R.layout.main);//del /* AdManager.getInstance(this).init("937f81391fe278d0",false); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); // 设置广告条的悬浮位置 layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角 // 实例化广告条 AdView adView = new AdView(this,AdSize.FIT_SCREEN); // 调用Activity的addContentView函数 this.addContentView(adView,layoutParams); // 监听广告条接口 adView.setAdListener(new AdViewListener() { @Override public void onSwitchedAd(AdView arg0) { Log.i("YoumiAdDemo","请求广告失败"); } }); SpotManager.getInstance(this).loadSpotAds(); SpotManager.getInstance(this).setShowInterval(20);// 设置20秒的显示时间间隔 SpotManager.getInstance(this).setSpotOrientation( SpotManager.ORIENTATION_PORTRAIT); Button spotBtn = (Button) findViewById(R.id.showSpot); spotBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 展示插播广告,可以不调用loadSpot独立使用 SpotManager.getInstance(AppActivity.this).showSpotAds( AppActivity.this,DiyDemo.class); startActivity(intent); } }); } */ @Override public void onBackPressed() { // 如果有需要,可以点击后退关闭插播广告。 if (!SpotManager.getInstance(AppActivity.this).disMiss(true)) { super.onBackPressed(); } } @Override protected void onStop() { // 如果不调用此方法,则按home键的时候会出现图标无法显示的情况。 SpotManager.getInstance(AppActivity.this).disMiss(false); super.onStop(); } @Override protected void onDestroy() { SpotManager.getInstance(this).unregisterSceenReceiver(); super.onDestroy(); } }
2、DiyDemo.java
package org.cocos2dx.cpp; import java.util.List; import com.yxk.youmi.R; import net.youmi.android.diy.AdObject; import net.youmi.android.diy.DiyManager; import net.youmi.android.diy.banner.DiyAdSize; import net.youmi.android.diy.banner.DiyBanner; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; public class DiyDemo extends Activity{ private Button showRecommendWallBtn,showRecommendAppWallBtn,showRecommendGameWallBtn; private List<AdObject> adList; public void onCreate(Bundle bundle){ super.onCreate(bundle); setContentView(R.layout.activity_diydemo); //普通布局,适用于应用 //获取要嵌入迷你广告条的布局 RelativeLayout adLayout=(RelativeLayout)findViewById(R.id.adLayout); //demo 1 迷你Banner : 宽满屏,高32dp DiyBanner banner = new DiyBanner(this,DiyAdSize.SIZE_MATCH_SCREENx32);//传入高度为32dp的AdSize来定义迷你Banner //demo 2 迷你Banner : 宽320dp,高32dp //DiyBanner banner = new DiyBanner(this,DiyAdSize.SIZE_320x32);//传入高度为32dp的AdSize来定义迷你Banner //将积分Banner加入到布局中 adLayout.addView(banner); //悬浮布局,适用于游戏 /* //实例化LayoutParams(重要) FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); //设置迷你Banner的悬浮位置 layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角 //实例化迷你Banner DiyBanner banner = new DiyBanner(this,DiyAdSize.SIZE_MATCH_SCREENx32);//传入高度为32dp的DiyAdSize来定义迷你Banner //调用Activity的addContentView函数 this.addContentView(banner,layoutParams); */ showRecommendWallBtn = (Button) findViewById(R.id.showRecommendWall); showRecommendWallBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 展示所有应用推荐墙 DiyManager.showRecommendWall(DiyDemo.this); } }); showRecommendAppWallBtn = (Button) findViewById(R.id.showRecommendAppWall); showRecommendAppWallBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 展示应用推荐墙 DiyManager.showRecommendAppWall(DiyDemo.this); } }); showRecommendGameWallBtn = (Button) findViewById(R.id.showRecommendGameWall); showRecommendGameWallBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 展示游戏推荐墙 DiyManager.showRecommendGameWall(DiyDemo.this); } }); } }
3、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yxk.youmi" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> <uses-sdk android:minSdkVersion="11"/> <uses-feature android:glEsVersion="0x00020000" /> <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" /> <activity android:name="org.cocos2dx.cpp.AppActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="net.youmi.android.AdBrowser" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" android:theme="@android:style/Theme.Light.NoTitleBar" > </activity> <service android:name="net.youmi.android.AdService" android:exported="false" > </service> <receiver android:name="net.youmi.android.AdReceiver" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiver> <provider android:name="net.youmi.android.spot.SpotAdContentProvider" android:authorities="com.youandroidmi" /> <activity android:name="org.cocos2dx.cpp.DiyDemo" android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize" > </activity> <Meta-data android:name="YOUMI_CHANNEL" android:value="0" > </Meta-data> </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.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.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>运行效果
原文链接:https://www.f2er.com/cocos2dx/345547.html