How to Monitor the Network Connection Status in Android

How to monitor network status in android

Your question is not clear!

If checking the network connection is what you want, the following will do.

// Check network connection
private boolean isNetworkConnected(){
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Android - Monitoring internet connection status

This is how I did to monitor the internet connection status. Create a Java class and name it as NetworkStateChangeReceiver.

NetworkStateChangeReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import static android.content.Context.CONNECTIVITY_SERVICE;

public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "yourapp.packagename.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";

@Override
public void onReceive(Context context, Intent intent) {
Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isConnectedToInternet(context));

LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
}

private boolean isConnectedToInternet(Context context) {
try {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
return false;
} catch (Exception e) {
Log.e(NetworkStateChangeReceiver.class.getName(), e.getMessage());
return false;
}
}
}

In your MainActivity, add the following lines.

IntentFilter intentFilter = new 
IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
String networkStatus = isNetworkAvailable ? "Connected!" : "Disconnected!";

final SweetAlertDialog CC = new SweetAlertDialog(MainActivity.this, SweetAlertDialog.WARNING_TYPE);
CC.setTitleText("Network Status");
CC.setContentText(networkStatus);
CC.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
CC.setCancelable(false);
CC.show();

You could use a simple alertdialog or a toast to show the message. I had used the SweetAlertDialog Library for cleaner UI.

How can I monitor the network connection status in Android?

Listen for CONNECTIVITY_ACTION

This looks like good sample code. Here is a snippet:

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
Log.w("Network Listener", "Network Type Changed");
}
};

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);

How to get network status in android?

To get the WiFi strength, you can create the following function:

public static int calculateSignalLevel (int Rssi, int numLevels){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int numOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numOfLevels);
}

If you want to get the status of the device and whether it is connected to the internet, you can use the following function:

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

Finally, to get the name of the phone's provider, you can use the following function:

public static String carrierName(){
TelephonyManager manager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
}

I hope this answers your question.

How to check Internet access at realtime?

Here's something I cooked earlier; maybe it will help:

    public class NetworkStateMonitor extends BroadcastReceiver {
Context mContext;
boolean mIsUp;

public interface Listener {
void onNetworkStateChange(boolean up);
}

public NetworkStateMonitor(Context context) {
mContext = context;
//mListener = (Listener)context;
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
mIsUp = isUp();
}

/**
* call this when finished with it, and no later than onStop(): callback will crash if app has been destroyed
*/
public void unregister() {
mContext.unregisterReceiver(this);
}

/*
* can be called at any time
*/
public boolean isUp() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isConnectedOrConnecting();
}

/**
* registerReceiver callback, passed to mListener
*/
@Override public void onReceive(Context context, Intent intent) {
boolean upNow = isUp();
if (upNow == mIsUp) return; // no change
mIsUp = upNow;
((Listener)mContext).onNetworkStateChange(mIsUp);
}
}

Android check Internet connection automatically

You can use a utility class and BroadcastReceiver for this purpose. Below link shows show step by step procedure

http://viralpatel.net/blogs/android-internet-connection-status-network-change/

Android - Programmatically check internet connection and display dialog if notConnected

Finally, I got the answer.

ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null){
Description.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(WelcomePage.this)
.setTitle(getResources().getString(R.string.app_name))
.setMessage(getResources().getString(R.string.internet_error))
.setPositiveButton("OK", null).show();
}else{
dialog = ProgressDialog.show(WelcomePage.this, "", "Loading...", true,false);
new Welcome_Page().execute();
}


Related Topics



Leave a reply



Submit