如何知道我的手机使用的互联网连接类型

Posted

技术标签:

【中文标题】如何知道我的手机使用的互联网连接类型【英文标题】:How to know the internet connection type that my mobile uses 【发布时间】:2020-02-01 13:54:18 【问题描述】:

我想知道我的手机使用的互联网连接类型(蜂窝或 wifi)。

Network network;  
manager = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
manager.getActiveNetworkInfo();

if (manager != null)
if (manager.getNetworkCapabilities(network).hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR))
Toast.makeText(this, "Mobile Internet", Toast.LENGTH_SHORT).show();
         
     

一条错误消息说:

(尝试在空对象引用上调用虚拟方法“booleanandroid.net.NetworkCapabilities.hasTransport(int)”)。

【问题讨论】:

签出此***.com/questions/2802472/… 一些方法如(getType) 已被弃用。 查看***.com/questions/32547006/… 【参考方案1】:

试试这个。

String networkStatus ="";
final ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//Check Wifi
final android.net.NetworkInfo wifi = manager.getActiveNetworkInfo();
//Check for mobile data
final android.net.NetworkInfo mobile = manager.getActiveNetworkInfo();

if( wifi.getType() == ConnectivityManager.TYPE_WIFI) 
    networkStatus = "wifi";
else if(mobile.getType() == ConnectivityManager.TYPE_MOBILE)
    networkStatus = "mobileData";
else
    networkStatus="noNetwork";

return networkStatus;

【讨论】:

getType 方法已弃用网络功能。 请检查。 ***.com/questions/52816443/…【参考方案2】:

您可以使用以下代码进行检查:

    private void checkInternetConnectionType() 
        boolean isConnected = false;
        NetworkInfo activeNetwork = null;
        ConnectivityManager cm =
            (ConnectivityManager)  this.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm != null) 
            activeNetwork = cm.getActiveNetworkInfo();
            isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        

        boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
        boolean isMobileData = activeNetwork.getType() ==  ConnectivityManager.TYPE_MOBILE;

向 AndroidManifest.xml 添加以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

另外,请查看以下文档: https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java

https://developer.android.com/reference/android/net/ConnectivityManager.html

【讨论】:

【参考方案3】:

使用下面类的getCurrentNetworkType函数(你可以修改类,只有1个函数,这个类是我项目的一部分):

public class ConnectionManagerK 

private static String TAG = "ConnectionManagerK";
private static ConnectionManagerK Instance;

public static ConnectionManagerK getInstance() 
    ConnectionManagerK localInstance = Instance;
    if (localInstance == null) 
        synchronized (ConnectionManagerK .class) 
            localInstance = Instance;
            if (localInstance == null) 
                Instance = localInstance = new ConnectionManagerK ();
            
        
    
    return localInstance;



public static int getCurrentNetworkType() 
    if (isConnectedOrConnectingToWiFi()) 
        return StatsController.TYPE_WIFI;
     else if (isRoaming()) 
        return StatsController.TYPE_ROAMING;
     else 
        return StatsController.TYPE_MOBILE;
    



public static boolean isNetworkOnline() 
    try 
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isAvailable())) 
            return true;
        
        netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (netInfo != null && netInfo.isConnectedOrConnecting()) 
            return true;
         else 
            netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (netInfo != null && netInfo.isConnectedOrConnecting()) 
                return true;
            
        
     catch (Exception e) 
        FileLog.e(e);
        return true;
    
    return false;


public static boolean isRoaming() 
    try 
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if (netInfo != null) 
            return netInfo.isRoaming();
        
     catch (Exception e) 
        FileLog.e(e);
    
    return false;


public static boolean isConnectedOrConnectingToWiFi() 
    try 
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo.State state = netInfo.getState();
        if (netInfo != null && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING || state == NetworkInfo.State.SUSPENDED)) 
            return true;
        
     catch (Exception e) 
        FileLog.e(e);
    
    return false;


public static boolean isConnectedToWiFi() 
    try 
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) 
            return true;
        
     catch (Exception e) 
        FileLog.e(e);
    
    return false;

【讨论】:

以上是关于如何知道我的手机使用的互联网连接类型的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 Android 应用程序中使用 3G 互联网连接?

如何在没有任何互联网连接的情况下安装 APK?

为啥我手机wifi已连接,但无法访问互联网?

手机处于飞行模式时如何检测 iCloud 用户?

没有互联网连接时使用 FCM 发送推送通知

如何快速将互联网连接类型发送到服务器:Alamofire [关闭]