Fully client server application. All data is flying from the backend. How often and where at the start is it better to check for an internet connection? Can I do this when scrolling splashscreen?
public class ActivitySplash extends AppCompatActivity { private static final int SPLASH_TIME = 2000; // delay @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(ActivitySplash.this, MainActivity.class); startActivity(intent); finish(); } }, SPLASH_TIME); } } The check itself looks like this for now:
private void checkNetwork(){ new Thread(new Runnable() { @Override public void run() { if( isInternetAvailable() && isDataServerAvailable()){ finish(); }else{ throwError(); } } }); } private void throwError() { new AlertDialog.Builder(this) .setMessage(R.string.no_internet) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }) .show(); } public boolean isInternetAvailable() { try { InetAddress ipAddr = InetAddress.getByName("google.com"); return !ipAddr.equals(""); } catch (Exception e) { Log.d("INTERNET", "NO INTERNET CONNECTION"); return false; } } public boolean isDataServerAvailable() { try { InetAddress ipAddr = InetAddress.getByName(Constants.BASE_URL); return !ipAddr.equals(""); } catch (Exception e) { Log.d("INTERNET", "DATA SERVER IS DOWN"); return false; } }
Threadand, especially, in anActivity. If you are not familiar yet, read about MVP. When I need to do a connection check (or something else) - I create a separateActivityfor checks, which contains all the logic inPresenter. If the very first “working”Activitycan work without the data that theActivity "для проверок"checksActivity "для проверок", then I inherit thisActivityfrom theActivity "для проверок"and everything is checked at startup. Otherwise, I first launch theActivity "для проверок". Of course, this is all accompanied by beautifulProgressBar's :). - Rostislav Dugin