什么是检查是否有可用的移动网络(没有数据连接)的正确方法 [英] What is the correct way of checking for mobile network available (no data connection)

查看:148
本文介绍了什么是检查是否有可用的移动网络(没有数据连接)的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是检查是否移动网络(GSM)连接,可在Android上的正确方法是什么? (> 2.1)我不想检查是否存在通过移动网络数据连接可用只是为您在一般的网络可用性。 (检查是否在移动网络电话是可能的)

目前我使用下面的检查:

 公共静态布尔checkMobileNetworkAvailable(上下文的背景下){
    TelephonyManager TM =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    INT NETWORKTYPE = tm.getNetworkType();
    返回(NETWORKTYPE = TelephonyManager.NETWORK_TYPE_UNKNOWN!);
}
 

但似乎有些设备始终报到NETWORK_TYPE_UNKNOWN。因此,检查失败所有的时间。

有没有这样做的更好的方法?

更新:

请问以下方法更好呢?

 公共静态布尔checkMobileNetworkAvailable(上下文的背景下){
       布尔isMobileNetworkAvailable = FALSE;
       ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
       的NetworkInfo []网络= cm.getAllNetworkInfo();
       的for(int i = 0; I< networks.length;我++){
        如果(网络[我] .getType()== ConnectivityManager.TYPE_MOBILE){
              如果(网络[I] .isAvailable()){
                isMobileNetworkAvailable = TRUE;
              }
            }
        }

    返回isMobileNetworkAvailable;
}
 

解决方案

对于我来说工作正常PhoneStateListener:

 私有静态类YourListener扩展PhoneStateListener {
    @覆盖
    公共无效onServiceStateChanged(ServiceStateserviceState){
        如果(serviceState.getState()== ServiceState.STATE_IN_SERVICE){
             //连接到蜂窝网络
        }
    }
}
 

和注册在TelephonyManager:

  TelephonyManager TM =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
YourListener监听器=新YourListener();
tm.li​​sten(监听器,PhoneStateListener.LISTEN_SERVICE_STATE);
 

目前登记的电话经理调用侦听器对象的onServiceChanged()方法,并传递当前状态。

PS:PhoneStateListener使用处理器发布结果的UI线程,所以你应该在UI线程创建

What is the correct way of checking if a mobile network (GSM) connection is available on Android? (>2.1) I don't want to check if there is data connection available over the mobile network just check for network availability in general. (check if phone calls over the mobile network are possible)

At the moment I am using the following check:

public static boolean checkMobileNetworkAvailable(Context context){
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = tm.getNetworkType();
    return (networkType != TelephonyManager.NETWORK_TYPE_UNKNOWN);
}

But it seems that some devices always report back "NETWORK_TYPE_UNKNOWN". So the check fails all the time.

Is there a better approach of doing it?

Update:

Would the following approach be better?

    public static boolean checkMobileNetworkAvailable(Context context){
       boolean isMobileNetworkAvailable = false;
       ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo[] networks = cm.getAllNetworkInfo();
       for(int i=0;i<networks.length;i++){
        if(networks[i].getType() == ConnectivityManager.TYPE_MOBILE){
              if(networks[i].isAvailable()){
                isMobileNetworkAvailable = true;
              }
            }
        }

    return isMobileNetworkAvailable;
}

解决方案

For me works fine PhoneStateListener:

private static class YourListener extends PhoneStateListener {
    @Override
    public void onServiceStateChanged(ServiceStateserviceState){            
        if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
             // connected to cellular network
        }
    }       
}

And register in TelephonyManager:

TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
YourListener listener = new YourListener();
tm.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);

At registration the telephony manager invokes the onServiceChanged() method on the listener object and passes the current state.

PS: PhoneStateListener uses Handler to post result to UI thread, so you should create it in UI thread.

这篇关于什么是检查是否有可用的移动网络(没有数据连接)的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