I have this code that on a click of a button a service will be started from a new thread and the service will start my TCP Client.
The TCP client, once connected will send a count down event to inform the fragment to continue working.
the code looks like this
connectButton = (Button) view.findViewById(R.id.connectBT);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
// ***** IntentService Way ****** //
Intent intent = new Intent(getActivity(), NetworkService.class);
getActivity().startService(intent);
// Once client is connected, give the server some info about client
mCountDown.await(3000, TimeUnit.MILLISECONDS);
String json = jsonMaker.toJson(new DeviceInfo());
DispatchToServer(json);
} catch (Exception ie) {
ie.getMessage();
}
}
}).run();
but when I click connect, my UI still freezes for some reason, even though I'm waiting on a different thread then the UI thread. Same code worked fine when I used an AsyncTask instead of the service.