I'm trying to run 2 methods but not at the same time. However, I've tried both wait
and Thread.sleep()
, but it doesn't delay method 2 which is an Intent
public void run() {
checkInternetConnection();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Intent intent = new Intent(this, Forside.class);
startActivity(intent);
finish();
}
private void checkInternetConnection() {
if (br == null) {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
NetworkInfo info = (NetworkInfo) extras
.getParcelable("networkInfo");
NetworkInfo.State state = info.getState();
Log.d("TEST Internet", info.toString() + " "
+ state.toString());
if(state == NetworkInfo.State.DISCONNECTED) {
AlertDialog alert = new AlertDialog.Builder(context)
.setMessage("Fejl, der er ingen internet forbindelse. Venligst forbind til et netværk")
.setNeutralButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
launchsettings();
}
}
).show();
}
}
};
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver((BroadcastReceiver) br, intentFilter);
}
}
private void launchsettings() {
startActivityForResult(new Intent(Settings.ACTION_SETTINGS), 0);
}
@Override
protected void onPause() {
this.unregisterReceiver(this.bur);
super.onPause();
}
Both methods start at the same time or the 2. method starts directly after the 1.
Create a function like this one
public static boolean isInternetConnected(Context context)
{
boolean result = false;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null)
{
result = info.isConnected();
Log.i("My Project", "Connected NetworkType: " + info.getTypeName());
}
return result;
}
then in you activity do this
if(isInternetConnected()){
//TODO
}else{
//TODO
}