本文整理汇总了Java中android.net.ConnectivityManager.getNetworkInfo方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager.getNetworkInfo方法的具体用法?Java ConnectivityManager.getNetworkInfo怎么用?Java ConnectivityManager.getNetworkInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.ConnectivityManager
的用法示例。
在下文中一共展示了ConnectivityManager.getNetworkInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConnectedNetworks
import android.net.ConnectivityManager; //导入方法依赖的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;
}
示例2: checkconnection
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public void checkconnection()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
//connection is avlilable
}else{
//no connection
Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection Please make Internet for Contine",
Toast.LENGTH_SHORT);
toast.show();
}
}
示例3: onReceive
import android.net.ConnectivityManager; //导入方法依赖的package包/类
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ( activeNetInfo != null ) {
wifiState.setCompoundDrawablesWithIntrinsicBounds(null, null, meh, null);
Toast.makeText(context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
}
if( mobNetInfo != null ) {
wifiState.setCompoundDrawablesWithIntrinsicBounds(null, null, good, null);
Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( activeNetInfo == null && mobNetInfo == null) {
Toast.makeText( context, "No internet for you", Toast.LENGTH_SHORT ).show();
wifiState.setCompoundDrawablesWithIntrinsicBounds(null, null, bad, null);
}
}
示例4: j
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static String[] j(Context context) {
String[] strArr = new String[]{"", ""};
try {
if (context.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", context.getPackageName()) != 0) {
strArr[0] = "";
return strArr;
}
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;
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: isConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public Boolean isConnected()
{
ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null)
{
if (info.isConnected())
{
return true;
}
}
}
return false;
}
示例6: isMobileConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
* 判断MOBILE网络是否可用
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null && mMobileNetworkInfo.isAvailable()) {
return mMobileNetworkInfo.isConnected();
}
}
return false;
}
示例7: isWifiConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
* 判断WIFI网络是否可用
*/
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null && mWiFiNetworkInfo.isAvailable()) {
return mWiFiNetworkInfo.isConnected();
}
}
return false;
}
示例8: isWIFIConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
* 判断手机是否采用wifi连接
*/
private static boolean isWIFIConnected(Context context)
{
// Context.CONNECTIVITY_SERVICE).
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected())
{
// Log.e("Net", "Wifi 已连接");
return true;
}
// Log.e("Net", "无Wifi连接");
return false;
}
示例9: onReceive
import android.net.ConnectivityManager; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiNetworkInfo.isConnected() || dataNetworkInfo.isConnected()) {
mView.onRestoreNetworkRefreshUI();
} else {
mView.onDisconnectionRefreshUI();
}
}else {
Network[] networks = connMgr.getAllNetworks();
StringBuilder sb = new StringBuilder();
//通过循环将网络信息逐个取出来
for (int i=0; i < networks.length; i++){
//获取ConnectivityManager对象对应的NetworkInfo对象
NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
if (networkInfo.isConnected()){
mView.onRestoreNetworkRefreshUI();
break;
}else {
mView.onDisconnectionRefreshUI();
break;
}
}
}
}
}
示例10: netIsConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean netIsConnected(Context context) {
ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//手机网络连接状态
NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//WIFI连接状态
NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
//当前无可用的网络
return false;
}
return true;
}
示例11: lollipopChangeDefaultNetwork
import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void lollipopChangeDefaultNetwork(final ConnectivityManager cm, Context context) {
Network[] array = cm.getAllNetworks();
for (Network network : array) {
NetworkInfo info = cm.getNetworkInfo(network);
mLogger.info("Network: \"" + network + "\"\nInfo: " + info);
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
mLogger.info("Setting the network: \"" + network + "\" as default (" + ConnectivityManager.setProcessDefaultNetwork(network) + ")");
}
}
}
示例12: isWifiConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean isWifiConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return info != null && info.isConnected();
}
示例13: isMobileConnected
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
示例14: isWifiAvaliable
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean isWifiAvaliable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
@SuppressWarnings("deprecation") NetworkInfo wifiWorkInfo = manager.getNetworkInfo(TYPE_WIFI);
return wifiWorkInfo != null && wifiWorkInfo.isAvailable();
}
示例15: checkconnection
import android.net.ConnectivityManager; //导入方法依赖的package包/类
public void checkconnection()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//connection is avlilable
//no connection
running = (wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected());
}