本文整理匯總了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;
}