博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity调用系统剪切板功能
阅读量:6600 次
发布时间:2019-06-24

本文共 3705 字,大约阅读时间需要 12 分钟。

package com.game.utils;import android.app.Activity;import android.content.ClipData;import android.content.ClipDescription;import android.content.ClipboardManager;import android.content.Context;import android.os.Handler;import android.os.Looper;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.NetworkInterface;import java.util.Collections;import java.util.List;public class GameUtils {    public byte[] ReadBytes(Activity activity, String path)    {        InputStream is = null;        try {            is = activity.getAssets().open(path);        }catch (IOException e){            e.printStackTrace();        }        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();        byte buf[] = new byte [1024];        int len;        try {            while ((len = is.read(buf)) != -1) {                outputStream.write                        (buf, 0, len);            }                outputStream.close();            is.close();        } catch (IOException e) {            e.printStackTrace();        }        return outputStream.toByteArray();    }    public static ClipboardManager clipboard = null;    // 向剪贴板中添加文本    public void copyTextToClipboard(final Context activity, final String str) throws Exception {        if(Looper.myLooper() == null)        {            Looper.prepare();        }        Handler handler = new Handler();        clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);        if (clipboard != null && clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {            ClipData textCd = ClipData.newPlainText("data", str);            clipboard.setPrimaryClip(textCd);        }        handler.getLooper().quit();    }    // 从剪贴板中获取文本    public String getTextFromClipboard(final Context activity) throws Exception {        if(Looper.myLooper() == null)        {            Looper.prepare();            Looper.myLooper();        }        StringBuilder res1 = new StringBuilder();        //Handler handler = new Handler();        clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);        //if (clipboard != null && clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
if (clipboard != null && clipboard.hasPrimaryClip()) { ClipData cdText = clipboard.getPrimaryClip(); ClipData.Item item = cdText.getItemAt(0); res1.append(item.getText().toString()); } else { res1.append("no key"); } //handler.getLooper().quit(); return res1.toString(); } /** * 获取MAC地址 * @return */ public String getAndroidMac(final Context activity) { try { List
all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return null; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(String.format("%02X:", b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { ex.printStackTrace(); } return null; }}

 

转载于:https://www.cnblogs.com/dabiaoge/p/9016147.html

你可能感兴趣的文章
频率域滤波
查看>>
Java中的简单工厂模式(转)
查看>>
file upload使用iframe
查看>>
使用OpenFiler来模拟存储配置RAC中ASM共享盘及多路径(multipath)的测试
查看>>
javascript将目标另存为
查看>>
图片存储类型的种类、特点、区别
查看>>
SQLServer数据库中创建临时表
查看>>
GETTING UP AND RUNNING WITH NODE.JS, EXPRESS, JADE, AND MONGODB
查看>>
课堂作业
查看>>
第五章设置 IntelliJ IDEA 主题和字体的方法
查看>>
求二叉树第K层节点的个数
查看>>
关于cocos2d-x面试的问题
查看>>
MySQLs数据库建外键时自动跑到缩影处,真奇怪
查看>>
Web应用架构-Database
查看>>
关于DBCC
查看>>
数据库2
查看>>
浅谈HIbernate缓存
查看>>
static关键字
查看>>
询问Spring Bott和高并发框架两个问题
查看>>
hdu 4160 Dolls
查看>>