cocos2d-x 之 添加sns分享

来源:互联网 发布:嘉实优化红利基金 编辑:程序博客网 时间:2024/05/21 14:57
ios的比较成熟了,各个sns,像weobo,微信都有专门接口,这里不讨论了。这里说说Android平台,其实就是JNI,这里主要是c++调用Android的sdk,即调用到所在的Activity即可。

步骤一:

先写Activity内容: 其实就是share函数。

package org.jw.bj;import org.cocos2dx.lib.Cocos2dxActivity;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;public class AgainstLordTV extends Cocos2dxActivity{static Activity acty;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);acty = this;}    static {         System.loadLibrary("game");    }        public static void Share() {      new Thread(new Runnable() {       public void run() {        Intent intent = new Intent("android.intent.action.SEND");        intent.setType("image/*");        intent.putExtra(Intent.EXTRA_SUBJECT, "我的分享");        intent.putExtra(Intent.EXTRA_TEXT, "拍手庆祝吧!!!");        intent.putExtra(          Intent.EXTRA_STREAM,          Uri.parse("file:////data/data/" + acty.getApplicationInfo().packageName + "/share.png"));        System.out.println("/data/data/" + acty.getApplicationInfo().packageName + "/share.png");        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        acty.startActivity(Intent.createChooser(intent, "分享"));       }      }).start();    }}
当然,也可以放在单独一个类中。此处直接放在主Activity中(用cocos2d-x 创建 Android 项目时就只有一个Activity).

步骤二:

c++中,调用类cpp文件头中加入调用函数:

#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include <jni.h>#include "android/log.h"#include "platform/android/jni/JniHelper.h"extern "C"{void Share(){bool hasMethod;JniMethodInfo jni_methodInfo;hasMethod = JniHelper::getStaticMethodInfo(jni_methodInfo, "org/jw/bj/AgainstLordTV", "Share", "()V");if(hasMethod){CCLog("function Share() was found");if(jni_methodInfo.methodID){jni_methodInfo.env->CallStaticVoidMethod(jni_methodInfo.classID,  jni_methodInfo.methodID);CCLog("function Share() was called");}}else{CCLog("function Share() was not found");}}}#endif

步骤三:直接在Event函数中调用,即可。

void ServerGameScene::onSpritePositionCallback(CCObject *pSender){#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)Share();#endif}




原创粉丝点击