本文整理汇总了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();
}
示例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;
}
}
示例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();
}
}
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
}
}
}
示例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();
};
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
示例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;
}
示例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;
}
示例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);
}
}