The clipboard code that works for API levels < 11 crashes on devices with API levels >= 11.
The clipboard code that work for API level >= 11 crashes on devices wi
I recently faced a similar problem. Here's how I handled it.
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "Text to Copy");
clipboard.setPrimaryClip(clip);
} else{
android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText("Text to Copy");
}
Toast.makeText(getApplicationContext(), "Text copied to clipboard", Toast.LENGTH_SHORT).show();
I'm not entirely sure if the first if block is necessary. But I'd rather not take a chance :)