cocos2d-x 之 添加sns分享

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
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值