2

I am using the following if else statement:

if (isInternetPresent) {
    try {
        startActivity(i2);
    } catch (Exception e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
        builder.setTitle("Title");
        builder.setMessage("Message");
        AlertDialog alert = builder.create();
        alert.show();
    }
} else {
    Toast.makeText(getApplicationContext(), "Please check your Internet Connection.", Toast.LENGTH_LONG).show();
}

The problem is when there is no internet connection, it shows the toast, but when I connect to the network (keeping my application on), it shows the same toast. Any help is appreciated.

    check = new CheckInterNetConnection(getApplicationContext());
    isInternetPresent = check.isConnectingToInternet();

this two lines are in my onCreate

Developer110
  • 182
  • 7

1 Answers1

2

You can try this piece of code

    public class FirstActivity extends Activity
    {
        ImageView iv;
        boolean isInternetPresent;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourxml); // containing ImageView

            iv=(ImageView)findViewById(R.id.imageView1);
            iv.setOnClickListener(new View.OnClickListener() 
            {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent i2=new Intent(FirstActivity.this,Second.class);

                    isInternetPresent=isNetworkAvailable();

                    if (isInternetPresent) {
                        try {
                            startActivity(i2);
                        } catch (Exception e) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(FirstActivity.this);
                            builder.setTitle("Title");
                            builder.setMessage("Message");
                            AlertDialog alert = builder.create();
                            alert.show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "Please check your Internet Connection.", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        private boolean isNetworkAvailable() 
        {
            ConnectivityManager connectivityManager 
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        }
    }

Just copy and past this code and replace xml and id of image view with yours.

Hope this helps you somehow.

A.R.
  • 2,511
  • 1
  • 16
  • 22
  • If you want to perform actions like checking internet and then start activity. The above code should work as per your requirement. And sorry if i misunderstood your question.Try let me know. – A.R. Jan 25 '15 at 17:00
  • Further you can modify my code as per your requirement if its works for you. – A.R. Jan 25 '15 at 17:01
  • I should thank you for your efforts and i think that you have understood my question, but still it is not working. I mean it check the internet only once before starting the intent, although i connect to internet (without restarting the app). – Developer110 Jan 25 '15 at 17:15
  • i now want to check the internet connectivity every second........... how can i do that? (i think that can work) – Developer110 Jan 25 '15 at 17:16
  • 1
    In order to achieve that you have to implement the Concept of "Timer" which is going to check your internet connection on every second. – A.R. Jan 25 '15 at 17:24
  • This solution will test whether there is a network connection but does not check whether the Internet is reachable. For a better solution, check out [this thread](http://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection/6493572#6493572). – Ted Hopp Jan 25 '15 at 20:09