Home »
Android
Android - Detect on which Network (WiFi or Mobile Data) Mobile is connected.
In this code snippet/application, we will learn how to detect connected network, this code will print Android device is connected on either WiFi or Mobile data.
Android Code Snippet - To Detect Connected Network
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.includehelp.networkinfo.MainActivity">
<Button
android:id="@+id/btnchk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check Network Info"
android:layout_marginTop="20dp"
android:onClick="getNetworkStatus"/>
<TextView
android:id="@+id/showstatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:layout_below="@id/btnchk"/>
</RelativeLayout>
MainActivity.java
package com.includehelp.networkinfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txtStatus;
NetworkInfo wifiInfo;
NetworkInfo mobInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtStatus = (TextView)findViewById(R.id.showstatus);
}
public void getNetworkStatus(View v){
ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo();
for (NetworkInfo netInfo : networkInfo) {
if (netInfo.getTypeName().equalsIgnoreCase("WIFI")) {
if (netInfo.isConnected()) {
txtStatus.setText("Wifi Connected");
}
}
else if (netInfo.getTypeName().equalsIgnoreCase("MOBILE")) {
if (netInfo.isConnected()) {
txtStatus.setText("Mobile Data Connected");
}
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.includehelp.networkinfo">
<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: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>
</application>
</manifest>
Result