Wednesday, 4 April 2012

Checking for Internet availability in Android.


public Boolean isNetAvailable(Context con)  {

        try{
            ConnectivityManager connectivityManager = (ConnectivityManager)      
                                                                      con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mobileInfo =
                                 connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
                return true;
            }
        }
        catch(Exception e){
           e.printStackTrace();
        }
        return false;
    }
   
    and also add these permissions in manifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

6 comments:

  1. Hi Rajareaddy ,

    You have posted answer on my stackoverflow profile (http://stackoverflow.com/questions/10024535/how-to-post-json-string-from-android#comment12820534_10024535) , and i have tried it but still not able to get json responce from server. Please help me
    post your email id, i will send you full code and then let me know where i have made mistake.
    Plz reply ASAP.

    ReplyDelete
    Replies
    1. you can check it now, i made it clear.

      Delete
  2. plz check your mail, i am really thankfull to you

    ReplyDelete
  3. Hey , I am working on an android xmpp client , I almost finish facebook and gmail , i saw your post in stackoverflow about other service providers such as yahoo and windows live . what did u find ?

    ReplyDelete