码迷,mamicode.com
首页 > 编程语言 > 详细

unity调用系统剪切板功能

时间:2018-05-09 21:00:05      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:java   etl   context   dma   format   activity   nts   roi   tst   

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<NetworkInterface> 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;
    }
}

 

unity调用系统剪切板功能

标签:java   etl   context   dma   format   activity   nts   roi   tst   

原文地址:https://www.cnblogs.com/dabiaoge/p/9016147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!