想实现复制到剪贴板,调用android接口,JAVA函数出错

想要实现文字复制到剪贴板的功能,正在测试原生安卓的,已经可以通过jsb调用JAVA的函数了,测试了一下简单的JAVA函数,比如打印字符或者简单的加减都可以正常运行,现在想做复制到剪贴板的功能,JAVA函数不会写了,试了网上 androd复制的剪贴板 的方案,编译的时候都会报错
现在的代码如下
package org.cocos2dx.javascript;
import android.content.Context;
import android.os.Bundle;
import android.content.ClipData;
import android.content.ClipboardManager;
import static android.content.Context.CLIPBOARD_SERVICE;

public class OperatorClipboard
{
    public static void CopyStrtoClipboard( String str) 
    {
         //获取剪贴板管理器:
         ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
         // 创建普通字符型ClipData
         ClipData mClipData = ClipData.newPlainText("Label", str);
         // 将ClipData内容放到系统剪贴板里。
         cm.setPrimaryClip(mClipData);
    }
}

第一句应该就报错了 部分的编译报错log

Compiling with JDK Java compiler API.
/Users/zhangzhengbo/workspace/CCProject/GHGame/build/jsb-default/frameworks/runtime-src/proj.android-studio/app/src/org/cocos2dx/javascript/OperatorClipboard.java:15: 错误: 找不到符号
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
^
符号: 方法 getSystemService(String)
位置: 类 OperatorClipboard
1 个错误
:GHGame:compileDebugJavaWithJavac FAILED
:GHGame:compileDebugJavaWithJavac (Thread[Task worker for ‘:’ Thread 3,5,main]) completed. Took 0.212 secs.

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:GHGame:compileDebugJavaWithJavac’.

Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

  • Get more help at https://help.gradle.org

BUILD FAILED in 4s
42 actionable tasks: 2 executed, 40 up-to-date
Error running command, return code: 1.

求大神支支招,我就想实现 点击一个按钮,然后运行一下复制到剪贴板的函数,把我传进去的参数复制到剪贴板

2赞

后来把函数改写成这样

public class OperatorClipboard
{
    public static Context sContext;
    public static void CopyStrtoClipboard( String str) 
    {
        ClipboardManager clipboard = (ClipboardManager)OperatorClipboard.sContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集,包含一个普通文本数据条目(需要复制的数据)
        ClipData clipData = ClipData.newPlainText(null, str); 
        // 把数据集设置(复制)到剪贴板
        clipboard.setPrimaryClip(clipData);
    }
}

static Context sContext 这个变量在AppActivity onCreate中赋值了

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            // Android launched another instance of the root activity into an existing task
            //  so just quietly finish and go away, dropping the user back into the activity
            //  at the top of the stack (ie: the last state of this task)
            // Don't need to finish it again since it's finished in super.onCreate .
            return;
        }
        // DO OTHER INITIALIZATION BELOW
        
        SDKWrapper.getInstance().init(this);
        OperatorClipboard.sContext=this;
    }

还是没什么用 报错已经不报错了,但是剪贴板中还是没有内容

要放到 ui 线程里

context.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if(Build.VERSION.SDK_INT >= 11) {
            android.content.ClipboardManager cmb = (android.content.ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
            cmb.setPrimaryClip(android.content.ClipData.newPlainText(null, content));
        }
    }
});
1赞

没有报错了 但是好像没起什么作用,运行了这个复制函数以后,我进行粘贴 无论是在退出了app 还是 在app里面 都没有粘贴成功

多谢大神 我自己有个地方写错了 现在弄好了

你不加runOnUiThread 线程可以用?

就是按照你给的代码写的 放在UI线程里面