Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:41:51+00:00 2026-06-13T17:41:51+00:00

I found something interesting in the android OS, that I want to use in

  • 0

I found something interesting in the android OS, that I want to use in my own apps but how?

Like in the ConnectivityManager http://developer.android.com/reference/android/net/ConnectivityManager.html, you see status codes like

public static int TYPE_WIFI = 1;

When you call the connectivity manager with this code:

ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Connectivity_Service);
NetworkInfo iswifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (iswifi.isConnected()){ msg = "You got WIFI!"}

but if I’m having a class that needs to say if I’m having a internet connection, how can i let it return a status code like that?

Already thanks for your answers!:)

Wout

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:41:53+00:00Added an answer on June 13, 2026 at 5:41 pm

    Here the simple version:

    /*@return boolean return true if the application can access the internet*/
    public static boolean haveInternet(Context context){
        NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info==null || !info.isConnected()) {
            return false;
        }
        if (info.isRoaming()) {
            // here is the roaming option you can change it if you want to disable internet while roaming, just return false
            return true;
        }
        return true;
    }
    

    Here you have it with preferences for wifi, you can specify if you want some actions to be limited by a preference set to only use wifi.
    For example you can check for small downloads using withRoamingPref = false; returning true for any internet connection.
    Then check for large downloads using withRoamingPref = true, in this case, it will check if the user has enabled “only wifi” to download them and returns true if using wifi or true if the user allows the mobile connection to download them, returning false if there is no internet connection or if you are on mobile connection and the user has disabled it to download heavy files.

    public static boolean haveInternet(Context context, boolean withRoamingPref){
        NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info==null || !info.isConnected()) {
            return false;
        }
        if (info.isRoaming()) {
            // here is the roaming option you can change it if you want to disable internet while roaming, just return false
            if(withRoamingPref) {
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
                boolean onlyWifi = settings.getBoolean("onlyWifi", false);
    
                if (onlyWifi) {
                    return false;
    
                }else{
                    return true;
                }
    
            }else{
                return true;
            }
        }
        return true;
    }
    

    Modified case to return all three states and decide how to act on another piece of the application:

    /*@return int return 0 if the application can't access the internet
        return 1 if on mobile data or 2 if using wifi connection*/
    public static int haveInternet(Context context){
        NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info==null || !info.isConnected()) {
            return 0;
        }
        if (info.isRoaming()) {
            return 1;
        }else{
            return 2;
        }
    }
    

    PS: Instead of the numbers you should assign each state with a static int and then return that instead of the raw int. For example, declare at the start of the class:

    public static int INTERNET_DISABLED = 0;

    then instead of “return 0;” you’ll use “return INTERNET_DISABLED;”

    this way you don’t have to remember what means each number, you’ll check in other segment of the app against the names as in:

    if(myClass.haveInternet == myClass.INTERNET_DISABLED){
         //TODO show no internet message
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this interesting interface(starts at 33 seconds http://vimeo.com/22946428 ), and would like to
I'm learning asp.net mvc and found something interesting: It seems that I can't explicitly
I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person
I am writing validation for my textfield, I found something interesting that whether I
I've searched the net but didn't found anything interesting. Maybe I'm doing something wrong.
I found an interesting issue that confused me. I noticed it when I use
I don't like Jackson. I want to use ajax but with Google Gson. So
I was going over some of the Android resources and found something interesting. It
I was looking through a code tutorial just now, and found something interesting --
I found something like this in a SqlServer DB stored proc: SELECT stuff FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.