Monday, 19 August 2013

Detect Network availability for Mobile Data as well as for Wifi! (Updated with androidx package)

When you are developing an application which needs network connection, you will first have to check that whether the connection has established properly. If yes, then let the application proceeds further otherwise display that there is no internet connection.

Here you can see that when the application is opened, the network availability is checked and if network is established the Congrats text is set in the TextView, but if no network is found, the alert dialog with 'No Network' is displayed.

Please Note:
In this example, the network availability is checked in onCreate() method, but in realistic applications, it should be checked in the onResume() method as the onCreate() method gets called only once. But as you navigate from one activity to another, the onResume() and onPause() methods get called. So every time the activity is displayed, the network connection availability should be checked if your app has network dependency, otherwise the app may crash.   




----------------------------------------------------------------------------------------------------------------------------
In detect_network_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">

<TextView
android:id="@+id/dn_checkNetworkTV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#f05442"
android:textSize="25dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textStyle="bold"
android:text=""/>

</LinearLayout>
-----------------------------------------------------------------------------------------------------------------------------
In DetectNetworkActivity.java:
package com.app.yourappname;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;

public class DetectNetworkActivity extends AppCompatActivity
{
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.detect_network_activity);
TextView tv = findViewById(R.id.dn_checkNetworkTV);
if(!isNetworkAvailable())
showMessage();
else
tv.setText("Congratulations! \n Network connection established!");
}

void showMessage()
{
AlertDialog.Builder b = new AlertDialog.Builder(this)
.setTitle("No Network!")
.setMessage("No network connection found.")
.setCancelable(true)
.setIcon(R.drawable.alert_icon)
.setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
}
});
b.show();
}

boolean isNetworkAvailable()
{
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
return (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED);
}
}

-----------------------------------------------------------------------------------------------------------------------------
In Android Manifest file, add network permissions and declare Activity name:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

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

<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=".DetectNetworkActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

The alert image is downloaded from pixaby.

No comments:

Post a Comment