andrid developers

java.lang.Object

---android.text.ClipboardManager

------android.context.ClipboardManager

Interface to the clipboard service, for placing and retrieving text in the global clipboard.

You do not instantiate this class directly; instead, retrieve it through getSystemService(Class).

The ClipboardManager API itself is very simple: it consists of methods to atomically get and set the current primary clipboard data. That data is expressed as a ClipData object, which defines the protocol for data exchange between applications.

SDK11之前请使用android.text.ClipboardManager

import android.text.ClipboardManager;

……

ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("内容");
if (clipboardManager.hasText()){
clipboardManager.getText();
}

SDK11起android.text.ClipboardManager被废弃,使用它的子类android.content.ClipboardManager替代

同样被废弃还有setText/getText/hasText方法,使用setPrimaryClip/getPrimaryClip/hasPrimaryClip替代

import android.content.ClipboardManager;

……

ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, "内容"));
if (clipboardManager.hasPrimaryClip()){
clipboardManager.getPrimaryClip().getItemAt(0).getText();
}
04-25 21:52