本文整理汇总了Java中android.net.ConnectivityManager.TYPE_WIFI属性的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager.TYPE_WIFI属性的具体用法?Java ConnectivityManager.TYPE_WIFI怎么用?Java ConnectivityManager.TYPE_WIFI使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.net.ConnectivityManager
的用法示例。
在下文中一共展示了ConnectivityManager.TYPE_WIFI属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isWifiAvailable
/**
* 判断WiFi是否可用
*
* @param context Context
* @return 表示网络可用
*/
public static boolean isWifiAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
return info != null && info.getType() == ConnectivityManager.TYPE_WIFI
&& info.isAvailable();
}
return false;
}
示例2: isWifi
/**
* 当前网络是否是wifi网络
*
* @param context
* @return
*/
public static boolean isWifi(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null) {
if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else {
return false;
}
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
示例3: isTunnelNeeded
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;
}
示例4: getNetworkType
/**
* 判断网络类型
*
* @param context 上下文
* @return -1:没有网络 1:WIFI网络 2:wap网络 3:net网络
*/
public static int getNetworkType(Context context) {
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
netType = 3;
} else {
netType = 2;
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1;
}
return netType;
}
示例5: doInBackground
@Override
public Long doInBackground(Void... unused) {
Log.d(TAG, "Upload Blob Task:");
MainActivity.YassPreferences preferences = new MainActivity.YassPreferences(context);
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
Log.d(TAG, "Skipping camera upload because network is disconnected");
return null;
}
if (preferences.cameraUploadOnlyOnWifi && activeNetwork.getType() != ConnectivityManager.TYPE_WIFI) {
Log.d(TAG, "Skipping camera upload because Wifi is disconnected");
return null;
}
long serial;
Uri uri;
String fileName;
long fileSize;
SQLiteDatabase db = new YassDbHelper(context).getReadableDatabase();
try {
String[] projection = {
"serial",
"file_uri",
"file_name",
"file_size"
};
String selection = null;
String[] selectionArgs = null;
String groupBy = null;
String having = null;
String orderBy = "serial ASC";
String limit = "1";
Cursor cursor = db.query("camera_uploads", projection, selection, selectionArgs,
groupBy, having, orderBy, limit);
try {
if (!cursor.moveToNext()) {
Log.d(TAG, "Did not find image to upload");
return null;
}
serial = cursor.getLong(cursor.getColumnIndexOrThrow("serial"));
uri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow("file_uri")));
fileName = cursor.getString(cursor.getColumnIndexOrThrow("file_name"));
fileSize = cursor.getLong(cursor.getColumnIndexOrThrow("file_size"));
} finally {
cursor.close();
}
} finally {
db.close();
}
Log.d(TAG, "Found image to upload: " + fileName);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(fileSize);
metadata.setContentType(context.getContentResolver().getType(uri));
PutObjectResult result;
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
// TODO: limited to 5 GB
result = MainActivity.getS3Client(preferences).putObject(preferences.bucketName,
"Camera Uploads/" + fileName, is, metadata);
} catch (AmazonClientException | IOException e) {
Log.e(TAG, "Could not upload file: " + e.getMessage());
return null;
}
return serial;
}
示例6: onReceive
@Override
public void onReceive(Context context, Intent intent) {
if(PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getBoolean("wifi_sync_only", false)) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
int networkType = connManager.getActiveNetworkInfo().getType();
if(networkType != ConnectivityManager.TYPE_WIFI){
sendDebugNotification("Not on WiFi, not syncing...", context);
return;
}
}
sendDebugNotification("Syncing with server...", context);
FlashManager flashManager = new FlashManager(context);
flashManager.refresh();
sendDebugNotification("Sync completed!", context);
}
示例7: getNetworkType
/**
*
* Get Current Network Type
*
* @return
*/
public static String getNetworkType() {
ConnectivityManager cm = (ConnectivityManager) CommonApp.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info == null ? "None" : (ConnectivityManager.TYPE_WIFI == info.getType() ?
"Wifi" : "Mobile"));
}
示例8: getNetworkType
/**
* 返回手机连接网络类型
* @param context
* @return 0: 无连接 1:wifi 2: mobile
*/
public static int getNetworkType(Context context)
{
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
int networkType = NOCONNECTION;
if (networkInfo != null)
{
int type = networkInfo.getType();
networkType = type == ConnectivityManager.TYPE_WIFI ? WIFI : MOBILE;
}
return networkType;
}
示例9: translateNetworkTypeToApiFlag
/**
* Translate a ConnectivityManager.TYPE_* constant to the corresponding
* DownloadManager.Request.NETWORK_* bit flag.
*/
private int translateNetworkTypeToApiFlag(int networkType) {
switch (networkType) {
case ConnectivityManager.TYPE_MOBILE:
return DownloadManager.Request.NETWORK_MOBILE;
case ConnectivityManager.TYPE_WIFI:
return DownloadManager.Request.NETWORK_WIFI;
default:
return 0;
}
}
示例10: getConnectedType
public static NetType getConnectedType(Context context) {
NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
if (net != null) {
switch (net.getType()) {
case ConnectivityManager.TYPE_WIFI:
return NetType.Wifi;
case ConnectivityManager.TYPE_MOBILE:
return NetType.Mobile;
default:
return NetType.Other;
}
}
return NetType.None;
}
示例11: updateConnectedFlags
private void updateConnectedFlags() {
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) {
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
} else {
wifiConnected = false;
mobileConnected = false;
}
}
示例12: getNetworkType
/**
* 获取当前网络类型
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @return 网络类型
* <ul>
* <li>{@link NetworkType#NETWORK_WIFI } </li>
* <li>{@link NetworkType#NETWORK_4G } </li>
* <li>{@link NetworkType#NETWORK_3G } </li>
* <li>{@link NetworkType#NETWORK_2G } </li>
* <li>{@link NetworkType#NETWORK_UNKNOWN} </li>
* <li>{@link 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;
}
示例13: getNetworkType
/**
* GPRS 2G(2.5) General Packet Radia Service 114kbps
* EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
* UMTS 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
* CDMA 2G 电信 Code Division Multiple Access 码分多址
* EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
* EVDO_A 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
* 1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
* HSDPA 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
* HSUPA 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
* HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access
* IDEN 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
* EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
* LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
* EHRPD 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
* HSPAP 3G HSPAP 比 HSDPA 快些
*
* @return {@link NetWorkType}
*/
public static NetWorkType getNetworkType(Context context) {
int type = getConnectedTypeINT(context);
switch (type) {
case ConnectivityManager.TYPE_WIFI:
return NetWorkType.Wifi;
case ConnectivityManager.TYPE_MOBILE:
case ConnectivityManager.TYPE_MOBILE_DUN:
case ConnectivityManager.TYPE_MOBILE_HIPRI:
case ConnectivityManager.TYPE_MOBILE_MMS:
case ConnectivityManager.TYPE_MOBILE_SUPL:
int teleType = getTelephonyManager(context).getNetworkType();
switch (teleType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NetWorkType.Net2G;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
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:
return NetWorkType.Net3G;
case TelephonyManager.NETWORK_TYPE_LTE:
return NetWorkType.Net4G;
default:
return NetWorkType.UnKnown;
}
default:
return NetWorkType.UnKnown;
}
}
示例14: onReceive
@Override
public void onReceive(Context context, Intent intent) {
// 这个监听wifi的打开与关闭,与wifi的连接无关
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
Log.e(TAG, "wifiState" + wifiState);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
Log.d(TAG, "onReceive: WIFI_STATE_DISABLED");
break;
case WifiManager.WIFI_STATE_DISABLING:
break;
case WifiManager.WIFI_STATE_ENABLING:
break;
case WifiManager.WIFI_STATE_ENABLED:
Log.d(TAG, "onReceive: WIFI_STATE_ENABLED");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
break;
default:
break;
}
}
// 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager
// .WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。
// 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,
// 当然刚打开wifi肯定还没有连接到有效的无线
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (null != parcelableExtra) {
NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
NetworkInfo.State state = networkInfo.getState();
boolean isConnected = state == State.CONNECTED;// 当然,这边可以更精确的确定状态
Log.e(TAG, "isConnected" + isConnected);
if (isConnected) {
} else {
}
}
}
// 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.
// 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。见log
// 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Log.i(TAG, "CONNECTIVITY_ACTION");
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.isConnected()) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Log.e(TAG, "当前WiFi连接可用 ");
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Log.e(TAG, "当前移动网络连接可用 ");
}
} else {
Log.e(TAG, "当前没有网络连接,请确保你已经打开网络 ");
}
Log.e(TAG, "info.getTypeName()" + activeNetwork.getTypeName());
Log.e(TAG, "getSubtypeName()" + activeNetwork.getSubtypeName());
Log.e(TAG, "getState()" + activeNetwork.getState());
Log.e(TAG, "getDetailedState()"+ activeNetwork.getDetailedState().name());
Log.e(TAG, "getDetailedState()" + activeNetwork.getExtraInfo());
Log.e(TAG, "getType()" + activeNetwork.getType());
} else { // not connected to the internet
Log.e(TAG, "当前没有网络连接,请确保你已经打开网络 ");
}
}
}
示例15: GetNetworkType
public static String GetNetworkType() {
String strNetworkType = "";
ConnectivityManager cm = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
strNetworkType = "WIFI";
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
String _strSubTypeName = networkInfo.getSubtypeName();
Log.e("cocos2d-x", "Network getSubtypeName : " + _strSubTypeName);
// TD-SCDMA networkType is 17
int networkType = networkInfo.getSubtype();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
strNetworkType = "2G";
break;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12
case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15
strNetworkType = "3G";
break;
case TelephonyManager.NETWORK_TYPE_LTE: //api<11 : replace by 13
strNetworkType = "4G";
break;
default:
// http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
strNetworkType = "3G";
} else {
strNetworkType = _strSubTypeName;
}
break;
}
}
}
return strNetworkType;
}