I am developing an Android application (I'm a newbie) and I am trying to open a socket and check the connection status when launching the app.
To do so, I have a MainActivity that starts and binds to a service. On the onBind() method of this service, I call a function openSocket() that tries to open a socket with given IP address and port. To make this connection, I use an AsyncTask where I try and catch exceptions on opening the socket in doInBackground() and I update a boolean according to the connection status in onPostExecute().
In MainActivity, I want to retrieve this boolean value (Service.getConnectionStatus()).
The problem is that Service.getConnectionStatus() gets called in MainActivity before onPostExecute(), resulting in the boolean value not being correctly updated.
I have been doing research for a few days now, trying many different things and now I'm quite confused... Would anyone have an idea how to wait for onPostExecute() to finish before calling getConnectionStatus in MainActivity ?
I've tried to set a Listener for AsyncTask, but the result is the same. I've also tried to call openSocket() from different methods but without success...
Thank you for your help.
MyService.java
public class MyService extends Service {
private final IBinder mIBinder = new LocalBinder();
private Socket mSocket;
private String mDstAddress = "192.168.0.222";
private int mDstPort = 23;
private Boolean mConnectionStatus = false;
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
openSocket(mDstAddress, mDstPort);
return mIBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
public int getConnectionStatus() {
if (mConnectionStatus) {
return 1;
} else {
return 0;
}
}
public void closeSocket() {
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException ex) {
//handle exception
}
}
mSocket = null;
}
public void openSocket(String dstAddress, int portAddress) {
mDstAddress = dstAddress;
mDstPort = portAddress;
ConnectSocket connect =
new ConnectSocket(mDstAddress, mDstPort);
connect.execute();
}
public class ConnectSocket extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
ConnectSocket(String addr, int port) {
dstPort = port;
dstAddress = addr;
}
@Override
protected Void doInBackground(Void... voids) {
try {
mSocket = new Socket(mDstAddress, mDstPort);
mConnectionStatus = true;
} catch (UnknownHostException e) {
mConnectionStatus = false;
response = "UnknownHostException: " + e.toString();
e.printStackTrace();
closeSocket();
} catch (IOException e) {
mConnectionStatus = false;
response = "IOException: " + e.toString();
e.printStackTrace();
closeSocket();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (response.contains("Exception")) {
mConnectionStatus = false;
} else {
mConnectionStatus = true;
}
this.cancel(true);
super.onPostExecute(aVoid);
}
}
MainActivity.java (onServiceConnected only) :
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected");
MyService.LocalBinder binder = (MyService.LocalBinder) service;
mService = binder.getService();
mIsBound = true;
int statusResult = mService.getConnectionStatus();
Log.i(TAG, "statusResult: " + Integer.toString(statusResult));
mDisplayNumber.setText(Integer.toString(statusResult));
if (statusResult == 0) {
createDialog();
}
}