当前位置 : 主页 > 编程语言 > c++ >

Unity3d调用Android内服务,需开辟新的线程

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt //因调用getSystemService()方法引发报错://Can't create handler inside thread that has not called Looper.prepare()//使用Runnable()开辟线程调用//final关键字可以将paramStr设为方法内共有参数public
gistfile1.txt
//因调用getSystemService()方法引发报错:
//Can't create handler inside thread that has not called Looper.prepare()
//使用Runnable()开辟线程调用

//final关键字可以将paramStr设为方法内共有参数
public void CopyToSystemClipboard(final String paramStr){
    Log.d("Unity", "=======CopyToSystemClipboard " + paramStr);
    //runOnUiThread方法调用一个UI线程
    runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    // 向剪贴板中添加文本
                    clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData textCd = ClipData.newPlainText("data", paramStr);
                    clipboard.setPrimaryClip(textCd);
                    mUnityPlayer.UnitySendMessage("GameManagerPrefeb", "OnShowCopySucessMsg", paramStr);
                }
            }
    );

}
网友评论