1. Download our Official Android App: Forums for Android!

Apps Check network connection

Discussion in 'Android Development' started by iwanabeguru, Sep 24, 2011.

  1. iwanabeguru

    iwanabeguru Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Sep 24, 2011

    Sep 24, 2011
    3
    0
    5
    Hi all

    I'm new to here and i hope i can get some help. I not searched the forums for similar topics cause i do not have much time, so please forgive me if this question asked before.

    So, I want to check that the network connection is alive on the phone from program but when i try to run this code
    Code (Text):
    1.  
    2. private boolean isOnline() {
    3.         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    4.         NetworkInfo netInfo = cm.getActiveNetworkInfo();
    5.         if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    6.             return true;
    7.         }
    8.         return false;
    9.     }
    10.  
    I get the message "Sorry 'Appname' is stopped unexpectedly. Try again"
    Can someone help me with this?

    EDIT1

    I might have to search the solution elsewhere, i added permissions for network, run the application and got the same error but when i tried to start it from the menu it said application not installed.

    Thank you

    iwanabeguru
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    You need to add the following lines to your manifest:
    Code (Text):
    1.  
    2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    3. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    4.  
     

Share This Page

Loading...