TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Android Network Connectivity Services

Android Network Connectivity Services with examples of Activity and Intent, Fragments, Menu, Service, alarm manager, storage, sqlite, xml, json, multimedia, speech, web service, telephony, animation and graphics

<< Back to ANDROID

Android Network Connectivity Services

Android network connectivity services allow us to check the network connectivity information of the device. It is very important to check the internet connection of the device while performing the task which is based on internet service such as fetching data from the server (internet) or writing data to the server.

Using Android Network Connectivity Services we can also determine the types of a network of android device. It may be of types TYPE_WIFI (wifi), TYPE_MOBILE (mobile), TYPE_BLUETOOTH (Bluetooth), etc.

Android Network Connectivity Service Example

Let's create a simple example to check the network connectivity of the device as well as its type.

To access the network connectivity of a device, we need to provide the network access permission in AndroidMenifest.xml file.

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

In the activity_main.xml file of layout add the following code. For accessing the network state of a device, there is no requirement of the layout file. Here, we are using the activity_main.xml file only for storing the state of network type.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.TheDeveloperBlog.com.networkconnectivityservice.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="networkstate"
        android:text="Network state ?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.183" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Check Connection Type"
        android:layout_marginTop="392dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Create a java class named as NetworkState.java. This class contains a getConnectivityStatus() method which returns the constant integer value on the basis of current network connection.

The code context.getSystemService(Context.CONNECTIVITY_SERVICE) is used to return the instance of the ConnectivityManager class which access the network properties.

NetworkState.java

package example.TheDeveloperBlog.com.networkconnectivityservice;


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

public class NetworkState {
    public static int TYPE_NOT_CONNECTED = 0;
    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;

    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        }
        return TYPE_NOT_CONNECTED;
    }

    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkState.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkState.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkState.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkState.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }
}

Create a receiver class named as NetworkReceiver.java and extends the BroadcastReciever class. This class handles the changes occur in the network state of a device. The onReceive() method of BroadcastReciever class called when the network state of device changes.

NetworkReceiver.java

package example.TheDeveloperBlog.com.networkconnectivityservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class NetworkReceiver extends BroadcastReceiver {
    static String  status =null;
    @Override
    public void onReceive(Context context, Intent intent) {
        status = NetworkState.getConnectivityStatusString(context);
        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
        if(status == "Wifi enabled"){
            //your code when wifi enable
        }
        else if(status=="Mobile data enabled"){
            //your code when TYPE_MOBILE network enable
        }
        else if(status=="Not connected to Internet"){
            //your code when no network connected
        }
    }
}

In the MainActivity.java class, we are displaying the network state of a device in TextView by clicking the Button.

MainActivity.java

package example.TheDeveloperBlog.com.networkconnectivityservice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NetworkReceiver receiver = new NetworkReceiver() ;
                String networkStatus = receiver.status;
                textView.setText("connection type: "+networkStatus);
                Toast.makeText(getApplicationContext(), networkStatus, Toast.LENGTH_LONG).show();
            }
        });
    }
}

In the AndroidMenifest.xml file, add the network access permission and receiver class that handles the changes occur in BroadcastReceiver class.

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.TheDeveloperBlog.com.networkconnectivityservice">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".NetworkReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Output

Android Network Connectivity Services Android Network Connectivity Services




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf