下面列出了android.net.NetworkInfo#isConnected ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns connection type for |network|.
* Only callable on Lollipop and newer releases.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@ConnectionType
int getConnectionType(Network network) {
NetworkInfo networkInfo = getNetworkInfo(network);
if (networkInfo != null && networkInfo.getType() == TYPE_VPN) {
// When a VPN is in place the underlying network type can be queried via
// getActiveNeworkInfo() thanks to
// https://android.googlesource.com/platform/frameworks/base/+/d6a7980d
networkInfo = mConnectivityManager.getActiveNetworkInfo();
}
if (networkInfo != null && networkInfo.isConnected()) {
return convertToConnectionType(networkInfo.getType(), networkInfo.getSubtype());
}
return ConnectionType.CONNECTION_NONE;
}
/**
* Get network type name
*
* @param context
* @return
*/
public static String getNetworkTypeName(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo;
String type = NETWORK_TYPE_DISCONNECT;
if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
return type;
}
;
if (networkInfo.isConnected()) {
String typeName = networkInfo.getTypeName();
if ("WIFI".equalsIgnoreCase(typeName)) {
type = NETWORK_TYPE_WIFI;
} else if ("MOBILE".equalsIgnoreCase(typeName)) {
String proxyHost = android.net.Proxy.getDefaultHost();
type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G)
: NETWORK_TYPE_WAP;
} else {
type = NETWORK_TYPE_UNKNOWN;
}
}
return type;
}
private static int getNetworkClass(Context context) {
int networkType = NETWORK_TYPE_UNKNOWN;
try {
final NetworkInfo network = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (network != null && network.isAvailable()
&& network.isConnected()) {
int type = network.getType();
if (type == ConnectivityManager.TYPE_WIFI) {
networkType = NETWORK_TYPE_WIFI;
} else if (type == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
networkType = telephonyManager.getNetworkType();
}
} else {
networkType = NETWORK_TYPE_UNAVAILABLE;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return getNetworkClassByType(networkType);
}
private static void initConnectionStatus() {
ConnectivityManager cm = (ConnectivityManager)ClientProperties.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm == null) {
return;
}
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni != null && ni.isConnected()) {
_connected = 1;
_wifi = ni.getType() == ConnectivityManager.TYPE_WIFI;
if(!_wifi) {
TelephonyManager tm = (TelephonyManager)ClientProperties.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
_networkType = tm.getNetworkType();
}
} else {
_connected = 0;
}
}
private void waitForSomeNetworkToConnect() throws Exception {
final SettableFuture<Void> future = SettableFuture.create();
ConnectivityManager.NetworkCallback cb =
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
NetworkInfo netInfo = connManager.getNetworkInfo(network);
if (netInfo != null && netInfo.isConnected()) {
future.set(null);
}
}
};
connManager.requestNetwork(
new NetworkRequest.Builder().addCapability(NET_CAPABILITY_INTERNET).build(), cb);
try {
future.get(NETWORK_STATE_CHANGE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} finally {
connManager.unregisterNetworkCallback(cb);
}
}
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo currentNetworkInfo = (NetworkInfo) intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
// do application-specific task(s) based on the current network
// state, such
// as enabling queuing of HTTP requests when currentNetworkInfo is
// connected etc.
boolean not_mobile = currentNetworkInfo.getTypeName()
.equalsIgnoreCase("MOBILE") ? false : true;
if (currentNetworkInfo.isConnected()
&& (mobile_updates || not_mobile)) {
checkUpdates(false);
updateHandler.postDelayed(periodicUpdate, updateInterval);
} else {
updateHandler.removeCallbacks(periodicUpdate); // no network
// anyway
}
}
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info != null && info.isConnected()) {
Log.i(TAG, "isConnected!");
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent(WIFI_STATE_CONNECTED));
}
}
/**
* 判断当前的网络连接方式是否为WIFI
*
* @param context
* @return
*/
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiNetworkInfo.isConnected();
}
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true;
}
return false;
}
public boolean isConnectedAsPreferred() {
try {
if (isWifiOnly()) {
ConnectivityManager connectivityManager = (ConnectivityManager)
mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
activeNetworkInfo.isConnected();
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* @return the info of the network that is available to this app.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private NetworkInfo getActiveNetworkInfo() {
final NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return null;
}
if (networkInfo.isConnected()) {
return networkInfo;
}
// If |networkInfo| is BLOCKED, but the app is in the foreground, then it's likely that
// Android hasn't finished updating the network access permissions as BLOCKED is only
// meant for apps in the background. See https://crbug.com/677365 for more details.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// https://crbug.com/677365 primarily affects only Lollipop and higher versions.
return null;
}
if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOCKED) {
// Network state is not blocked which implies that network access is
// unavailable (not just blocked to this app).
return null;
}
if (ApplicationStatus.getStateForApplication()
!= ApplicationState.HAS_RUNNING_ACTIVITIES) {
// The app is not in the foreground.
return null;
}
return networkInfo;
}
private boolean shouldAttemptPost() {
PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(permission.ACCESS_NETWORK_STATE, context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
return false;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public boolean isConnectedToNetwork() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager)
mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
} catch (Exception e) {
return false;
}
}
private boolean isNetworkAvailable(Context ctx) {
ConnectivityManager connectivityManager
= (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state != WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.v(TAG, " WiFi P2P is no longer enabled.");
}
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected() && networkInfo.getTypeName().equals("WIFI_P2P")) {
salutInstance.isConnectedToAnotherDevice = true;
manager.requestConnectionInfo(channel, salutInstance);
} else {
salutInstance.isConnectedToAnotherDevice = false;
Log.v(TAG, "Not connected to another device.");
if (salutInstance.thisDevice.isRegistered) {
if (salutInstance.unexpectedDisconnect != null) {
salutInstance.unregisterClient(salutInstance.unexpectedDisconnect, null, false);
}
}
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
if (salutInstance.thisDevice.deviceName == null) {
salutInstance.thisDevice.deviceName = device.deviceName;
salutInstance.thisDevice.macAddress = device.deviceAddress;
}
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
/**
* 判断wifi是否连接
*
* @param context
* @return
*/
public static boolean isWifiConnected(Context context) {
NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
return net != null && net.getType() == ConnectivityManager.TYPE_WIFI && net.isConnected();
}
/**
* Checks if device is connected to a cellular network
*
* @param context the context
* @return true if connected
*/
public static boolean isCellularConnected(@NonNull Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}