has Text on Clipboard - Android android.content

Android examples for android.content:Clipboard

Description

has Text on Clipboard

Demo Code

import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;

public class Main{

    public static boolean hasText(Context context) {
        ClipboardManager cm = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ClipDescription description = cm.getPrimaryClipDescription();
            ClipData clipData = cm.getPrimaryClip();
            return clipData != null
                    && description != null
                    && (description
                            .hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
        } else {//from   w  w w  .ja  va  2 s  .co  m
            //noinspection deprecation
            return cm.hasText();
        }
    }

}

Related Tutorials