In my onCreate method in main activity i have some code which is checking if wifi connection is enabled. If it isn’t i’m trying to automatically enabled it (very important now ) and after that i want to make other stuff ( starting service ..and other things). Its very important that it must be enabled before proceeding with execution !
I already tried AsyncTask , but with no luck. (code is still executing down below, before task in AsyncTask is completed ).
How to achive that some code will execute AFTER certain task is completed?
EDIT:
my onCreate in main activity
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()) {
new Asyn(this, progress, wifiManager);
}
startService(new Intent(intentt));
Asyn Class
public class Asyn extends AsyncTask<Context, Integer, Long> {
private Context context;
private ProgressDialog pd;
private WifiManager wm;
public Asyn(Context context, ProgressDialog pd, WifiManager wm) {
this.context = context;
this.pd = pd;
this.wm = wm;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(context, "", "Enabling wifi");
}
@Override
protected Long doInBackground(Context... params) {
wm.setWifiEnabled(true);
return null;
}
@Override
protected void onPostExecute(Long result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
}
}
I dont want to execute “startService” until wifi is not fully enabled!
Async task is not the solution since starting WIFI is something that you can’t just assume will happen after you call
wm.setWifiEnabled(true);You’ll have to add a Broadcast receiver on WiFi state.
Please have a look at this answer:
Listening WIFI state
I believe the second answer, (not the one that was accepted) is what you seek