0

I'm using this code to put items on the clipboard and it works fine, apps can paste what I put there just fine, but SwiftKey never shows on their history what I put on the clipboard. What am I doing wrong?

ClipboardManager clipboard = (ClipboardManager) context.
                getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null) {
            ClipData clip = ClipData.newRawUri("URL", Uri.parse(url));
            if (clip != null) {
                clipboard.setPrimaryClip(clip);
            } else {
                Log.w(TAG, "Failed to copy to clipboard");
            }
        } else {
            Log.w(TAG, "Unexpected error because clipboard is null");
        }

Edit: Made a sample project just to test it and still failed:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val clipboard: ClipboardManager =
            getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        if (clipboard != null) {
            val clip = ClipData.newRawUri("URL", Uri.parse("http://google.com"))
            if (clip != null) {
                clipboard.setPrimaryClip(clip)
            } else {
                Log.w("MainActivity", "Failed to copy to clipboard")
            }
        } else {
            Log.w("MainActivity", "Unexpected error because clipboard is null")
        }
    }
}

I can paste just fine but here is what SwiftKey shows:

enter image description here

casolorz
  • 8,486
  • 19
  • 93
  • 200
  • I complied with `API 29` and tested it on `Android 9` with `SwiftKey version 7.4.6.6`. It worked fine. Could you please share more details? – Mir Milad Hosseiny Jan 20 '20 at 23:10
  • Thanks for doing that, but that is all there is to the code. Did you try it with a url? maybe you can share your code. I'm editing the post to show a sample project I just made and still have the issue. – casolorz Jan 21 '20 at 15:26
  • I'm sorry. I checked my code and figured out I tried with `newPlainText` method. – Mir Milad Hosseiny Jan 21 '20 at 16:17
  • Wonder if url just doesn't work, maybe I need to switch to plain text. Can't see much harm on that, it is just a clipboard. – casolorz Jan 21 '20 at 17:10
  • I just tested and `newPlainText()` shows up fine so I can just use that. If you want to make it an answer I can mark it as correct. – casolorz Jan 24 '20 at 21:15

1 Answers1

1

Use ClipData.newPlainText() method instead of ClipData.newRawUri() method. So your code could be as below:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val clipboard: ClipboardManager =
            getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        if (clipboard != null) {
            val clip = ClipData.newPlainText("URL", "http://google.com")
            if (clip != null) {
                clipboard.setPrimaryClip(clip)
            } else {
                Log.w("MainActivity", "Failed to copy to clipboard")
            }
        } else {
            Log.w("MainActivity", "Unexpected error because clipboard is null")
        }
    }
}
Mir Milad Hosseiny
  • 2,769
  • 15
  • 19