#include <string.h> #include "cocos2d.h" using namespace std; using namespace cocos2d; class InterfaceJNI { public: static void openTweetDialog(const char* tweet); };
#include "InterfaceJNI.h" #include "platform/android/jni/JniHelper.h" #include <jni.h> #include <android/log.h> #define JNICLASSNAME "org/cocos2dx/cpp/AppActivity" #define TWEET "tweet" void InterfaceJNI::openTweetDialog(const char* tweet) { JniMethodInfo methodInfo; if( JniHelper::getStaticMethodInfo(methodInfo , JNICLASSNAME , TWEET , "(Ljava/lang/String;)V") ) { jstring str = methodInfo.env->NewStringUTF(tweet); methodInfo.env->CallStaticVoidMethod(methodInfo.classID , methodInfo.methodID , str); methodInfo.env->DeleteLocalRef(str); methodInfo.env->DeleteLocalRef(methodInfo.classID); } }
: LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/InterfaceJNI.cpp \ ← この行を追加 ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp :
package org.cocos2dx.cpp; import org.cocos2dx.lib.Cocos2dxActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; public class AppActivity extends Cocos2dxActivity { static AppActivity my; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); my = this; } public static void tweet(String msg) { String url = "twitter://post?message="+Uri.encode(msg); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); my.startActivity(intent); } }
void HelloWorld::menuTweetCallback(cocos2d::Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) InterfaceJNI::openTweetDialog("アプリからのツイートテスト\n#test"); #endif }