1. Are you ready for the Galaxy S20? Here is everything we know so far!

Check network connection

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

  1. iwanabeguru

    iwanabeguru Lurker
    Thread Starter

    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
     


  2. jonbonazza

    jonbonazza Android Expert

    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.  
     
Loading...

Share This Page

Loading...