content: package com.liyi.sutils.utils;
  
  import android.content.ClipData;
  import android.content.ClipboardManager;
  import android.content.Context;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Build;
  import android.support.annotation.RequiresApi;
  
  /**
   * 剪贴板相关工具类
   */
  @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
  public class ClipboardUtil {
  
      private ClipboardUtil() {
          throw new UnsupportedOperationException("cannot be instantiated");
      }
  
      /**
       * 复制文本到剪贴板
       *
       * @param text 文本
       */
      public static void copyText(final CharSequence text) {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          clipboard.setPrimaryClip(ClipData.newPlainText("text", text));
      }
  
      /**
       * 获取剪贴板的文本
       *
       * @return 剪贴板的文本
       */
      public static CharSequence getText() {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          ClipData clip = clipboard.getPrimaryClip();
          if (clip != null && clip.getItemCount() > 0) {
              return clip.getItemAt(0).coerceToText(SUtils.getApp());
          }
          return null;
      }
  
      /**
       * 复制uri到剪贴板
       *
       * @param uri uri
       */
      public static void copyUri(Uri uri) {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          clipboard.setPrimaryClip(ClipData.newUri(SUtils.getApp().getContentResolver(), "uri", uri));
      }
  
      /**
       * 获取剪贴板的 uri
       *
       * @return 剪贴板的 uri
       */
      public static Uri getUri() {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          ClipData clip = clipboard.getPrimaryClip();
          if (clip != null && clip.getItemCount() > 0) {
              return clip.getItemAt(0).getUri();
          }
          return null;
      }
  
      /**
       * 复制意图到剪贴板
       *
       * @param intent 意图
       */
      public static void copyIntent(final Intent intent) {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          clipboard.setPrimaryClip(ClipData.newIntent("intent", intent));
      }
  
      /**
       * 获取剪贴板的意图
       *
       * @return 剪贴板的意图
       */
      public static Intent getIntent() {
          ClipboardManager clipboard = (ClipboardManager) SUtils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
          ClipData clip = clipboard.getPrimaryClip();
          if (clip != null && clip.getItemCount() > 0) {
              return clip.getItemAt(0).getIntent();
          }
          return null;
      }
  }
  