Android进阶篇-判断3G/WIFI/WAP

手机上网的方式主要是GRPS/WIFI/WAP三种形式。

有的时候,我们需要获取到当前上网的形式。

这就需要使用到下面这个函数:

    /**
     * 判断联网状态及联网方式
     * @param context 当前应用上下文
     * @return NO_NETWORK 无可用网路; WIFI 通过wifi方式联网; GRPS 通过GPRS方式联网 
     * */
    public static int theWayOfNetwork(Context context){
        ConnectivityManager conn = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        
        if(conn.getActiveNetworkInfo() == null || (!conn.getActiveNetworkInfo().isAvailable())){
            return HttpUtil.NO_NETWORK;
        }
        if(conn.getNetworkInfo(1).getState() ==  NetworkInfo.State.CONNECTED){
            return HttpUtil.WIFI;
        }
        if(conn.getNetworkInfo(0).getState() ==  NetworkInfo.State.CONNECTED){
            String proxyHost = android.net.Proxy.getDefaultHost();
            if (proxyHost != null && !proxyHost.equals("")) {
                // WAP方式
                return HttpUtil.WAP;
            }
            return HttpUtil.GPRS;
        }
        return HttpUtil.NO_NETWORK;
    }    

读取网络状态权限:

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

当当前无网络状态时,跳转进入网络设置界面

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

判断一串String是否是电话号码:

    /**是否是电话号码
     * @param mobile
     * @return
     * 2012-6-4
     */
    public static boolean isPhoneNum(String mobile) {
        mobile = mobile.replaceAll("-", "");
        if (mobile == null || "".equals(mobile)) {
            return false;
        }
        /**如果有+86开头,则去掉+86*/
        if(mobile.startsWith("+86")){ 
          mobile = mobile.substring(3, mobile.length());
         } 
        /**是否完整的手机号码字数:11位*/
        if (!mobile.startsWith("1") || mobile.length() != 11) {
            return false;
        }
        List<String> chinaMobile  = Arrays.asList(new String[] {"135","134","136","137","138","182","187","186","188","182","159","158","157","152","151","150","139"}) ; 
        List<String> chinaUnicom = Arrays.asList(new String[] {"130","131","132","156","155","185","186"}) ; 
        List<String> chinaHicdma = Arrays.asList(new String[] {"133","153","189","180"}) ;
        boolean bolChinaUnicom  = (chinaUnicom.contains(mobile.substring(0,3))) ; 
        boolean bolChinaMobile1 = (chinaMobile.contains(mobile.substring(0,3))) ; 
        boolean bolChinaMobile2 = (chinaHicdma.contains(mobile.substring(0,3))) ;
        
        if (bolChinaUnicom || bolChinaMobile1 || bolChinaMobile2)  {
             return true  ;
        } else {
            return false;
        }
    }

 

posted @ 2012-05-10 16:13  暗殇  阅读(353)  评论(0编辑  收藏  举报