2

Well, after my SplashScreen screen. My app will check for an Internet connection. if there is internet available, it will call the WebView. Otherwise, it will call one Activity of error.

But how do I pair to check the internet DURING SplashScreen?

ACTIVITY SPLASH SCREEN:

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

    }

}

MY CLASS CheckINTERNET:

public class CheckInternet extends Activity{

    boolean status = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internet);

        Button btnStatus = (Button) findViewById(R.id.check_btn);
        btnStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status = checkInternetConnection();

                if (status) {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            final Intent mainIntent = new Intent(CheckInternet.this, MainActivity.class);
                            CheckInternet.this.startActivity(mainIntent);
                            CheckInternet.this.finish();
                        }
                    }, 5000);

                }
                else {
                    Toast.makeText(getApplicationContext(), "You don't have Internet connection. Try Again", Toast.LENGTH_LONG).show();

                }
            }
        });
    }


    public boolean checkInternetConnection() {

        ConnectivityManager connectivity = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity != null) {
            NetworkInfo[] inf = connectivity.getAllNetworkInfo();
            if (inf != null) {
                for (int i = 0; i < inf.length; i++) {
                        return true;
                }
            }
        }
        return false;
    }

}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Kevin Jhon
  • 55
  • 2
  • 12
  • You got wrong implementation of splash screen. check this http://stackoverflow.com/a/36916491/3436179 and in splash activity call main activity after chek internet. – Alexander Jun 01 '16 at 20:51

5 Answers5

3

try this code... maybe help you

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.splash_screen);

    if(isWorkingInternetPersent()){
        splash();
    }
    else{
        showAlertDialog(SplashScreen.this, "Internet Connection",
                "You don't have internet connection", false);
    }
}
public void splash() {
    Thread timerTread = new Thread() {
        public void run() {
            try {
                sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Intent intent = new Intent(getApplicationContext(), New_Main_Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        }
    };
    timerTread.start();
}
public boolean isWorkingInternetPersent() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }

    }
    return false;
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    // alertDialog.setIcon((status) ? R.mipmap.ic_launcher : R.mipmap.ic_launcher);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            finish();
            System.exit(0);
        }
    });

    // Showing Alert Message
    alertDialog.show();
}
Amit Basliyal
  • 840
  • 1
  • 10
  • 28
0

You should not really have an Intent just for checking connectivity. My suggestion is to create a "utility" class - call is MyConnectivityChecker and in there you add a static method (isConnected) with the following code:

    public class MyConnectivityChecker {

        public static boolean  isConnected(Context context){
         boolean connected = false;
         ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
         NetworkInfo mobile = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        
         connected = (wifi.isAvailable() && wifi.isConnectedOrConnecting() || (mobile.isAvailable() && mobile.isConnectedOrConnecting()));
         return connected;
    }
}

Then in your Splash Activity wherever you want to check connectivity, you call the isConnected method like this:

MyConnectivityChecker.isConnected(this)

For example you could do something like this:

if(MyConnectivityChecker.isConnected(this)){
//connectivity available
}
else{
 //no connectivity
}

I hope this helps. Give it a try and let me know.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

what you can do is check for internet connection inside of your timer of SplashScreen.

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

    public void run() {
        if(isOnline()){

            Intent intent = new Intent(Splash.this,WebViewActivity.class);
            startActivity(intent);
            finish();

        }else{

             Intent intent = new Intent(Splash.this,ErrorActivity.class);
             startActivity(intent);
             finish();

        }
    }
}, 2000);

And for internet checking you can use this method.

public boolean isOnline() {
    System.out.println("executeCommand");
    Runtime localRuntime = Runtime.getRuntime();
    try {
        int i = localRuntime.exec("/system/bin/ping -c 1 8.8.8.8")
                .waitFor();
        System.out.println(" mExitValue " + i);
        boolean bool = false;
        if (i == 0) {
            bool = true;
        }
        return bool;
    } catch (InterruptedException localInterruptedException) {
        localInterruptedException.printStackTrace();
        System.out.println(" Exception:" + localInterruptedException);
        return false;
    } catch (IOException localIOException) {
        localIOException.printStackTrace();
        System.out.println(" Exception:" + localIOException);
    }
    return false;
} 

NOTE: Add this method in your SplashScreen outside the onCreate() methode.

Happy Coding..

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

Create Following Utility Class as below :

public class Utility {

/**
     * This function check <u>Mobile Data</u> or <u>WiFi</u> is switched on or not..<br />
     * It will be return <b>true</b> when switched on and return <b>false</b> when switched off.<br />
     * <br />
     * Developed by <b><a href="http://about.me/SutharRohit">Suthar Rohit</a></b>
     *
     * @param context {@link Context} of activity
     * @return true if <u>Mobile Data</u> or <u>WiFi</u> is switched on.
     */
    public static boolean isOnline(Context context) {
        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo nInfo = cm.getActiveNetworkInfo();
            return nInfo != null && nInfo.isConnected();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

and use as below in splash screen :

if(Utility.isOnline()) {
     // INTERNET AVAILABLE

} else {
     // INTERNET NOT AVAILABLE
}
Rohit Suthar
  • 2,635
  • 1
  • 22
  • 27
0
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;

public class SplashACtivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                NetworkInfo activeNetwork = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))
                        .getActiveNetworkInfo();

                if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {

                    // Load Webview
                    startActivity(new Intent(SplashACtivity.this, WebViewActivity.class));
                } else {

                    // Show No internet
                    startActivity(new Intent(SplashACtivity.this, NoInternetACtivity.class));
                }
            }
        }, 5000);
    }
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154