cocos2dx之友盟统计(android/ios)
一、玩家登录,在lua代码中获取玩家的帐号信息 ---------账号登录统计---------------
if device.platform == "android" then
local args = { 1,tostring(PlayerId),1 }
local luaj = require "cocos.cocos2d.luaj"
local sigs = "(ILjava/lang/String;I)I"
local className = "com/cocos2dx/sample/LuaJavaBridge"
local ok,ret = luaj.callStaticMethod(className,"sendLuaToJavaProfileSignIn",args,sigs)
if not ok then
print("luaj error:",ret)
else
print("The ret is:",ret)
end
elseif device.platform == "ios" then
iosProfileSignInWithPUID(tostring(PlayerId))
-- iosProfileSignInWithPUID("playerID_str_param")
end
---------账号登录统计---------------
lua中获取信息后,通过不同平台来调用不同平台的方法。 android情况:项目文件下,src–>com.cocos2dx.sample–>LuaJavaBridge.java // 账号登录统计
public static int sendLuaToJavaProfileSignIn(int type,String UserID,final int luaFuncID){
//System.out.printf("%n","sendLuaToJavaProfileSignIn **********************************");
//Log.d("tag","正sendLuaToJavaProfileSignIn播放中");
MobclickAgent.onProfileSignIn(UserID); //友盟账号登录统计的方法
return 1;
}
调用了友盟统计登录的方法即可。 @Override
protected void onResume() {
super.onResume();
//添加友盟的部分
MobclickAgent.onResume(this);
}
@Override
public void onPause(){
super.onPause();
//添加友盟的部分
MobclickAgent.onPause(this);
}
在项目文件下,找到如下,在其中添加 <application>
<!-- umeng Sdk start -->
<meta-data android:value="584e516aa3251114e10012a3" android:name="UMENG_APPKEY"/>
<meta-data android:value="umeng" android:name="UMENG_CHANNEL"/>
<!-- umeng Sdk end -->
</application>
“584e516aa3251114e10012a3”:在友盟后台申请的应用Appkey(根据自己申请的来填写)。 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
添加权限。 iOS情况:AppDelegate.h中,声明函数 //友盟统计登录
static int iosProfileSignInWithPUID(lua_State *L);
AppDelegate.cpp中,applicationDidFinishLaunching里面,lua注册函数 lua_register(L,"iosProfileSignInWithPUID",iosProfileSignInWithPUID);
以及实现函数 // lua 调用 c 友盟账号统计
int AppDelegate::iosProfileSignInWithPUID(lua_State *L)
{
if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
{
//从lua处得到账号信息,lua里面会调用已经注册的该函数iosProfileSignInWithPUID
std::string playerId = lua_tostring(L,1);
printf("玩家的playerID: %sn",playerId.c_str());
//新建一个接口,这个接口能被c/c++函数iosProfileSignInWithPUID调用,接口中又可以调用oc代码,lua代码又可以调用该函数iosProfileSignInWithPUID
UMProfile::profileSignInWithPUID(playerId.c_str());
}
return 1;
}
创建该接口 // 实现文件 UMProfile.cpp
#include "UMProfile.h"
//导入友盟sdk
#import "UMMobClick/MobClick.h"
//--------------------------------------------------
void UMProfile::profileSignInWithPUID(const char* userID)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//获取用户账号后转换string类型
NSString *strNSString = [[NSString alloc] initWithUTF8String:userID];
//调用友盟iOS统计函数即可。
[MobClick profileSignInWithPUID:strNSString];
#endif
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |