1

I have a splash screen and it is working perfectly, but now I want to run a function to perform an internet check and decide which activity to send the User.

public class Splash extends Activity {

    private static int tempo_splash = 1000;    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash); 
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Para o layout preencher toda tela do cel (remover a barra de tit.)

        new Timer().schedule(new TimerTask() {    

            public void run() {
                finish();

                Intent intent = new Intent();
                intent.setClass(Splash.this, MainActivity.class); //Chamando a classe splash e a principal (main)
                startActivity(intent);
            }
        }, 2000);    

    }
}

And this is my class for checkInternet:

public class MyConnectivityChecker extends AppCompatActivity {    

    public void verificaInternet() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm.getActiveNetworkInfo()!= null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {

            Intent i = new Intent(this, MainActivity.class);

        } else {

            Intent i = new Intent(this, CheckInternet.class);
            startActivity(i);

        }
    }    
}
dinotom
  • 4,990
  • 16
  • 71
  • 139
Kevin Jhon
  • 55
  • 2
  • 12

1 Answers1

1

your code could look like this

private Class verificaInternet() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (cm.getActiveNetworkInfo()!= null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return MainActivity.class;
    } else {
       return CheckInternet.class;
    }
}

add above method in your splashActivity and your timer should look like this

new Timer().schedule(new TimerTask() {
       @Override public void run() {
            Intent intent = new Intent();
            intent.setClass(Splash.this, verificaInternet()); //Chamando a classe splash e a principal (main)
            startActivity(intent);
            finish();//this should be after starting intent
        }
    }, 2000);
Kosh
  • 6,140
  • 3
  • 36
  • 67