Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信

前端之家收集整理的这篇文章主要介绍了Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、Jni不在赘述,翻看前面博客

2、直接上代码

1)Java层,直接加在AppActivity.java中

public class AppActivity extends Cocos2dxActivity{
	public static Activity acty;
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		acty = this;
	}
	
	static {
		System.loadLibrary("cocos2dcpp");
	}
	public static void Share(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("smsto:18928475901");
				Intent it = new Intent(Intent.ACTION_SENDTO,uri);
				it.putExtra("sms_body","短信内容");
				System.out.println("test");
				it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				acty.startActivity(Intent.createChooser(intent,"分享"));
				acty.startActivity(it);
			}
		}).start();
	}
}

2)Jni层:Jni/hellocpp/test类的.h和.cpp代码如下

test.h
#ifndef TEST_H_
#define TEST_H_
extern "C"
{
	//C++调Java的函数接口,该方法在HelloWorldScene中menuCallback函数中使用。
//	void showTipDialog(const char* title,const char* msg);
	void Share();
}
#endif
test.cpp 注意,这两段代码是在之前代码修改的,部分代码与此功能无关。请自行删除
#include "test.h"
#include "cocos2d.h"
#include "platform/android/jni/JniHelper.h"
#include "../../../Classes/JniTest.h"
#include <jni.h>

#define CLASS_NAME "org/cocos2dx/cpp/JniTestHelper"
#define CLASS_NAMENEW "org/cocos2dx/cpp/AppActivity"
using namespace cocos2d;
extern "C"
{
	void Share()
	{
		bool hasMethod;
		JniMethodInfo t;
		hasMethod = JniHelper::getStaticMethodInfo(t,CLASS_NAMENEW,"Share","()V");
		if(hasMethod)
		{
			log("Share function");
			if(t.methodID)
			{
				t.env->CallStaticVoidMethod(t.classID,t.methodID);
				log("function share() was called");
			}else{
				log("function share was not called");
			}
		}else
		{
			log("function share was not found");
		}
	}
}

3)C++层

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//    showTipDialog("exit","Exit,Really Go?");
    Share();//调用tes.cpp中的Share()函数,该函数调用Java中的Share()函数
#endif
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

4)注意

在mk文件加上test.cpp文件路径。
在头文件加上平台判断,代码如下:
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "../proj.android/jni/hellocpp/test.h"
#endif
原文链接:https://www.f2er.com/cocos2dx/346468.html

猜你在找的Cocos2d-x相关文章