当前位置: 首页>>代码示例>>Java>>正文


Java ConnectivityManager.getNetworkInfo方法代码示例

本文整理汇总了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;
}
 
开发者ID:LightSun,项目名称:android-util2,代码行数:31,代码来源:NetworkCompatUtil.java

示例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();
    }
}
 
开发者ID:Shobhit-pandey,项目名称:CollegeDoc,代码行数:18,代码来源:MainActivity.java

示例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);
    }
}
 
开发者ID:feup-infolab,项目名称:labtablet,代码行数:20,代码来源:SubmissionStep1.java

示例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();
    }
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:28,代码来源:bt.java

示例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;
}
 
开发者ID:MSay2,项目名称:EasyUpdateApplication,代码行数:17,代码来源:MainActivity.java

示例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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:NetUtil.java

示例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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:NetUtil.java

示例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;
}
 
开发者ID:qingfeng14,项目名称:4over6,代码行数:17,代码来源:NetCheck.java

示例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;
            }
        }
    }
}
}
 
开发者ID:funnyzhaov,项目名称:Tribe,代码行数:32,代码来源:NetWorkStateReceiver.java

示例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;
}
 
开发者ID:hshare,项目名称:MVPArmsTest1,代码行数:13,代码来源:DeviceUtils.java

示例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) + ")");
		}
	}
}
 
开发者ID:hoanglm4,项目名称:RxAndroidTBP,代码行数:13,代码来源:WifiNetworkUtils.java

示例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();
}
 
开发者ID:Assassinss,项目名称:Moment,代码行数:7,代码来源:NetUtils.java

示例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;
}
 
开发者ID:liuyongfeng90,项目名称:JKCloud,代码行数:11,代码来源:NetUtils.java

示例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();
}
 
开发者ID:Yuanhongliang,项目名称:HLOLI,代码行数:6,代码来源:NetworkUtil.java

示例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());
    }
 
开发者ID:Shobhit-pandey,项目名称:CollegeDoc,代码行数:13,代码来源:Topics.java


注:本文中的android.net.ConnectivityManager.getNetworkInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。