简体   繁体   English

如何知道我的 android 设备是否已连接到 VoWIFI(Wifi 语音)

[英]How to know if my android device is connected to VoWIFI (Voice over Wifi)

I'm developing an android application to get some information about the network.我正在开发一个 android 应用程序来获取有关网络的一些信息。 I use to work with TelephonyManager to get the network type where my device is connected to.我过去常常使用 TelephonyManager 来获取我的设备连接到的网络类型。 I succed to get those information for 2G, 3G, 4G.我成功获得了 2G、3G、4G 的这些信息。 Now I want to know if I'm connected to VoWIFI.现在我想知道我是否已连接到 VoWIFI。

I've tried to look at the android documentation: https://developer.android.com/reference/android/net/wifi/WifiInfo.html to find out where to get this information but I wasn't able to find it.我试图查看 android 文档: https : //developer.android.com/reference/android/net/wifi/WifiInfo.html以找出从何处获取此信息,但我无法找到它。

What I did for 2G, 3G, 4G:我为 2G、3G、4G 做了什么:

 public String getNetworkClass() {
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        int networkType = mTelephonyManager.getNetworkType();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "4G";
            default:
                return "Unknown";
        }
    }

You can detect if you application is connected to the internet like this您可以检测您的应用程序是否像这样连接到互联网

  public class Utility {
  public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  }
  }

And then you call it in any activity like this然后你在任何这样的活动中调用它

  Utility.isNetworkAvailable(AnyActivity.this);

Add those permission in your Manifest file在您的清单文件中添加这些权限

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

Another fancy way is to create a BroadcastReceiver that listens to your connctivity status.另一种奇特的方法是创建一个 BroadcastReceiver 来监听您的连接状态。

If you want to explore this approach more I can write an example for you.如果你想更多地探索这种方法,我可以为你写一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM