Open top menu



In this tutorial we will explain how to detect Internet connection. the internet connection we will find before  call http request and json etc , in our android application.

first you have declare internet accuses permission in Manifest.xml.



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

Internet_Status.java
..........................................................................................................................................................

package com.androidbeginnerpoint.testapplication;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Internet_Status {
   
    private Context _context;
   
    public Internet_Status(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;
    }
}


MainActivity.java

in this class create a Internet_ststus.java instance and find is return value.
.............................................................................................................................................................

package com.androidbeginnerpoint.testapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
boolean isInternet_connect= false;
 
// creating connection detector class instance
Internet_Status detect_internet=new Internet_Status(getApplication());
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Internetconnection tutorial");
setContentView(R.layout.activity_main);
isInternet_connect=detect_internet.isConnectingToInternet();
setContentView(R.layout.activity_main);
// check for Internet status
if(isInternet_connect) {
Toast.makeText(getApplication(), "Internet connection is persent", Toast.LENGTH_LONG).show();
AlertDialogBox("your internet connection is persent !");
  }
else {
AlertDialogBox("your internet connection is Not persent !");
}
 
}
public  void AlertDialogBox(String str) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("LOOK UP");
builder.setMessage(str);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});

builder.create().show();
}
}
Tagged

0 comments