Created
July 14, 2021 07:41
-
-
Save puke3615/b4149db49e279bffafa29fd694bd81ea to your computer and use it in GitHub Desktop.
ClipboardUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ClipData; | |
import android.content.ClipboardManager; | |
import android.content.Context; | |
import android.net.Uri; | |
/** | |
* 剪切板工具类, 提供复制、粘贴功能 | |
* | |
* @author puke | |
* @version 2018/5/24 | |
*/ | |
public class ClipboardUtil { | |
/** | |
* 复制Uri | |
*/ | |
public static void copyUri(Uri uri) { | |
if (uri == null) { | |
return; | |
} | |
ClipboardManager clipboardManager = (ClipboardManager) App.getApplication().getSystemService(Context.CLIPBOARD_SERVICE); | |
if (clipboardManager != null) { | |
clipboardManager.setPrimaryClip(ClipData.newRawUri(null, uri)); | |
} | |
} | |
/** | |
* 复制文本 | |
*/ | |
public static void copyText(String text) { | |
ClipboardManager clipboardManager = (ClipboardManager) App.getApplication().getSystemService(Context.CLIPBOARD_SERVICE); | |
if (clipboardManager != null) { | |
if (text == null) { | |
text = ""; | |
} | |
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); | |
} | |
} | |
/** | |
* @return 获取剪切板文本 | |
*/ | |
public static String getClipText() { | |
ClipboardManager clipboardManager = (ClipboardManager) App.getApplication().getSystemService(Context.CLIPBOARD_SERVICE); | |
if (clipboardManager != null) { | |
ClipData clip = clipboardManager.getPrimaryClip(); | |
if (clip != null && clip.getItemCount() > 0) { | |
return clip.getItemAt(0).coerceToText(App.getApplication()).toString(); | |
} | |
return null; | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment