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

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Further you can modify my code as per your requirement if its works for you.
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).
i now want to check the internet connectivity every second........... how can i do that? (i think that can work)
In order to achieve that you have to implement the Concept of "Timer" which is going to check your internet connection on every second.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.