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


Java NetworkInfo类代码示例

本文整理汇总了Java中android.net.NetworkInfo的典型用法代码示例。如果您正苦于以下问题:Java NetworkInfo类的具体用法?Java NetworkInfo怎么用?Java NetworkInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


NetworkInfo类属于android.net包,在下文中一共展示了NetworkInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getNetworkType

import android.net.NetworkInfo; //导入依赖的package包/类
/**
 * Get network type
 * 
 * @param context
 * @return
 */
public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? -1 : networkInfo.getType();
}
 
开发者ID:harrylefit,项目名称:eazycore,代码行数:13,代码来源:NetWorkUtils.java

示例2: isWifiConnectivityType

import android.net.NetworkInfo; //导入依赖的package包/类
/**
 * @brief Checks the connectivity type of the terminal
 * @return True if the connectivity type is Wifi, false in another case.
 * @details Checks the connectivity type of the terminal
 */
//devuelve true si es conexion wifi, false en caso contrario
private boolean isWifiConnectivityType(){
	ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo info = connectivityManager.getActiveNetworkInfo();
       
       String connectionType = info.getTypeName();
       
       if (connectionType.equalsIgnoreCase("wifi")){
       	return true;
       }
       else{
       	//3g u otro tipo
       	return false;
       }
       	
}
 
开发者ID:CodyyAndroid,项目名称:LibVNCAndroid,代码行数:22,代码来源:ActivityTabs.java

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

示例4: isNetworkAvailable

import android.net.NetworkInfo; //导入依赖的package包/类
public boolean isNetworkAvailable() {

        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
	}
 
开发者ID:NewCasino,项目名称:browser,代码行数:19,代码来源:MainApp.java

示例5: setNetworkInfo

import android.net.NetworkInfo; //导入依赖的package包/类
public static void setNetworkInfo(Context context, boolean isConnected, boolean isRoaming, boolean isWifi) {
    ShadowConnectivityManager manager =
            shadowOf((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
    NetworkInfo.DetailedState detailedState =
            isConnected ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED;
    int type = isConnected ? (isWifi ? ConnectivityManager.TYPE_WIFI : ConnectivityManager.TYPE_MOBILE) : -1;
    NetworkInfo networkInfo =
            ShadowNetworkInfo.newInstance(detailedState, type, 0, isConnected, isConnected, isRoaming);
    manager.setActiveNetworkInfo(networkInfo);
}
 
开发者ID:Doist,项目名称:JobSchedulerCompat,代码行数:11,代码来源:DeviceTestUtils.java

示例6: isRoaming

import android.net.NetworkInfo; //导入依赖的package包/类
public static boolean isRoaming() {
    try {
        ConnectivityManager cm = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null) {
            return netInfo.isRoaming();
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    return false;
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:13,代码来源:ConnectionsManager.java

示例7: onReceive

import android.net.NetworkInfo; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo =
                intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(networkInfo.isConnected()) {
            if(!mIsConnected) {
                Log.d(TAG, "Wifi is connected: " + String.valueOf(networkInfo));
                mListener.onConnectionStateChanged(true);
                mIsConnected = true;
            }
        } else {
            if(mIsConnected) {
                Log.d(TAG, "Wifi is disconnected: " + String.valueOf(networkInfo));
                mIsConnected = false;
                mListener.onConnectionStateChanged(false);
            }
        }
    }
}
 
开发者ID:Orange-OpenSource,项目名称:OCast-Java,代码行数:21,代码来源:WifiMonitor.java

示例8: networkChecker

import android.net.NetworkInfo; //导入依赖的package包/类
@Singleton
@Provides
NetworkChecker networkChecker(LoriApp app) {
    return () -> {
        ConnectivityManager internetManager = (ConnectivityManager) app.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = internetManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable() && networkInfo.isConnectedOrConnecting();
    };
}
 
开发者ID:zmeiko,项目名称:LoriApp,代码行数:10,代码来源:AppModule.java

示例9: getNetworkType

import android.net.NetworkInfo; //导入依赖的package包/类
/**
 * 获取当前网络状态类型
 *
 * @param context
 * @return
 */
public static int getNetworkType(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    int type = TYPE_NO;
    if (info != null && info.isAvailable()) {
        //获取网络类型
        int netWorkType = info.getType();
        if (netWorkType == ConnectivityManager.TYPE_WIFI) {
            type = TYPE_WIFI;
        } else if (netWorkType == ConnectivityManager.TYPE_MOBILE) {
            type = TYPE_MOBILE;
        } else {
            type = TYPE_UNKNOWN;
        }
    }
    return type;
}
 
开发者ID:xiarunhao123,项目名称:Ency,代码行数:24,代码来源:AppNetWorkUtil.java

示例10: isNetworkAvailable

import android.net.NetworkInfo; //导入依赖的package包/类
public static boolean isNetworkAvailable() {
    boolean status = false;
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null
            && netInfo.getState() == NetworkInfo.State.CONNECTED) {
        status = true;
    } else {
        netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null
                && netInfo.getState() == NetworkInfo.State.CONNECTED)
            status = true;
    }
    return status;
}
 
