Android check internet availability

When you create an application to use the internet resources or allow the user to download files from or upload files to the internet, it is a good idea to check internet availability and tell the user about it. Android provides a convenient class called ConnectivityManager that can be used to check the internet connections (Wi-Fi, GPRS, etc.). Its getActiveNetworkInfo method returns a NetworkInfo object that represents the current network connection. It will return null if the network connection is unavailable. You should call the isConnected() method of the NetworkInfo object before making any data transmission over the internet. Note that these classes and methods require the permission ACCESS_NETWORK_STATE. Thus, you have to add the following code to AndroidManifest.xml file.

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

Now to have a workable application on checking internet availability, you need to create a new Android project. Then open the activity_main.xml file to add an id to the TextView so that you can refer to it later in code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
     android:id="@+id/txtview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/hello_world" />

</RelativeLayout>


In the onCreate() method of the MainActivity class, the code is written to create a reference to the TextView and check internet availability.

package com.example.internetchecking;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.widget.TextView;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.txtview);
tv.setTextSize(20);  
try{
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      if( activeNetwork==null){
        tv.setText("Not connected to the internet");
        tv.setTextColor(Color.parseColor("#ff0000"));
    
      }
      else if(activeNetwork.isConnected()){
         tv.setText("Internet is available.");
         tv.setTextColor(Color.parseColor("#00ff00"));
      }
    
}catch(Exception e){}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-check-internet-availability

Posted by: Dara | post date: 08-23-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.