本文整理匯總了Java中android.net.NetworkInfo類的典型用法代碼示例。如果您正苦於以下問題:Java NetworkInfo類的具體用法?Java NetworkInfo怎麽用?Java NetworkInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NetworkInfo類屬於android.net包,在下文中一共展示了NetworkInfo類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getNetworkType
import android.net.NetworkInfo; //導入依賴的package包/類
/**
* Get network type
*
* @param context
* @return
*/
public static int getNetworkType(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager)context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
return networkInfo == null ? -1 : networkInfo.getType();
}
示例2: isWifiConnectivityType
import android.net.NetworkInfo; //導入依賴的package包/類
/**
* @brief Checks the connectivity type of the terminal
* @return True if the connectivity type is Wifi, false in another case.
* @details Checks the connectivity type of the terminal
*/
//devuelve true si es conexion wifi, false en caso contrario
private boolean isWifiConnectivityType(){
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
String connectionType = info.getTypeName();
if (connectionType.equalsIgnoreCase("wifi")){
return true;
}
else{
//3g u otro tipo
return false;
}
}
示例3: onReceive
import android.net.NetworkInfo; //導入依賴的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();
}
}
}
示例4: isNetworkAvailable
import android.net.NetworkInfo; //導入依賴的package包/類
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
示例5: setNetworkInfo
import android.net.NetworkInfo; //導入依賴的package包/類
public static void setNetworkInfo(Context context, boolean isConnected, boolean isRoaming, boolean isWifi) {
ShadowConnectivityManager manager =
shadowOf((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo.DetailedState detailedState =
isConnected ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED;
int type = isConnected ? (isWifi ? ConnectivityManager.TYPE_WIFI : ConnectivityManager.TYPE_MOBILE) : -1;
NetworkInfo networkInfo =
ShadowNetworkInfo.newInstance(detailedState, type, 0, isConnected, isConnected, isRoaming);
manager.setActiveNetworkInfo(networkInfo);
}
示例6: isRoaming
import android.net.NetworkInfo; //導入依賴的package包/類
public static boolean isRoaming() {
try {
ConnectivityManager cm = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null) {
return netInfo.isRoaming();
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return false;
}
示例7: onReceive
import android.net.NetworkInfo; //導入依賴的package包/類
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo =
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(networkInfo.isConnected()) {
if(!mIsConnected) {
Log.d(TAG, "Wifi is connected: " + String.valueOf(networkInfo));
mListener.onConnectionStateChanged(true);
mIsConnected = true;
}
} else {
if(mIsConnected) {
Log.d(TAG, "Wifi is disconnected: " + String.valueOf(networkInfo));
mIsConnected = false;
mListener.onConnectionStateChanged(false);
}
}
}
}
示例8: networkChecker
import android.net.NetworkInfo; //導入依賴的package包/類
@Singleton
@Provides
NetworkChecker networkChecker(LoriApp app) {
return () -> {
ConnectivityManager internetManager = (ConnectivityManager) app.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = internetManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable() && networkInfo.isConnectedOrConnecting();
};
}
示例9: getNetworkType
import android.net.NetworkInfo; //導入依賴的package包/類
/**
* 獲取當前網絡狀態類型
*
* @param context
* @return
*/
public static int getNetworkType(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
int type = TYPE_NO;
if (info != null && info.isAvailable()) {
//獲取網絡類型
int netWorkType = info.getType();
if (netWorkType == ConnectivityManager.TYPE_WIFI) {
type = TYPE_WIFI;
} else if (netWorkType == ConnectivityManager.TYPE_MOBILE) {
type = TYPE_MOBILE;
} else {
type = TYPE_UNKNOWN;
}
}
return type;
}
示例10: isNetworkAvailable
import android.net.NetworkInfo; //導入依賴的package包/類
public static boolean isNetworkAvailable() {
boolean status = false;
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null
&& netInfo.getState() == NetworkInfo.State.CONNECTED) {
status = true;
} else {
netInfo = cm.getActiveNetworkInfo();
if (netInfo != null
&& netInfo.getState() == NetworkInfo.State.CONNECTED)
status = true;
}
return status;
}
示例11: getNetworkType
import android.net.NetworkInfo; //導入依賴的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: getNetworkType
import android.net.NetworkInfo; //導入依賴的package包/類
public static int getNetworkType() {
NetworkInfo networkInfo = ((ConnectivityManager) BaseApplication.getInstance().getSystemService("connectivity")).getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isAvailable()) {
return 0;
}
if (1 == networkInfo.getType()) {
return 1;
}
switch (((TelephonyManager) BaseApplication.getInstance().getSystemService("phone")).getNetworkType()) {
case 1:
case 2:
case 4:
return 2;
case 13:
return 3;
default:
return 3;
}
}
示例13: checkPending
import android.net.NetworkInfo; //導入依賴的package包/類
/**
* Checks if there is already a pending request for the user.
*
* @param name of user pending
* @return Boolean value. Whether if there is a pending request for the user.
* @throws Exception
*/
private boolean checkPending(String name) { // change to more specific exception later.
// Check if app is connected to a network.
ConnectivityManager cm = (ConnectivityManager) getActivity().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null == activeNetwork) {
Toast.makeText(getActivity(), "You are offline.", Toast.LENGTH_SHORT).show();
} else {
ElasticSearchUserController.GetUserTask getUserTask =
new ElasticSearchUserController.GetUserTask();
getUserTask.execute(name.toLowerCase());
try {
User user2 = getUserTask.get().get(0); // get first user from result
String currentUsername = CurrentUserSingleton.getInstance().getUser().getName();
return user2.getPending().contains(currentUsername);
} catch (Exception e) {
Log.d("Error", "Unable to get user from elastic search");
}
}
return false;
}
示例14: isTunnelNeeded
import android.net.NetworkInfo; //導入依賴的package包/類
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;
}
示例15: loadnet
import android.net.NetworkInfo; //導入依賴的package包/類
private void loadnet() {
ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE);
//獲取係統的連接服務。
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//獲取網絡的連接情況。
if (networkInfo != null && networkInfo.isAvailable()) {
/* if (networkInfo.getType()==connectivityManager.TYPE_WIFI){
Toast.makeText(A.this,"網絡已啟動啦(WIFI)",Toast.LENGTH_SHORT).show();
}else if (networkInfo.getType()==connectivityManager.TYPE_MOBILE) {
Toast.makeText(A.this,"網絡已啟動啦(3G)",Toast.LENGTH_SHORT).show();
}*/
loadingLayout.setStatus(LoadingLayout.Success);//加載成功
} else {
loadingLayout.setStatus(LoadingLayout.Loading);
}
}