I have a button that performs 2 actions. One is to check for internet/server connection, and the other is to get data from server if there is internet/server connection.
The problem I faced now is that my button will only perform the action to check internet/server connection but will not follow up the result of that connection to the next action.
However, when I press the button again, it will perform the second action.
Is there something wrong with my codes? I need some advise on this.
Checking Internet/Server connection
public Boolean isOnline() {
try {
p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
if (reachable) {
new uploadDB(new uploadDB.returnResult() {
@Override
public void onFinish(String result) {
if (result.equalsIgnoreCase("success")) {
isInternetConnected = true;
} else {
isInternetConnected = false;
showdialog("No Internet Connection", "Connection to server failed.");
}
}
}).execute("ping");
} else {
isInternetConnected = false;
showdialog("No Internet", "Internet connection is needed to perform certain actions.");
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Button Click
@Override
public void onHistoryClick(final int position) {
isOnline();
if(isInternetConnected == true)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
pd.setMessage("Retrieving History Information...");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
});
new uploadDB(new uploadDB.returnResult() {
@Override
public void onFinish(String result) {
if(result != null)
{
Intent intent = new Intent(MainActivity.this, HistoryYearActivity.class);
intent.putExtra("barrelUUID", barrelList.get(position).getBeaconUUID());
intent.putExtra("barrelName", barrelList.get(position).getBarrelName());
intent.putExtra("result", result);
startActivity(intent);
pd.dismiss();
}
}
}).execute("historypageall",barrelList.get(position).getBeaconUUID());
}
}