Search code examples
androidprogressdialogandroid-wifi

Create a dialogbox inside a thread while checking for an internet connection


I am trying to find out if my app is connected to the internet or not. I have a timeout set to 3 seconds. Sometimes the Internet Check will come back as "Not Connected" (even if I do have an internet connection) and sometimes it doesn't. Why does it take longer sometimes to check than others? Would I be able to have a dialogbox or something to popup while this is being checked?

public void isNetworkAvailable(final Handler handler)
{
    new Thread()
    {
        private boolean responded = false;

        @Override
        public void run()
        {
            new Thread()
            {
                @Override
                public void run()
                {
                    HttpGet requestForTest = new HttpGet("http://m.google.com");
                    try
                    {
                        new DefaultHttpClient().execute(requestForTest);
                        responded = true;
                    }
                    catch (Exception e)
                    {
                    }
                }
            }.start();

            try
            {
                int waited = 0;
                while (!responded && (waited < 3000))
                {
                    sleep(100);
                    if (!responded)
                    {
                        waited += 1000;
                    }
                }
            }
            catch (InterruptedException e)
            {
            } // do nothing
            finally
            {
                if (!responded)
                {
                    handler.sendEmptyMessage(0);
                }
                else
                {
                    handler.sendEmptyMessage(1);
                }
            }
        }
    }.start();
}

Handler h = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    { 
        if (msg.what != 1)
        { // code if not connected
            Log.i("Internet check", "Not connected");
        }
        else
        { // code if connected
            Log.i("Internet check", "Connected");
        }
    }
};

Solution

  • I was able to complete this by putting it inside an AsyncTask.

    class online extends AsyncTask<String, String, String> 
    {
        boolean responded = false;
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            pDialog2 = new ProgressDialog(Main.this);
            pDialog2.setMessage("Checking internet, please wait...");
            pDialog2.setIndeterminate(false);
            pDialog2.setCancelable(false);
            pDialog2.show();
        }
    
        protected String doInBackground(String... args) 
        {
    
            HttpGet requestForTest = new HttpGet("http://m.google.com");
            try
            {
                new DefaultHttpClient().execute(requestForTest); // can
                responded = true;
            }
            catch (Exception e)
            {
            }
    
            try
            {
                int waited = 0;
                while (!responded && (waited < 5000))
                {
                    mHandler.postDelayed(new Runnable() 
                    {
                        public void run() 
                        {
                        }
                    }, 100);
                    waited += 100;
                }
            }
            finally
            {
                if (!responded)
                {
                    h.sendEmptyMessage(0);
                }
                else
                {
                    h.sendEmptyMessage(1);
                }
            }
            return null;
        }
    
        protected void onPostExecute(String file_url) 
        {
            pDialog2.dismiss();
        }
    }