java - Service or background thread -


i have created bluetooth tic tac toe application in android. not familiar android, learned reading source codes on net. of how works this:

  • activity 1 > displays new game, about, exit. nothing here
  • activity 2 > listview buttons make discoverable , find devices. user touches on discovered device , connection established. button available exchange player names, , 1 more button go next activity
  • activity 3 > decide player goes first. 3 buttons corresponding rock, paper, scissor, button exchange info, , button go next activity
  • activity 4 > actual game. logic(it's not ordinary tic tac toe), on user touch makes calculations, sends across integer , waits data in return.

to implement communication have 3 threads: serverthread listen connections, connectthread connect device , commsthread send , receive data. after connection established commsthread needed. lose clarity. have object of type commsthread in game class, call commsthreadobject.start() in activity 2 once connection established , keep thread running till application exits. on each activity, pass handle belonging activity commsthread.

now code work me, doesn't feel neat(in fact feels dirty code). other updating ui handler requires static objects, ended converting lot of things static. understand keeping thread running can result in memory leaks. learning services, wanna know if right way this?

also in earlier experiments had single activity , switched layouts on button click. approach?

this code handler in game class.

static handler receivecell = new handler() {     @override     public void handlemessage(message msg) {         int numofbytesreceived = msg.arg1;         byte[] buffer = (byte[]) msg.obj;         string strreceived = new string(buffer);         strreceived = strreceived.substring(0, numofbytesreceived);             gameinstance.changesymbol();             int x = integer.parseint(strreceived);             int cur = gameinstance.curcell;             boardgroup[cur].put(x % 3, x / 3, gameinstance.current);             gameinstance.state = boardgroup[cur].checklines();             gameinstance.checkstate(cur % 3, cur / 3);             gameinstance.changesymbol();             gameinstance.curcell = x;             if (boardgroup[x].emptyblock() == 0) {                 numpad.enableinput();             } else {                 boardgroup[x].enableinput();             }             currentturn.settext(playername);         }     }; 

as can see there's quite bit in handler. had make variables referenced here static. instead there anyway can move out of handler. maybe way know when message has received? local strreceived.wait() , strreceived.notify()?

well, if screens logically different (like splash/setting/main/about) makes sense make them different activities - app becomes more complex still don't monster class way much.

as thread vs service - if manage thread (disconnect socket , kill thread in onpause() , bring in onresume(), etc...) there's no problem approach. however, if want receive data in background or other reason have more complex lifecycle services thing.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -