在Android上可以同时使用WiFi和3G。Android系统支持多种无线通信方式,包括Wi-Fi、3G、4G和5G。在Android设备上,可以使用WiFi网络进行通信,也可以使用3G/4G/5G网络进行通信。
要同时使用WiFi和3G,需要在Android应用中集成相应的通信API,如ConnectivityManager
和Network
类。ConnectivityManager
可以获取当前连接的WiFi和蜂窝网络信息,Network
类可以获取网络连接状态。
以下是一个简单的示例代码,演示如何使用ConnectivityManager
和Network
类检测网络连接状态:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.NetworkState;
import android.os.StatFs;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class NetworkUtil {
private static final String TAG = "NetworkUtil";
private static final int NETWORK_REQUEST_CODE = 1234;
private static final int NETWORK_TIMEOUT = 10000;
public static boolean isConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
public static boolean isConnectedWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}
public static boolean isConnectedMobile(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting() && activeNetwork.getType() != ConnectivityManager.TYPE_WIFI;
}
public static String getNetworkType(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null) {
return "";
}
String type = activeNetwork.getTypeName();
return type;
}
public static String getNetworkTypeName(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null) {
return "";
}
String type = activeNetwork.getTypeName();
return type;
}
public static String getNetworkTypeDisplayName(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null) {
return "";
}
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
String type = networkCapabilities.getTransportName();
return type;
}
public static boolean isNetworkTypeValid(Context context, String networkType) {
if (TextUtils.isEmpty(networkType)) {
return false;
}
NetworkInfo activeNetwork = getActiveNetwork(context);
if (activeNetwork == null || activeNetwork.getType() == ConnectivityManager.TYPE_NONE) {
return false;
}
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
if (!networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return false;
}
return networkCapabilities.hasTransport(networkType);
}
public static NetworkInfo getActiveNetwork(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network
领取专属 10元无门槛券
手把手带您无忧上云