Android SDK低版本获取剪贴板最新项时间戳

freddon 发表于2019-11-17   阅读 1225 | 评论 0

ClipDescription中需要API level26+,才可以调用clipDescription.getTimestamp()。
既然知道有这个属性,那就利用反射就可以了 ``` public static Hashtable<Long, CharSequence> postFromClipBoard(Context context) { ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); if (clipboardManager != null && clipboardManager.hasPrimaryClip()) { ClipData primary = clipboardManager.getPrimaryClip(); Hashtable<Long, CharSequence> hashtable = new Hashtable<>(); int timeStamp = 0; if (primary != null) { ClipData.Item item = primary.getItemAt(0); ClipDescription clipDescription = primary.getDescription(); Class<?> f = clipDescription.getClass(); Field field = null; try { field = f.getDeclaredField("mTimeStamp"); field.setAccessible(true); timeStamp = (long) field.get(clipDescription); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } hashtable.put(timeStamp, item.getText()); return hashtable; } } return null; } ```

分类 :日常记录

评论(0)


登录,才能发评论哦~