本文整理匯總了Java中android.net.NetworkInfo.State方法的典型用法代碼示例。如果您正苦於以下問題:Java NetworkInfo.State方法的具體用法?Java NetworkInfo.State怎麽用?Java NetworkInfo.State使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.net.NetworkInfo
的用法示例。
在下文中一共展示了NetworkInfo.State方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onReceive
import android.net.NetworkInfo; //導入方法依賴的package包/類
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo.State wifiState = null;
NetworkInfo.State mobileState = null;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if (wifiState != null && mobileState != null
&& NetworkInfo.State.CONNECTED != wifiState
&& NetworkInfo.State.CONNECTED == mobileState) {
// Toast.makeText(context, context.getString(R.string.net_mobile), Toast.LENGTH_SHORT).show();
// 手機網絡連接成功
} else if (wifiState != null && mobileState != null
&& NetworkInfo.State.CONNECTED != wifiState
&& NetworkInfo.State.CONNECTED != mobileState) {
// Toast.makeText(context, context.getString(R.string.net_none), Toast.LENGTH_SHORT).show();
// 手機沒有任何的網絡
} else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
// 無線網絡連接成功
// Toast.makeText(context, context.getString(R.string.net_wifi), Toast.LENGTH_SHORT).show();
}
}
示例2: onReceive
import android.net.NetworkInfo; //導入方法依賴的package包/類
@Override
public void onReceive(Context context, Intent intent) {
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 == NetworkInfo.State.CONNECTED;
if (isConnected) {
MySharedPre.getInstance().putIpAddress(Utils.getIpAddress());
} else {
MySharedPre.getInstance().putIpAddress("0.0.0.0");
}
}
}
}
示例3: onReceive
import android.net.NetworkInfo; //導入方法依賴的package包/類
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager=
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager!=null) {
if(noNetWorkNotice == null) noNetWorkNotice = NoNetWorkNotice.getInstance(activity);
NetworkInfo[] networkInfos=connectivityManager.getAllNetworkInfo();
for (NetworkInfo networkInfo : networkInfos) {
NetworkInfo.State state = networkInfo.getState();
if (NetworkInfo.State.CONNECTED == state) {
if(null != noNetWorkNotice && noNetWorkNotice.isShowing()){
noNetWorkNotice.cancel();
}
return;
}
}
if(null != noNetWorkNotice && !noNetWorkNotice.isShowing()){
noNetWorkNotice.show();
}
}
}
示例4: getNetworkState
import android.net.NetworkInfo; //導入方法依賴的package包/類
/**
* 獲取網絡狀態
*
* @param context
* @return
*/
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Wifi
NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
return NETWORN_WIFI;
}
// 3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
return NETWORN_MOBILE;
}
return NETWORN_NONE;
}
示例5: hasState
import android.net.NetworkInfo; //導入方法依賴的package包/類
/**
* Filter, which returns true if at least one given state occurred
*
* @param states NetworkInfo.State, which can have one or more states
* @return true if at least one given state occurred
*/
public static Predicate<Connectivity> hasState(final NetworkInfo.State... states) {
return new Predicate<Connectivity>() {
@Override public boolean test(@NonNull Connectivity connectivity) throws Exception {
for (NetworkInfo.State state : states) {
if (connectivity.getState() == state) {
return true;
}
}
return false;
}
};
}
示例6: getCurrentNetworkState
import android.net.NetworkInfo; //導入方法依賴的package包/類
/**
* 獲取當前網絡的狀態
*
* @param context 上下文
* @return 當前網絡的狀態。具體類型可參照NetworkInfo.State.CONNECTED、NetworkInfo.State.CONNECTED.DISCONNECTED等字段。當前沒有網絡連接時返回null
*/
public static NetworkInfo.State getCurrentNetworkState(Context context) {
NetworkInfo networkInfo
= ((ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
return networkInfo != null ? networkInfo.getState() : null;
}
示例7: isGpsNet
import android.net.NetworkInfo; //導入方法依賴的package包/類
/**
* 檢查網絡是否是GPRS連接
*/
public static boolean isGpsNet(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo.State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if (gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING) {
return true;
}
return false;
}
示例8: hasState
import android.net.NetworkInfo; //導入方法依賴的package包/類
/**
* Determines if any of provided {@link android.net.NetworkInfo.State states} occurred.
*
* @param networkStates one or many {@link android.net.NetworkInfo.State}
*
* @return {@code true} if any of the given network states occurred, {@code false} otherwise
*/
public static Predicate<RxNetworkInfo> hasState(final NetworkInfo.State... networkStates) {
return new Predicate<RxNetworkInfo>() {
@Override
public boolean test(RxNetworkInfo networkInfo) throws Exception {
return Arrays.asList(networkStates).contains(networkInfo.getState());
}
};
}
示例9: getState
import android.net.NetworkInfo; //導入方法依賴的package包/類
public NetworkInfo.State getState() {
return state;
}
示例10: onReceive
import android.net.NetworkInfo; //導入方法依賴的package包/類
@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, "當前沒有網絡連接,請確保你已經打開網絡 ");
}
}
}
示例11: getState
import android.net.NetworkInfo; //導入方法依賴的package包/類
/** @see NetworkInfo#getState() */
public NetworkInfo.State getState() {
return state;
}