Dealing with deprecated android.text.ClipboardManager

后端 未结 4 655
情歌与酒
情歌与酒 2021-02-07 13:09

android.text.ClipboardManager was deprecated since API level 11, and replaced with android.content.ClipboardManager (source).

How do I write c

4条回答
  •  清歌不尽
    2021-02-07 14:02

    Referring to this answer:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    final android.content.ClipData clipData = android.content.ClipData
            .newPlainText("text label", "text to clip");
    clipboardManager.setPrimaryClip(clipData);
    } else {
    final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setText("text to clip");
    }
    

提交回复
热议问题