twitter
Copyright (C) 2014 by Nobuhide Tsuda
twitter for Android
- C++ → Java インタフェースクラスを作成
- クラス名は InterfaceJNI とする
#include <string.h>
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;
class InterfaceJNI
{
public:
static void openTweetDialog(const char* tweet);
};
"InterfaceJNI.cpp":
#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);
}
}
"project/proj.android/jni/Android.mk" に "InterfaceJNI.cpp" を追加
:
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/InterfaceJNI.cpp \ ← この行を追加
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
:
AppActivity に onCreate(Bundle savedInstanceState) と tweet(String msg) を追加
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);
}
}
C++ コードから InterfaceJNI::openTweetDialog() をコール
void HelloWorld::menuTweetCallback(cocos2d::Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
InterfaceJNI::openTweetDialog("アプリからのツイートテスト\n#test");
#endif
}