Skip to content

Instantly share code, notes, and snippets.

@cketti
Last active June 13, 2016 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cketti/77a3ca308c7528f3fa6febeeedaa0fd0 to your computer and use it in GitHub Desktop.
Save cketti/77a3ca308c7528f3fa6febeeedaa0fd0 to your computer and use it in GitHub Desktop.
Activity that copies the URL from the Intent to the clipboard
public class CopyToClipboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
if (uri != null) {
copyTextToClipboard(uri.toString());
Toast.makeText(this, "Link copied to clipboard", Toast.LENGTH_SHORT).show();
}
// Finish right away. We don't want to actually display a UI.
finish();
}
private void copyTextToClipboard(String url) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("URL", url);
clipboard.setPrimaryClip(clip);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment