FileDocCategorySizeDatePackage
ClipboardManager.javaAPI DocAndroid 5.1 API7675Thu Mar 12 22:22:10 GMT 2015android.content

ClipboardManager

public class ClipboardManager extends android.text.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 {@link android.content.Context#getSystemService}.

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 {@link ClipData} object, which defines the protocol for data exchange between applications.

Developer Guides

For more information about using the clipboard framework, read the Copy and Paste developer guide.

see
android.content.Context#getSystemService

Fields Summary
private static final Object
sStaticLock
private static IClipboard
sService
private final android.content.Context
mContext
private final ArrayList
mPrimaryClipChangedListeners
private final IOnPrimaryClipChangedListener.Stub
mPrimaryClipChangedServiceListener
static final int
MSG_REPORT_PRIMARY_CLIP_CHANGED
private final android.os.Handler
mHandler
Constructors Summary
public ClipboardManager(android.content.Context context, android.os.Handler handler)
{@hide}

        mContext = context;
    
Methods Summary
public voidaddPrimaryClipChangedListener(android.content.ClipboardManager$OnPrimaryClipChangedListener what)

        synchronized (mPrimaryClipChangedListeners) {
            if (mPrimaryClipChangedListeners.size() == 0) {
                try {
                    getService().addPrimaryClipChangedListener(
                            mPrimaryClipChangedServiceListener, mContext.getOpPackageName());
                } catch (RemoteException e) {
                }
            }
            mPrimaryClipChangedListeners.add(what);
        }
    
public ClipDatagetPrimaryClip()
Returns the current primary clip on the clipboard.

        try {
            return getService().getPrimaryClip(mContext.getOpPackageName());
        } catch (RemoteException e) {
            return null;
        }
    
public ClipDescriptiongetPrimaryClipDescription()
Returns a description of the current primary clip on the clipboard but not a copy of its data.

        try {
            return getService().getPrimaryClipDescription(mContext.getOpPackageName());
        } catch (RemoteException e) {
            return null;
        }
    
private static IClipboardgetService()


                                         
       

                             
         
    

        
        synchronized (sStaticLock) {
            if (sService != null) {
                return sService;
            }
            IBinder b = ServiceManager.getService("clipboard");
            sService = IClipboard.Stub.asInterface(b);
            return sService;
        }
    
public java.lang.CharSequencegetText()

deprecated
Use {@link #getPrimaryClip()} instead. This retrieves the primary clip and tries to coerce it to a string.

        ClipData clip = getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return clip.getItemAt(0).coerceToText(mContext);
        }
        return null;
    
public booleanhasPrimaryClip()
Returns true if there is currently a primary clip on the clipboard.

        try {
            return getService().hasPrimaryClip(mContext.getOpPackageName());
        } catch (RemoteException e) {
            return false;
        }
    
public booleanhasText()

deprecated
Use {@link #hasPrimaryClip()} instead.

        try {
            return getService().hasClipboardText(mContext.getOpPackageName());
        } catch (RemoteException e) {
            return false;
        }
    
public voidremovePrimaryClipChangedListener(android.content.ClipboardManager$OnPrimaryClipChangedListener what)

        synchronized (mPrimaryClipChangedListeners) {
            mPrimaryClipChangedListeners.remove(what);
            if (mPrimaryClipChangedListeners.size() == 0) {
                try {
                    getService().removePrimaryClipChangedListener(
                            mPrimaryClipChangedServiceListener);
                } catch (RemoteException e) {
                }
            }
        }
    
voidreportPrimaryClipChanged()

        Object[] listeners;

        synchronized (mPrimaryClipChangedListeners) {
            final int N = mPrimaryClipChangedListeners.size();
            if (N <= 0) {
                return;
            }
            listeners = mPrimaryClipChangedListeners.toArray();
        }

        for (int i=0; i<listeners.length; i++) {
            ((OnPrimaryClipChangedListener)listeners[i]).onPrimaryClipChanged();
        }
    
public voidsetPrimaryClip(ClipData clip)
Sets the current primary clip on the clipboard. This is the clip that is involved in normal cut and paste operations.

param
clip The clipped data item to set.

        try {
            if (clip != null) {
                clip.prepareToLeaveProcess();
            }
            getService().setPrimaryClip(clip, mContext.getOpPackageName());
        } catch (RemoteException e) {
        }
    
public voidsetText(java.lang.CharSequence text)

deprecated
Use {@link #setPrimaryClip(ClipData)} instead. This creates a ClippedItem holding the given text and sets it as the primary clip. It has no label or icon.

        setPrimaryClip(ClipData.newPlainText(null, text));