开发者ID:skydoves,项目名称:SyncMarket,代码行数:17,代码来源:SyncMarket.java

示例11: 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;
}
 
开发者ID:FacePlusPlus,项目名称:MegviiLicMgr-Android-SDK,代码行数:36,代码来源:NetWorkHelper.java

示例12: getNetworkType

import android.net.NetworkInfo; //导入依赖的package包/类
public static int getNetworkType() {
    NetworkInfo networkInfo = ((ConnectivityManager) BaseApplication.getInstance().getSystemService("connectivity")).getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isAvailable()) {
        return 0;
    }
    if (1 == networkInfo.getType()) {
        return 1;
    }
    switch (((TelephonyManager) BaseApplication.getInstance().getSystemService("phone")).getNetworkType()) {
        case 1:
        case 2:
        case 4:
            return 2;
        case 13:
            return 3;
        default:
            return 3;
    }
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:20,代码来源:NetworkUtils.java

示例13: checkPending

import android.net.NetworkInfo; //导入依赖的package包/类
/**
 * Checks if there is already a pending request for the user.
 *
 * @param name of user pending
 * @return Boolean value. Whether if there is a pending request for the user.
 * @throws Exception
 */
private boolean checkPending(String name) { // change to more specific exception later.
    // Check if app is connected to a network.
    ConnectivityManager cm = (ConnectivityManager) getActivity().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null == activeNetwork) {
        Toast.makeText(getActivity(), "You are offline.", Toast.LENGTH_SHORT).show();
    } else {
        ElasticSearchUserController.GetUserTask getUserTask =
                new ElasticSearchUserController.GetUserTask();
        getUserTask.execute(name.toLowerCase());
        try {
            User user2 = getUserTask.get().get(0); // get first user from result
            String currentUsername = CurrentUserSingleton.getInstance().getUser().getName();
            return user2.getPending().contains(currentUsername);
        } catch (Exception e) {
            Log.d("Error", "Unable to get user from elastic search");
        }
    }
    return false;
}
 
开发者ID:CMPUT301W17T08,项目名称:Moodr,代码行数:29,代码来源:StrangerProfile.java

示例14: isTunnelNeeded

import android.net.NetworkInfo; //导入依赖的package包/类
private boolean isTunnelNeeded(NetworkInfo info) {
	if (info == null) {
		Log.i("No connectivity: tunnel should be disabled");
		return false;
	}

	String pref = mPrefs.getTunnelMode();

	if (getString(R.string.tunnel_mode_entry_value_always).equals(pref)) {
		return true;
	}

	if (info.getType() != ConnectivityManager.TYPE_WIFI
			&& getString(R.string.tunnel_mode_entry_value_3G_only).equals(pref)) {
		Log.i("need tunnel: 'no wifi' connection");
		return true;
	}

	return false;
}
 
开发者ID:treasure-lau,项目名称:Linphone4Android,代码行数:21,代码来源:LinphoneManager.java

示例15: loadnet

import android.net.NetworkInfo; //导入依赖的package包/类
private void loadnet() {


        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE);
        //获取系统的连接服务。
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        //获取网络的连接情况。
        if (networkInfo != null && networkInfo.isAvailable()) {
          /*  if (networkInfo.getType()==connectivityManager.TYPE_WIFI){
                Toast.makeText(A.this,"网络已启动啦(WIFI)",Toast.LENGTH_SHORT).show();
            }else if (networkInfo.getType()==connectivityManager.TYPE_MOBILE) {
                Toast.makeText(A.this,"网络已启动啦(3G)",Toast.LENGTH_SHORT).show();

            }*/
            loadingLayout.setStatus(LoadingLayout.Success);//加载成功


        } else {
            loadingLayout.setStatus(LoadingLayout.Loading);
        }

    }
 
开发者ID:Shmilyz,项目名称:Swap,代码行数:23,代码来源:SpecialFragment.java


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