參考 http://blog.csdn.net/mydreamremindme/article/details/49229205
1.1 程式碼增加
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
bool PageMain::copyToClipboard(const std::string &content)
{
return copyToClipboardJNI(content.c_str());
}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
bool PageMain::copyToClipboard(const std::string &content)
{
if (OpenClipboard(NULL))
{
EmptyClipboard();
HGLOBAL clipbuffer;
char* buffer;
const char* str = content.c_str();
const int length = strlen(str);
clipbuffer = GlobalAlloc(GMEM_DDESHARE, length + 1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, str);
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT, clipbuffer);
CloseClipboard();
return true;
}
return false;
}
#endif
1.2 增加對應的函數 讓C呼叫
cocos2d-x\cocos\platform\android\jni\Java_org_cocos2dx_lib_Cocos2dxHelper.cpp
extern bool copyToClipboardJNI(const char* content)
{
JniMethodInfo t;
bool ret = false;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxHelper", "copyToClipboard", "(Ljava/lang/String;)Z"))
{
jstring stringArg = t.env->NewStringUTF(content);
ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID,stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
return ret;
}
1.3 撰寫JAVA 的程式碼
cocos2d-x\cocos\platform\android\java\src\org\cocos2dx\lib\Cocos2dxHelper.java
1.3.1 宣告
在 public class Cocos2dxHelper { 增加一行
//example in line 94
private static android.content.ClipboardManager clipboardmanager = null;
1.3.2 設定對應函數
在 public static void init(final Activity activity) { 增加一行
//example in line 167 Cocos2dxBitmap.setContext(activity); 之上
Cocos2dxHelper.clipboardmanager = (android.content.ClipboardManager)activity.getSystemService(Context.CLIPBOARD_SERVICE);//獲取剪貼板服務
1.3.3 JAVA與C的接口
在 public static void init(final Activity activity) 函數之後 增加下列函數
//example in line 195 找地方寫函數就對了
public static boolean copyToClipboard(String content){
clipboardmanager.setText(content);
System.out.println("come here");
return true;
}