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 8121213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:24:58+00:00 2026-06-06T05:24:58+00:00

Hai i am new bie to Android. i need to check the network connection

  • 0

Hai i am new bie to Android. i need to check the network connection throughout the application and inform to user about the network status when the state changed.please tell me how to do with some sample code.thanks in advance

  • 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-06T05:25:00+00:00Added an answer on June 6, 2026 at 5:25 am

    You can use runOnUiThread and ConnectivityManager for checking Network Status after a fixed time and show message to user as:

    private boolean mNetworkRunning=false;
    private  ConnectivityManager connMgr; 
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView (R.layout.main);
     mClockRunning=true;
     NetworkStatusThread();
     }
       @Override
        protected void onDestroy() {
            super.onDestroy();
            // Stop Thread Here.
            mNetworkRunning=false;
        }  
     public void NetworkStatusThread(){
         Thread th=new Thread(){
        @Override
        public void run(){
         try
          {
           while(mNetworkRunning)
           {
             Thread.sleep(1000L);// set time here for refresh time in show update to user
            NetworkActivity.this.runOnUiThread(new Runnable() { 
             @Override
             public void run() {
             // TODO Auto-generated method stub
              if(mNetworkRunning)
               {
                 boolean statu=isMobileNetworkAvailable();
                 if(statu==true)
                Toast.makeText(YourCurrentActivity.this, "Network Available",Toast.LENGTH_LONG).show();
                 else
                 Toast.makeText(YourCurrentActivity.this, "Network Not Available",Toast.LENGTH_LONG).show();
               } 
              };
        }
        }catch (InterruptedException e) {
            // TODO: handle exception
        }
       }
     };
     th.start();
    }
    
    public  boolean isMobileNetworkAvailable(Context con){  
        if(null == connMgr){  
         connMgr = (ConnectivityManager)con.getSystemService(Context.CONNECTIVITY_SERVICE);  
        }  
        NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
        NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
        if(wifiInfo.isAvailable()){  
            return true;  
        }else if(mobileInfo.isAvailable()){  
            return true;  
        }else{  
            return false;  
        }  
      }
    

    and second option without using Thread then you can register an receiver for CONNECTIVITY_CHANGE Manifest.xml as:

    < !-- Needed to check when the network connection changes -->   
    < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
    < receiver android:name="you_package_name.ConnectionChangeReceiver"   
      android:label="NetworkConnection">   
    < intent-filter>   
          < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   
    < /intent-filter>   
    < /receiver>
    

    in code:

    public class ConnectionChangeReceiver extends BroadcastReceiver {  
    
        private static final String TAG =ConnectionChangeReceiver.class.getSimpleName();  
    
        @Override   
        public void onReceive(Context context, Intent intent) {   
        boolean success = false;   
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);    
        State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();   
        //WIFI
        if (State.CONNECTED == state) {  
        success = true;   
        }   
        //GPRS
        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
        if (State.CONNECTED != state) {   
        success = true;   
        }   
        if (!success) {   
        Toast.makeText(context, "SHOW STATUS HERE", Toast.LENGTH_LONG).show();   
        }    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hai am new in android Application,am trying to get the latitude and longtitude using
Hai all I am new in android programming..... For my application I am using
Hai I have an application which is designed in netbeans6.0.1 where i need to
Hai I have developed a standalone application in which when an user logs in,
Hai i developed a application in android,in my application i used a dynamically created
Hai guys, I started a new asp.net mvc application and i followed the steps
hai every one. i am new to android. in my project i had some
Hai I am new to the Android. I am using Android4 sdk. I can't
i am new programmer in android application i would like display multiple notifications one
Hai I am new to iphone and i am creating an application. I want

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.