Monday, 27 June 2016

Checking internet connection in android





If you are developing an Android app you may already fetching information from internet. While doing so there is a chance that internet connection is not available on users handset. Hence its always a good idea to check the network state before performing any task that requires internet connection.
You might also want to check what kind of internet connection is available in handset. For example is wifi currently enabled? or is mobile data network is connected
Checking internet connection in android
-------------------------------------------
activity_main.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
   
android:gravity="center"
   
tools:context="com.sgp.anil.checkinginternetconnection.MainActivity">


    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Google"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal" />
</
LinearLayout>
InternetCheckClass:
---------------------
package com.sgp.anil.checkinginternetconnection;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
 * Created by Anil on 27-06-2016.
 */

public class InternetCheckingClass {
   
private Context _context;

   
public InternetCheckingClass(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
--------------------------
package com.sgp.anil.checkinginternetconnection;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button
googlebtn;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
googlebtn=(Button)findViewById(R.id.button);
       
googlebtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
if(new InternetCheckingClass(getApplicationContext()).isConnectingToInternet()){
                    startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));

                }
else {
                    Snackbar snackbar=Snackbar.make(v,
"Network is Not Connected Check Your Interne tConnection",Snackbar.LENGTH_LONG);
                    snackbar.show();
                }
            }
        });
    }
}

goto AndroidManifest.xml give Permissions
<uses-permission android:name="android.permission.INTERNET"/>
 <
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>











No comments:

Post a Comment