Android copy/paste from clipboard manager

前端 未结 7 1040
情书的邮戳
情书的邮戳 2020-11-28 10:04

Is it possible to send past command so that it pastes text into currently focused edit text. Scenario:

  1. Background service listening for notification (done)
7条回答
  •  眼角桃花
    2020-11-28 10:43

    you can copy and paste text using following code :

    • for copy :

      ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
      ClipData clip = ClipData.newPlainText("your_text_to_be_copied");
      clipboard.setPrimaryClip(clip);
      
    • And paste it :

      ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
      String pasteData = "";
      
       // If it does contain data, decide if you can handle the data.
      if (!(clipboard.hasPrimaryClip())) {
      
      } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
      
          // since the clipboard has data but it is not plain text
      
      } else {
      
          //since the clipboard contains plain text.
          ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
      
          // Gets the clipboard as text.
          pasteData = item.getText().toString(); 
      }
      

    for more details check here

提交回复
热议问题