Unity Calls system Clipboard function

Source: Internet
Author: User
Tags prepare

 Packagecom.game.utils;Importandroid.app.Activity;ImportAndroid.content.ClipData;Importandroid.content.ClipDescription;ImportAndroid.content.ClipboardManager;ImportAndroid.content.Context;ImportAndroid.os.Handler;ImportAndroid.os.Looper;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.net.NetworkInterface;Importjava.util.Collections;Importjava.util.List; Public classGameutils { Public byte[] readbytes (activity activity, String path) {InputStream is=NULL; Try{ is=activity.getassets (). open (path); }Catch(IOException e) {e.printstacktrace (); } bytearrayoutputstream OutputStream=NewBytearrayoutputstream (); byteBuf[] =New byte[1024]; intLen; Try {             while(len = Is.read (BUF))! =-1) {outputstream.write (buf,0, Len);            } outputstream.close ();        Is.close (); } Catch(IOException e) {e.printstacktrace (); }        returnOutputstream.tobytearray (); }     Public StaticClipboardmanager Clipboard =NULL; //add text to the Clipboard     Public voidCopytexttoclipboard (FinalContext activity,FinalString str)throwsException {if(Looper.mylooper () = =NULL) {looper.prepare (); } Handler Handler=NewHandler (); 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 (); }    //get text from the Clipboard     PublicString Gettextfromclipboard (FinalContext activity)throwsException {if(Looper.mylooper () = =NULL) {looper.prepare ();        Looper.mylooper (); } StringBuilder res1=NewStringBuilder (); //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 ();        returnres1.tostring (); }    /*** Get MAC Address *@return     */     PublicString Getandroidmac (FinalContext 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=NewStringBuilder ();  for(byteb:macbytes) {Res1.append (String.Format ("%02X:", b)); }                if(Res1.length () > 0) {Res1.deletecharat (Res1.length ()-1); }                returnres1.tostring (); }        } Catch(Exception ex) {ex.printstacktrace (); }        return NULL; }}

Unity Calls system Clipboard function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.