Skip to main content

Check internet connectivity in Android

Write a class which check internet connectivity in android mobile

ConncetionDetector.class

public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}

public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
 if (connectivity != null) 
 {
 NetworkInfo[] info = connectivity.getAllNetworkInfo();
 if (info != null) 
 for (int i = 0; i < info.length; i++) 
 if (info[i].getState() == NetworkInfo.State.CONNECTED)
 {
 return true;
 }

 }
 return false;
}
}


Activity:

public class MainActivity extends ActionBarActivity {

ConnectionDetector cd;
public static boolean isInternetPresent = false;
TextView info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd=new ConnectionDetector(getBaseContext());
isInternetPresent = cd.isConnectingToInternet();
Button click=(Button)findViewById(R.id.button1);
click.setOnClickListener(new  OnClickListener() {
@Override
public void onClick(View v) {
info=(TextView)findViewById(R.id.textView1);
if(isInternetPresent){
info.setText("Connected to internet");
}else{
info.setText("No internet connection");
}
}
});
}
}

Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sachinsandbhor.internetconnectiondemo.MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp"
        android:text="Check Internet Connection" />

</RelativeLayout>

Add these two lines in AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

ScreenShots:




 Here is the sample Code

Comments

Popular posts from this blog

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...

How prevent the Android OS to take a screenshot of the app when it's being pushed into the background for security reasons

How to create custom title in Android Application

Custom title is nothing but a layout you bind to your application's title bar. MainActivity.java package com.example.customtitlebar; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Window; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customheader); init(); } private void init() { // TODO Auto-generated method stub Button Left=(Button)findViewById(R.id.header_left_btn); Left.setText("BACK"); Left.setTextColor(Color.GRAY); Button Title=(Button)findViewById(R.id.header_text); Title.setText("Scorecard"); Title.setTextColor(Color.GRAY); Button Right=(Button)findViewById(R.id.header_right_...