本文整理汇总了Java中android.net.NetworkInfo.getType方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInfo.getType方法的具体用法?Java NetworkInfo.getType怎么用?Java NetworkInfo.getType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.NetworkInfo
的用法示例。
在下文中一共展示了NetworkInfo.getType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getProxy
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static NetworkProxy getProxy(Context context) {
if (context == null) {
return null;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
if (connectivityManager == null) {
return null;
}
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo == null) {
return null;
}
if (activeNetworkInfo.getType() == 0) {
Object b = b(context);
int a = a(context);
if (!TextUtils.isEmpty(b) && a >= 0) {
return new NetworkProxy(b, a);
}
}
return null;
}
示例2: onReceive
import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, AutoLoginService.class);
try {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Log.d(LOG_TAG, "Have Wifi Connection");
AutoLoginService.setState(context,LoginState.START);
context.startService(serviceIntent);
}
else {
Log.d(LOG_TAG, "Don't have Wifi Connection");
AutoLoginService.setState(context,LoginState.STOPPED);
context.stopService(serviceIntent);
}
}
catch (Exception ex){
Log.e(LOG_TAG, ex.getMessage());
}
}
示例3: a
import android.net.NetworkInfo; //导入方法依赖的package包/类
private boolean a() {
if (this.d.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", this.d.getPackageName()) != 0) {
return false;
}
try {
NetworkInfo activeNetworkInfo = ((ConnectivityManager) this.d.getSystemService("connectivity")).getActiveNetworkInfo();
if (!(activeNetworkInfo == null || activeNetworkInfo.getType() == 1)) {
String extraInfo = activeNetworkInfo.getExtraInfo();
if (extraInfo != null && (extraInfo.equals("cmwap") || extraInfo.equals("3gwap") || extraInfo.equals("uniwap"))) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
示例4: getNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取当前网络类型
*
* @return -1:没有网络, 0:移动网络, 1:wifi网络
*/
public static int getNetworkType() {
int status = TYPE_NONE;
ConnectivityManager cm = (ConnectivityManager) BaseApplication.get().getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo == null) {
return status;
}
int mType = netInfo.getType();
if (mType == ConnectivityManager.TYPE_MOBILE) {
status = TYPE_MOBILE;
} else if (mType == ConnectivityManager.TYPE_WIFI) {
status = TYPE_WIFI;
}
return status;
}
示例5: hasNetworkConnection
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* Validate the network connection. Supports checking for WiFi only when
* this settings has been set to true.
*
* @param activity Activity
* @return Has network connection
*/
public static boolean hasNetworkConnection(Activity activity) {
ConnectivityManager connMgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null)
return false;
if (PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(Settings.PREF_WIFI_ONLY, false))
return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
return networkInfo.isConnected();
}
示例6: isNetworkNotOnWifiType
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isNetworkNotOnWifiType() {
final ConnectivityManager manager = (ConnectivityManager) FileDownloadHelper.getAppContext().
getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null) {
FileDownloadLog.w(FileDownloadUtils.class, "failed to get connectivity manager!");
return true;
}
final NetworkInfo info = manager.getActiveNetworkInfo();
return info == null || info.getType() != ConnectivityManager.TYPE_WIFI;
}
示例7: is3G
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* check is3G
*
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
示例8: isConnectedToWifi
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isConnectedToWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnected()
&& ni.getType() == ConnectivityManager.TYPE_WIFI;
}
示例9: bU
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean bU() {
try {
NetworkInfo activeNetworkInfo = ((ConnectivityManager) f.getServiceContext().getSystemService("connectivity")).getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.getType() == 1;
} catch (Exception e) {
return false;
}
}
示例10: isWifi
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isWifi(Context context) {
NetworkInfo networkINfo = ((ConnectivityManager) context.getSystemService("connectivity")
).getActiveNetworkInfo();
if (networkINfo == null || networkINfo.getType() != 1) {
return false;
}
return true;
}
示例11: getDeviceNetworkIp
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取手机ip地址,如果WiFi可以,则返回WiFi的ip,否则就返回网络ip.
*
* @param context 上下文
* @return ip
*/
public static String getDeviceNetworkIp(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context
.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { //wifi
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifi.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
return convertToIp(ip);
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { // gprs
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
//这里4.0以上部分手机返回的地址将会是ipv6地址,而且在4G下也验证了返回的是
//ipv6,而服务器不处理ipv6地址
if (!inetAddress.isLoopbackAddress() && !(inetAddress
instanceof Inet6Address)) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
Log.e(TAG, "e", e);
}
}
}
return DEFAULT_NETWORK_IP;
}
示例12: getCurrentNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取当前网络的类型
*
* @param context 上下文
* @return 当前网络的类型。具体类型可参照ConnectivityManager中的TYPE_BLUETOOTH、TYPE_MOBILE、TYPE_WIFI等字段。当前没有网络连接时返回NetworkUtils.NETWORK_TYPE_NO_CONNECTION
*/
public static int getCurrentNetworkType(Context context) {
NetworkInfo networkInfo
= ((ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
return networkInfo != null
? networkInfo.getType()
: NETWORK_TYPE_NO_CONNECTION;
}
示例13: getNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取当前网络类型
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @param context 上下文
* @return 网络类型
* <ul>
* <li>{@link NetworkUtils.NetworkType#NETWORK_WIFI } </li>
* <li>{@link NetworkUtils.NetworkType#NETWORK_4G } </li>
* <li>{@link NetworkUtils.NetworkType#NETWORK_3G } </li>
* <li>{@link NetworkUtils.NetworkType#NETWORK_2G } </li>
* <li>{@link NetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
* <li>{@link NetworkUtils.NetworkType#NETWORK_NO } </li>
* </ul>
*/
public static NetworkType getNetworkType(Context context) {
NetworkType netType = NetworkType.NETWORK_NO;
NetworkInfo info = getActiveNetworkInfo(context);
if (info != null && info.isAvailable()) {
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
netType = NetworkType.NETWORK_WIFI;
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
switch (info.getSubtype()) {
case NETWORK_TYPE_GSM:
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
netType = NetworkType.NETWORK_2G;
break;
case NETWORK_TYPE_TD_SCDMA:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
netType = NetworkType.NETWORK_3G;
break;
case NETWORK_TYPE_IWLAN:
case TelephonyManager.NETWORK_TYPE_LTE:
netType = NetworkType.NETWORK_4G;
break;
default:
String subtypeName = info.getSubtypeName();
if ("TD-SCDMA".equalsIgnoreCase(subtypeName) ||
"WCDMA".equalsIgnoreCase(subtypeName) ||
"CDMA2000".equalsIgnoreCase(subtypeName)) {
netType = NetworkType.NETWORK_3G;
} else {
netType = NetworkType.NETWORK_UNKNOWN;
}
break;
}
} else {
netType = NetworkType.NETWORK_UNKNOWN;
}
}
return netType;
}
示例14: getNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
* 获取当前网络类型
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @return 网络类型
* <ul>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_WIFI } </li>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_4G } </li>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_3G } </li>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_2G } </li>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
* <li>{@link XNetworkUtils.NetworkType#NETWORK_NO } </li>
* </ul>
*/
public static NetworkType getNetworkType() {
NetworkType netType = NetworkType.NETWORK_NO;
NetworkInfo info = getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
netType = NetworkType.NETWORK_WIFI;
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
switch (info.getSubtype()) {
case NETWORK_TYPE_GSM:
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
netType = NetworkType.NETWORK_2G;
break;
case NETWORK_TYPE_TD_SCDMA:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
netType = NetworkType.NETWORK_3G;
break;
case NETWORK_TYPE_IWLAN:
case TelephonyManager.NETWORK_TYPE_LTE:
netType = NetworkType.NETWORK_4G;
break;
default:
String subtypeName = info.getSubtypeName();
if (subtypeName.equalsIgnoreCase("TD-SCDMA")
|| subtypeName.equalsIgnoreCase("WCDMA")
|| subtypeName.equalsIgnoreCase("CDMA2000")) {
netType = NetworkType.NETWORK_3G;
} else {
netType = NetworkType.NETWORK_UNKNOWN;
}
break;
}
} else {
netType = NetworkType.NETWORK_UNKNOWN;
}
}
return netType;
}
示例15: checkNetworkType
import android.net.NetworkInfo; //导入方法依赖的package包/类
public static int checkNetworkType(Context context) {
int netType = 0;
//连接管理对象
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取NetworkInfo对象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo == null)
return netType;
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
case ConnectivityManager.TYPE_WIMAX:
case ConnectivityManager.TYPE_ETHERNET:
return 1;
case ConnectivityManager.TYPE_MOBILE:
switch (networkInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_LTE: // 4G
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_EHRPD:
return 2;
case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return 3;
case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
case TelephonyManager.NETWORK_TYPE_EDGE:
return 4;
default:
return netType;
}
default:
return netType;
}
}