0

In my android application I am checking whether internet connection is present like this in my main activity. Once internet ( data ) connection failure, then it will show error correctly.

But when I quit the application and turn on internet connection and run app then also it showing the same dialogue ( "no internet connection " ), and it will cleared when I reinstall the app, or restart the device .

My Code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        setFirsLaunchFlag();

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }

... 
}

I am very new to android.. please help me

Update

boolean connected = false;

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

        ConnectivityManager connectivityManager =

                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

            //we are connected to a network
            connected = true;
        }
        else
            connected = false;

        if(connected==false){
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }
... 

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ramesh
  • 4,008
  • 13
  • 72
  • 117
  • Check that your code looks like this and make sure that you have added the correct permission to your manifest as in the answer to this question: http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available – Erik Nedwidek May 10 '13 at 17:05

2 Answers2

1

use this code.

boolean connected = false;

ConnectivityManager connectivityManager =

(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

    //we are connected to a network
    connected = true;
}
else
    connected = false;

use permission

"android.permission.ACCESS_NETWORK_STATE"

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • Hi thanks for the reply ... but still getting the same condition ... see my updated code plz – ramesh May 10 '13 at 17:51
  • did you enable your wifi in device? please enable and disable this and try to get resutl. – Sunil Kumar May 10 '13 at 17:57
  • yes ... I tried .. first time its showing "no connection" if there is no connection, then if I turn on the connection then launch app, then its also showing no connection. So I give a "Toast" above this, unfortunately that toast also not showing on sec time ... any idea ?\ – ramesh May 10 '13 at 18:03
1
public static boolean hasInternet(Activity a) 
{
        try {
            boolean hasConnectedWifi = false;
            boolean hasConnectedMobile = false;
            ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();
            for (NetworkInfo ni : netInfo) {
                if (ni.getTypeName().equalsIgnoreCase("wifi"))
                    if (ni.isConnected())
                        hasConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("mobile"))
                    if (ni.isConnected())
                        hasConnectedMobile = true;
            }
            return hasConnectedWifi || hasConnectedMobile;
        }
        catch (Exception ex) {
        }
        return false;

}

Use this method to check if you have internet connection... this method will return a boolean true if it has internet and false if there is no internet

Srikanth Pai
  • 926
  • 3
  • 17
  • 30