本文整理汇总了Java中android.net.NetworkInfo.getState方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInfo.getState方法的具体用法?Java NetworkInfo.getState怎么用?Java NetworkInfo.getState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.NetworkInfo
的用法示例。
在下文中一共展示了NetworkInfo.getState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isNetworkAvailable
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 判断网络是否可用
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
示例2: getConnectedNetworks
import android.net.NetworkInfo; //导入方法依赖的package包/类
private static List<NetworkInfo> getConnectedNetworks(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return null;
}
final List<NetworkInfo> list = new ArrayList<NetworkInfo>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
NetworkInfo[] networkInfoList = cm.getAllNetworkInfo();
if (networkInfoList != null) {
final int length = networkInfoList.length;
for (int i = 0; i < length; i++) {
if (networkInfoList[i].getState() == NetworkInfo.State.CONNECTED) {
list.add(networkInfoList[i]);
}
}
}
} else {
final Network[] networks = cm.getAllNetworks();
if (networks != null && networks.length > 0) {
NetworkInfo info;
for (Network network : networks) {
info = cm.getNetworkInfo(network);
if (info != null && info.getState() == NetworkInfo.State.CONNECTED) {
list.add(info);
}
}
}
}
return list;
}
示例3: isNetworkAvailable
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 检查当前网络是否可用
*
* @param activity 活动
* @return boolean boolean
*/
public static boolean isNetworkAvailable(@NonNull Activity activity) {
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (NetworkInfo aNetworkInfo : networkInfo) {
// 判断当前网络状态是否为连接状态
if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
示例4: onReceive
import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getExtras() != null) {
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
Log.i("app", "Network " + ni.getTypeName() + " connected");
if (connectionCallback != null) {
connectionCallback.hasActiveConnection(true);
}
}
}
if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
Log.d("app", "There's no network connectivity");
if (connectionCallback != null) {
connectionCallback.hasActiveConnection(false);
}
}
}
示例5: ConnectivityReceiver
import android.net.NetworkInfo; //导入方法依赖的package包/类
public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
mTerminalManager = manager;
final ConnectivityManager cm =
(ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE);
final WifiManager wm = (WifiManager) manager.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mWifiLock = wm.createWifiLock(TAG);
final NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
mIsConnected = (info.getState() == State.CONNECTED);
}
mLockingWifi = lockingWifi;
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
manager.registerReceiver(this, filter);
}
示例6: onReceive
import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.w(TAG, "onReceived() called: " + intent);
return;
}
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
Log.d(TAG, "onReceived() called; noConnectivity? " + noConnectivity + "; isFailover? " + isFailover);
if (noConnectivity && !isFailover && mIsConnected) {
mIsConnected = false;
mTerminalManager.onConnectivityLost();
} else if (!mIsConnected) {
NetworkInfo info = (NetworkInfo) intent.getExtras()
.get(ConnectivityManager.EXTRA_NETWORK_INFO);
if (mIsConnected = (info.getState() == State.CONNECTED)) {
mTerminalManager.onConnectivityRestored();
}
}
}
示例7: checkNet
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* @param context c
* @return true网路可用。false不可用
* @since 1.0.0
*/
public static boolean checkNet(Context context) {
try {
//获取连接管理对象
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
//获取活动的网络连接
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
LogUtils.e(e);
}
return false;
}
示例8: isConnected
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 判断网络是否连接
*/
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == cm) {
return false;
}
NetworkInfo info = cm.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
示例9: isNetworkAvailable
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 描述:判断网络是否有效.
*
* @return true, if is network available
*/
public static boolean isNetworkAvailable(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
示例10: onReceive
import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (null != parcelableExtra) {
NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
NetworkInfo.State state = networkInfo.getState();
boolean isConnected = state == NetworkInfo.State.CONNECTED;
if (isConnected) {
MySharedPre.getInstance().putIpAddress(Utils.getIpAddress());
} else {
MySharedPre.getInstance().putIpAddress("0.0.0.0");
}
}
}
}
示例11: isConnected
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 判断网络是否连接
*
* @param context context
* @return true is connected
*/
@Deprecated
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
示例12: checkNetwork
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* Prüft, ob das aktuelle Gerät mit dem Internet verbunden ist.
*
* @return true, falls eine aktive Netzwerkverbindung besteht; false, falls nicht
*/
public static boolean checkNetwork() {
ConnectivityManager c = (ConnectivityManager) getController().getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (c != null) {
NetworkInfo n = c.getActiveNetworkInfo();
if (n != null) {
return n.getState() == NetworkInfo.State.CONNECTED;
}
}
return false;
}
示例13: isNetworkConnected
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isNetworkConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
示例14: k
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static String[] k(Context context) {
String[] strArr = new String[]{"", ""};
try {
if (a(context, "android.permission.ACCESS_NETWORK_STATE")) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
if (connectivityManager == null) {
strArr[0] = "";
return strArr;
} else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
strArr[0] = d;
return strArr;
} else {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
if (networkInfo.getState() == State.CONNECTED) {
strArr[0] = c;
strArr[1] = networkInfo.getSubtypeName();
return strArr;
}
return strArr;
}
}
strArr[0] = "";
return strArr;
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: getNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取网络连接状态
*/
public static int getNetworkType(Context context) {
// NetworkInfo 有以下方法
// getDetailedState():获取详细状态。
// getExtraInfo():获取附加信息(3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap)
// getReason():获取连接失败的原因。
// getType():获取网络类型(一般为移动或Wi-Fi)。
// getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
// isAvailable():判断该网络是否可用。
// isConnected():判断是否已经连接。
// isConnectedOrConnecting():判断是否已经连接或正在连接。
// isFailover():判断是否连接失败。
// isRoaming():判断是否漫游
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
if (networkInfo != null) {
int type = networkInfo.getType();
State state = networkInfo.getState();
if (type == ConnectivityManager.TYPE_WIFI
&& state == State.CONNECTED) {
// wifi连接通畅
return NETWORK_CONNETED_WIFI;
} else if (type == ConnectivityManager.TYPE_MOBILE
&& state == State.CONNECTED) {
return NETWORK_CONNETED_GPRS;
}
}
return NETWORK_DISCONNETED;
}