Monday, 3 September 2012

Check Network Connection in Android

Check Network Connection in Android
Hello Friends,


Note: Declare following network permission in your  
           Androidmanifest.xml file:  
         
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  























1. MainActivity.java


package com.mukesh.networkcheck;

import com.mukesh.netwoekcheck.R;

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final int REQUEST_CODE = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean isEnabled = isNetworkEnable();

        displayNetworkState(isEnabled);

        if (!isEnabled) {
            buildAlertMessageNoGps();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            displayNetworkState(isNetworkEnable());
        }
    }

    private void displayNetworkState(boolean isEnabled) {
        String status = "Your Network is "
                + (isEnabled ? "Enabled" : "Disabled");

        ((TextView) findViewById(R.id.text_gpsstatus)).setText(status);
    }

    private boolean isNetworkEnable() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        // ARE WE CONNECTED TO THE NET

        if (conMgr.getActiveNetworkInfo() != null
                && conMgr.getActiveNetworkInfo().isAvailable()
                && conMgr.getActiveNetworkInfo().isConnected()) {

            return true;

        } else {

            Log.v("TAG", "Internet Connection Not Present");

            return false;

        }
    }

    private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(R.string.network_availability_title);
        builder.setMessage(R.string.network_availability_message);
        builder.setCancelable(false);

        builder.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog,
                            final int id) {
                        launchNetworkOptions();
                    }
                });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int id) {
                dialog.cancel();
            }
        });

        builder.create().show();
    }

    private void launchNetworkOptions() {
        startActivityForResult(new Intent(
                android.provider.Settings.ACTION_WIRELESS_SETTINGS),
                REQUEST_CODE);
    }
}


2. AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mukesh.netwoekcheck"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mukesh.networkcheck.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download Source Code: Network Check





Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

1 comments:

  1. source code link is broken

    Please Update

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin