本文整理汇总了Java中android.net.NetworkInfo.State类的典型用法代码示例。如果您正苦于以下问题:Java State类的具体用法?Java State怎么用?Java State使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
State类属于android.net.NetworkInfo包,在下文中一共展示了State类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ConnectivityReceiver
import android.net.NetworkInfo.State; //导入依赖的package包/类
public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
mTerminalManager = manager;
final ConnectivityManager cm =
(ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE);
final WifiManager wm = (WifiManager) manager.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mWifiLock = wm.createWifiLock(TAG);
final NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
mIsConnected = (info.getState() == State.CONNECTED);
}
mLockingWifi = lockingWifi;
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
manager.registerReceiver(this, filter);
}
示例2: onReceive
import android.net.NetworkInfo.State; //导入依赖的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();
}
}
}
示例3: getNetworkAccessMode
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static String[] getNetworkAccessMode(Context context) {
String[] strArr = new String[]{UNKNOW, UNKNOW};
if (context.getPackageManager().checkPermission("android.permission" +
".ACCESS_NETWORK_STATE", context.getPackageName()) != 0) {
strArr[0] = UNKNOW;
return strArr;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService
("connectivity");
if (connectivityManager == null) {
strArr[0] = UNKNOW;
return strArr;
} else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
strArr[0] = "Wi-Fi";
return strArr;
} else {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
if (networkInfo.getState() != State.CONNECTED) {
return strArr;
}
strArr[0] = "2G/3G";
strArr[1] = networkInfo.getSubtypeName();
return strArr;
}
}
示例4: j
import android.net.NetworkInfo.State; //导入依赖的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: updateState
import android.net.NetworkInfo.State; //导入依赖的package包/类
void updateState(State state) {
if (state != State.DISCONNECTED) {
mTryCounter = 0;
}
Setting s = mSetting; int resId;
switch (state) {
case CONNECTED: s.checked = true; s.enabled = true; resId = R.string.txt_net_status_connected; break;
case CONNECTING: s.checked = false; s.enabled = false; resId = R.string.txt_net_status_connecting; break;
case DISCONNECTED: s.checked = false; s.enabled = true; resId = R.string.txt_net_status_disconnected; break;
case DISCONNECTING: s.checked = true; s.enabled = false; resId = R.string.txt_net_status_disconnecting; break;
default: s.checked = s.enabled = false; resId = R.string.txt_status_unknown; break;
}
s.descr = getString(resId);
s.updateView();
}
示例6: getNetworkState
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Wifi
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return STATE_WIFI;
}
// 3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return STATE_MOBILE;
}
return STATE_DISCONNECT;
}
示例7: k
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static String[] k(Context context) {
String[] strArr = new String[]{"", ""};
try {
if (a(context, "android.permission.ACCESS_NETWORK_STATE")) {
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;
}
}
strArr[0] = "";
return strArr;
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: a
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static boolean a(Context context) {
if (!o.a(context, "android.permission.INTERNET")) {
return false;
}
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService("connectivity");
if (connectivityManager == null) {
return false;
}
Object allNetworkInfo = connectivityManager.getAllNetworkInfo();
if (l.b(allNetworkInfo)) {
for (NetworkInfo state : allNetworkInfo) {
if (state.getState() == State.CONNECTED) {
return true;
}
}
}
}
Toast.makeText(context, "Unable to connect to the network.", 0).show();
return false;
}
示例9: isNetworkAvailable
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static boolean isNetworkAvailable(Context context) {
if (context != null) {
NetworkInfo[] info = ((ConnectivityManager) context.getSystemService("connectivity"))
.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo state : info) {
if (state.getState() == State.CONNECTED) {
return true;
}
}
}
}
return false;
}
示例10: isWifiEnabled
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService
("connectivity");
return (mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState
() == State.CONNECTED) || ((TelephonyManager) context.getSystemService("phone"))
.getNetworkType() == 3;
}
示例11: getNetworkType
import android.net.NetworkInfo.State; //导入依赖的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: checkNet
import android.net.NetworkInfo.State; //导入依赖的package包/类
/**
* 判断Android客户端网络是否连接
* 只能判断是否有可用的连接,而不能判断是否能连网
*
* @param context
* @return true/false
*/
public static boolean checkNet(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}
示例13: getCurrentNetType
import android.net.NetworkInfo.State; //导入依赖的package包/类
/**
* 获取当前网络状态的类型
* @return 返回网络类型
*/
public NetType getCurrentNetType() {
ConnectivityManager connManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // wifi
NetworkInfo gprs = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // gprs
if (wifi != null && wifi.getState() == State.CONNECTED) {
Log.d(TAG, "getCurrentNetType | Current net type: WIFI.");
return NetType.NETWORK_TYPE_WIFI;
} else if (gprs != null && gprs.getState() == State.CONNECTED) {
Log.d(TAG, "getCurrentNetType | Current net type: MOBILE.");
return NetType.NETWOKR_TYPE_MOBILE;
} else {
Log.d(TAG, "getCurrentNetType | Current net type: NONE.");
return NetType.NETWORK_TYPE_NONE;
}
}
示例14: isConnected
import android.net.NetworkInfo.State; //导入依赖的package包/类
/**
* 判断网络是否连接
*
* @param
* @return
*/
public static boolean isConnected() {
ConnectivityManager connectivity = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
示例15: getNetworkState
import android.net.NetworkInfo.State; //导入依赖的package包/类
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Wifi
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_WIFI;
}
// 3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_MOBILE;
}
return NETWORN_NONE;
}