0

Here are my simple class Splash Screen. Now, I need checking if have internet in my webview. Where I make this checking?

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);


    }

}

**OBS: I are using the permissions in manifest:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Kevin Jhon
  • 55
  • 2
  • 12

2 Answers2

1

You can use this method:

public static boolean isOnline() {
    ConnectivityManager connMgr = (ConnectivityManager) yourcontext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isAvailable());
}
Victor Gomes
  • 463
  • 6
  • 15
0

I use a different method for check if the internet connection is enable

 public boolean isOnline() {

    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException | InterruptedException e) { e.printStackTrace(); }

    return false;
}

This method check if you can connect to the google server, hope this help

Vodet
  • 1,491
  • 1
  • 18
  • 36