Java tutorial
//package com.java2s; import android.annotation.SuppressLint; import android.content.ClipData; import android.content.Context; import android.os.Build; public class Main { private static Context sCurrentContext; /** * <pre> * Copy text to clipboard. * Different implementation for API < 11 and API >= 11. * </pre> */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") public static void copyTextToClipboard(String text) { if (Build.VERSION.SDK_INT < 11) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getCurrentContext() .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(text); } else { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getCurrentContext() .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("PoketChat Copy To Clipboard", text)); } } /** * <pre> * Get current context of the app. This method resolves the inconvenience of Android which requires context for most of its API. * If no activity is resumed, this method returns application context. Otherwise, this method returns last resumed activity. * </pre> */ public static Context getCurrentContext() { return sCurrentContext; } }