cocos2dx复制内容到剪贴板

前端之家收集整理的这篇文章主要介绍了cocos2dx复制内容到剪贴板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

android

c++中

javar 类型签名

类型签名 Java类型
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L full-qualified-class; 完全限定的类,分号必须要有
[ type type[ ]
(arg-types) ret-type (参数类型列表)返回值类型
int copyToClipboard(const std::string& paras) {
        JniMethodInfo minfo;
        if (JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/javascript/AppActivity","copyToClipboard","(Ljava/lang/String;)I"))
        {
            jstring jstr = minfo.env->NewStringUTF(paras.c_str());

            int code = minfo.env->CallStaticIntMethod(minfo.classID,minfo.methodID,jstr);
             return code;
        }
#endif
        return -1;
    }

java中

public class AppActivity extends Cocos2dxActivity {
    static AppActivity sgsActivity;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sgsActivity = this;

    }
    //参数要加final关键字,否则内部类不能访问
    static public int copyToClipboard(final  String text)
    {
        final Context context = sgsActivity;//参数要加final关键字,否则内部类不能访问
        try
        {


            Log.d("cocos2dx","copyToClipboard " + text);
            Runnable runnable = new Runnable() {
                public void run() {
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                    android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text",text);
                    clipboard.setPrimaryClip(clip);
                }
            };
            //getSystemService运行所在线程必须执行过Looper.prepare()
            //否则会出现Can't create handler inside thread that has not called Looper.prepare()
            sgsActivity.runOnUiThread(runnable);

        }catch(Exception e){
            Log.d("cocos2dx","copyToClipboard error");
            e.printStackTrace();
            return -1;
        }
        return 0;
    }

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