android - NetworkOnMainThreadException even though there is a background thread inside service -
i developing udp chat app. network processing inside thread in service. still getting error message 3.1 , 4.0 oeprating system. versions 2.3 , below working fine. question: should create 2 apps, 1 version 2.3 , below , 1 version 3.0 , higher? error happens when write(byte[] out) method called according logcat.
if disable strictmode ics app working fine.
public class chatservice extends service { private binder binder; private comthread comthread; public ibinder onbind(intent intent) { return binder; } public void oncreate() { } public int onstartcommand(intent intent, int flags, int startid) { binder = new chatservicebinder(); start(); return super.onstartcommand(intent, flags, startid); } public synchronized void start() { comthread = new comthread(); comthread.start(); } public void ondestroy() { stop(); } public void write(byte[] out) { comthread.write(out); } public synchronized void stop() { if (comthread != null) { comthread.cancel(); comthread = null; } } private class comthread extends thread { private static final int bcast_port = 2562; datagramsocket msocket; inetaddress mybcastip, mylocalip; public comthread() { try { mybcastip = getbroadcastaddress(); if (d) log.d(tag, "my bcast ip : " + mybcastip); mylocalip = getlocaladdress(); if (d) log.d(tag, "my local ip : " + mylocalip); msocket = new datagramsocket(bcast_port); msocket.setbroadcast(true); } catch (ioexception e) { log.e(tag, "could not make socket", e); } } public void run() { try { byte[] buf = new byte[1024]; if (d) log.d(tag, "run(), com thread startet"); // listen on socket receive messages while (true) { datagrampacket packet = new datagrampacket(buf, buf.length); msocket.receive(packet); inetaddress remoteip = packet.getaddress(); if (remoteip.equals(mylocalip)) continue; string s = new string(packet.getdata(), 0, packet.getlength()); if (d) log.d(tag, "run(), " + s); message msg = new message(); msg.obj = s; msg.arg1 = messagehandler.msg_in; state.gethandler().sendmessage(msg); } } catch (ioexception e) { e.printstacktrace(); } } /** * write broadcast packet. */ public void write(byte[] buffer) { try { string data = new string(buffer); datagrampacket packet = new datagrampacket(data.getbytes(), data.length(), mybcastip, bcast_port); msocket.send(packet); } catch (exception e) { log.e(tag, "write(), exception during write", e); } } /** * calculate broadcast ip need send packet along. */ private inetaddress getbroadcastaddress() throws ioexception { wifimanager mwifi = (wifimanager) state .getsystemservice(context.wifi_service); wifiinfo info = mwifi.getconnectioninfo(); if (d) log.d(tag, "\nwifi status: " + info.tostring()); // dhcpinfo simple object retrieving results of dhcp // request dhcpinfo dhcp = mwifi.getdhcpinfo(); if (dhcp == null) { log.d(tag, "could not dhcp info"); return null; } int broadcast = (dhcp.ipaddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; (int k = 0; k < 4; k++) quads[k] = (byte) ((broadcast >> k * 8) & 0xff); // returns inetaddress corresponding array of bytes. return inetaddress.getbyaddress(quads); // high order byte // quads[0]. } private inetaddress getlocaladdress() throws ioexception { try { (enumeration<networkinterface> en = networkinterface .getnetworkinterfaces(); en.hasmoreelements();) { networkinterface intf = en.nextelement(); (enumeration<inetaddress> enumipaddr = intf .getinetaddresses(); enumipaddr.hasmoreelements();) { inetaddress inetaddress = enumipaddr.nextelement(); if (!inetaddress.isloopbackaddress()) { return inetaddress; } } } } catch (socketexception ex) { log.e(tag, ex.tostring()); } return null; } public void cancel() { try { msocket.close(); } catch (exception e) { log.e(tag, "close() of connect socket failed", e); } } } public class chatservicebinder extends binder { private chatservice service = chatservice.this; public chatservice getservice() { return service; } } }
}
thanks.
a little late, , not super great answer, on android 3+ runnable won't interpreted permitted unless it's inside service (not sub-class have it). know limiting check given freedom have create pretty want want, again udp multicasting isn't android developers mess with. hope helps.
Comments
Post a Comment