1

I have a project for share clipboard content between phone and desktop service.

When I try getText from clipboard it return empty string. It worked previously for a few months on a Android Pie.

ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
String clipContent = "";
MainActivity activity = ((MainActivity) getActivity());
if (clipboard.hasPrimaryClip()) {
  ClipData clipData = clipboard.getPrimaryClip();
  ClipData.Item item = clipData.getItemAt(0);
  clipContent = item.getText().toString();
} 

See MainActivity.onActivityResult(). Please help find solution.

Papp Zoltán
  • 131
  • 7

2 Answers2

0

It will be enough to give the value you want to copy.

  ClipboardManager clipboardManager = (ClipboardManager)
                            getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clipData = ClipData.newPlainText("nonsense_data",
                            mResultEt.getText().toString());
                    clipboardManager.setPrimaryClip(clipData);
                    Toast.makeText(MainActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
0

I solved the problem. Clipboard content was not text type.

Papp Zoltán
  • 131
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 24 '22 at 12:41