Android复制文本内容到系统剪贴板(ClipboardManager)

小米手机,EditeText长按后没有显示让粘贴复制的内容
原因:
使用的api过时,之前使用的方法:

ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        try {
                            MsgEntity msgentity = new Gson().fromJson(choseimMessage.getContent(), MsgEntity.class);
                            cmb.setText(msgentity.getMsgString());
                        } catch (Exception e) {
                            CharSequence text = cmb.getText();
                            if (null != text)
                                cmb.setText(choseimMessage.getContent());
                        }

修改后使用的方法

//获取剪贴板管理器:
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        try {
            MsgEntity msgentity = new Gson().fromJson(choseimMessage.getContent(), MsgEntity.class);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", msgentity.getMsgString());
            // 将ClipData内容放到系统剪贴板里。
            cmb.setPrimaryClip(mClipData);

        } catch (Exception e) {
            ClipData mClipData = ClipData.newPlainText("Label", choseimMessage.getContent());
            if (null != cmb) {
                cmb.setPrimaryClip(mClipData);
            }
        }

https://blog.csdn.net/yjhdxflqm/article/details/51152898
https://blog.csdn.net/qq_36487432/article/details/82380786
https://www.jianshu.com/p/555c02931671
https://www.jianshu.com/p/52950080a4aa
https://www.jianshu.com/p/1e84d33154bd
https://blog.csdn.net/qq_22078107/article/details/53447905
https://ask.csdn.net/questions/389858
https://blog.csdn.net/Queen_332/article/details/79315122
https://blog.csdn.net/Jiang_Rong_Tao/article/details/61919364
https://cloud.tencent.com/info/53a2f5d9f5e173641b6949947244e318.html
https://ask.csdn.net/questions/213686?sort=comments_count
https://www.cnblogs.com/exmyth/p/4603513.html

你可能感兴趣的:(android)