Android Check Network Status Tool Class Explanation
- 2021-09-05 00:47:14
- OfStack
When developing applications with network interaction in Android, sometimes we need to check the network status to determine whether to request the network, so we need to use public classes
Code:
package com.example.ldp.com.util;
/**
* Created by Administrator on 2017/4/7.
*/
import android.content.Context;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;
import com.example.ldp.com.forestteaching.R;
/**
* Description :NetUtil For Network monitoring class
* Author:ldp
* Data:2017/4/7
*/
public class NetUtil {
/**
* Judge the network situation
*
* @param context Context
* @return false Indicates that there is no network true Indicates that there is a network
*/
public static boolean isNetworkAvalible(Context context) {
// Get Network State Manager
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// Create a network array
NetworkInfo[] net_info = connectivityManager.getAllNetworkInfo();
if (net_info != null) {
for (int i = 0; i < net_info.length; i++) {
// Whether the obtained network state is in a connected state or not is judged
if (net_info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
// If there is no network, the Network Settings dialog box pops up
public static void checkNetwork(final Activity activity) {
if (!NetUtil.isNetworkAvalible(activity)) {
TextView msg = new TextView(activity);
msg.setText(" There is no available network at present, please set up the network! ");
new AlertDialog.Builder(activity).setIcon(R.drawable.ic_launcher).setTitle(" Network status prompt ").setView(msg).setPositiveButton(" Determine ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Jump to Settings Interface
activity.startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0);
}
}).create().show();
}
return;
}
/**
* Determine whether the network is connected or not
**/
public static boolean netState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// Object that represents the networking state NetWorkInfo Object
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
// Gets whether the current network connection is available
boolean available = false;
try {
available = networkInfo.isAvailable();
} catch (Exception e) {
e.printStackTrace();
return false;
}
if (available) {
Log.i(" Notice ", " The current network connection is available ");
return true;
} else {
Log.i(" Notice ", " The current network connection is available ");
return false;
}
}
/**
* On that basi of connecting to the network , Determining whether the device is SIM Network connection
*
* @param context
* @return
*/
public static boolean IsMobileNetConnect(Context context) {
try {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if (State.CONNECTED == state)
return true;
else
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}