Home > Net >  android 9 get content from clipboard programmatically
android 9 get content from clipboard programmatically

Time:07-22

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.

CodePudding user response:

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();

CodePudding user response:

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

  • Related