Call Android Toast Function in Cocos2dx by Using JNI Function
Propose : How to call jni function in cocos2dx develop enviroment.
I recommend that create Service class first,
Once you have service class, will reduce develop time for another app what you want to call method. just copy and paste service class file and re-use it.
Here is example to call Toast function in Cocos2dx. To do that create DeviceService class first.
[ Header File ]
#include "cocos2d.h"
USING_NS_CC;
using namespace std;
class DeviceServices
{
public:
DeviceServices();
~DeviceServices();
public:
static void showToastMessage(const std::string &iMsg);
}
[ Source File ]
#pragma execution_character_set("utf-8")
#include "DeviceServices.h"
DeviceServices::DeviceServices(){}
DeviceServices::~DeviceServices(){}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
const char* AppActivityClassName = "com/eyen/cpp/AppActivity";
void DeviceServices::showToastMessage(const std::string &iMsg)
{
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "jniShowToastMessage", "(Ljava/lang/String;)V"))
{
jstring word = t.env->NewStringUTF(iMsg.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, word);
t.env->DeleteLocalRef(t.classID);
}
}
#else
Simply done for C++ part.
To use it, just call "DeviceServices::showToastMessage("Hello") method where you want to call.
#include "DeviceServices.h"
void MainScene::welcome()
{
DeviceServices::showToastMessage("반가워요");
}
Really easy right?
Next step is that implement Java (android) part of code.
Just copy and paste below example code.
public class AppActivity extends Cocos2dxActivity {
// main
private static AppActivity _appActiviy;
// toast message
String mToastMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_appActiviy = this;
}
// show toast message
public void showToastMessage() {
Toast.makeText(getContext(), mToastMessage, Toast.LENGTH_SHORT).show();
}
// Jni call from Cocos2dx
public static void jniShowToastMessage(final String iWord) {
_appActiviy.mToastMessage = iWord;
_appActiviy.runOnUiThread(new Runnable(){
@Override
public void run() {
_appActiviy.showToastMessage();
}
});
}
}
Same way to call other function as like vibration, send email, send SMS and so on.
Good luck.! and Enjoy develop.
'English > Programming Tips' 카테고리의 다른 글
Android vibrate method in Unity Enviroment (0) | 2017.07.02 |
---|---|
Best CC0 sprites for mobile developer (0) | 2017.01.24 |
How to play cocos studio sprite animation (ActionTimeLine) (0) | 2016.11.04 |
How to use pugixml in android cocos2d develop enviroment (pugixml singleton / pugixml local path) (0) | 2016.10.05 |
How to set pugi xml in android cocos2d develop enviroment (0) | 2016.10.05 |
How to use text with enableOutline in cocos2dx / runtime text creation with outline (0) | 2016.10.05 |
kingdom rush frontiers stage 10 get 3 stars (0) | 2016.10.04 |
최근댓글