android - Write An NDEF Message To A Tag Inside Runnable -
i have started developing in android , have started project using nfc tags ndef messages. have managed create simple application read text/plain ndef records , write data screen.
now working on writing ndef messages tags. seem have working wonder whether or not it's best write tag on thread other main ui thread?
having started learning, have been taught doing (potentially) long-running tasks in main thread no-no, , figured writing tag count such. putting code write tag inside runnable(), though, seems cause write fail. so, question is. wrong in assumption writing tag should done off of main thread? or wrong use runnable? , apologies if has been answered before, tried googling couldn't find on topic.
i've included example code:
the button handler:
public void writebutton(view view) { //some validation createmessagetowrite(); }
creating message:
public void createmessagetowrite() { edittext messagebox = (edittext)findviewbyid(r.id.towrite); string towrite = messagebox.gettext().tostring(); if(towrite.length() == 0) towrite = "this text writer"; ndefrecord textrecord = ndefrecord.createmime("text/plain", towrite.getbytes()); ndefrecord aar = ndefrecord.createapplicationrecord(getpackagename()); ndefmessage message = new ndefmessage(new ndefrecord[]{textrecord, aar}); messagetowrite=message; toast.maketext(getapplicationcontext(), "message made", toast.length_short).show(); write(); }
and, finally, code write tag:
public void write() { toast.maketext(getapplicationcontext(),"starting write. touch tag",toast.length_short).show(); sleep(3000); // runnable r = new runnable() //{ // @override // public void run() // { toast.maketext(getapplicationcontext(),"starting runnable. touch tag",toast.length_short).show(); sleep(3000); if(messagetowrite == null) { toast.maketext(getapplicationcontext(),"message null",toast.length_short).show(); return; } toast.maketext(getapplicationcontext(), "trying", toast.length_short).show(); sleep(3000); try { if(thetag != null) { thetag.connect();//the tag global var set in oncreate() or onnewintent() thetag.writendefmessage(messagetowrite); thetag.close(); } else { toast.maketext(getapplicationcontext(), "tag null", toast.length_short).show(); } } catch (ioexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "ioexception", toast.length_long).show(); } catch (formatexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "format", toast.length_long).show(); } toast.maketext(getapplicationcontext(),"written",toast.length_short).show(); } // }; // thread t = new thread(r); // t.start(); // }
the code posted works, uncommenting runnable() stuff causes app crash. unfortunately, system i'm working on isn't set allow usb debugging device , don't have permissions install drivers. so, until that's set up, have settle "the app has stopped working" message...
many , help
edit: grammar
first of all, without knowing exception get, assume crash/exception not caused doing write operation in separate thread showing toast
non-ui thread.
toast
s must shown ui thread. can overcome issue wrapping toast.maketext(...).show()
call in following:
runonuithread(new runnable() { public void run() { toast.maketext(youractivityclass.this, yourmessage, toast.length_short).show(); } });
second, practice access nfc tags (or in general io operations) in separate thread.
third, user interaction nfc tag typically short action , user typically tap tag during actual read, write, etc. operations. saving tag
handle , calling write operation later upon additional user input (pressing button) bad idea (some rare exception may exist though).
a better approach store ndef message written, set application "write mode" state , prompt user tap tag. then, upon tag detection, start write thread , let write saved message.
Comments
Post a Comment