android网络操作(wifi,apn)

开关wifi

检查网络是否连通

检查接入点类型,检查网络类型

权限


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



    public static final byte NET_TYPE_UNCONNECT = -1;
    public static final byte NET_TYPE_WAP = 0;
    public static final byte NET_TYPE_NET = 1;
    public static final byte NET_TYPE_WIFI = 2;

    public static final byte SIM_TYPE_NOCARD = -1;
    public static final byte SIM_TYPE_CM = 0;
    public static final byte SIM_TYPE_UNI = 1;
    public static final byte SIM_TYPE_3G = 2;
    public static final byte SIM_TYPE_CT = 3;
     
    public static final Uri PREFERRED_APN_URI = Uri
            .parse("content://telephony/carriers/preferapn");
    public static final Uri APN_TABLE_URI = Uri
            .parse("content://telephony/carriers");
 


/**

     * 查看wifi 状态
     * @param context
     * @return
     */
    public static int getWifiState(Context context){
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if(wm !=null ){
            return wm.getWifiState();
        }else{
            return -1;
        }
    }
    
    /**
     * 关闭wifi
     * @param context
     */
    public static void switchWifi(Context context,boolean switch){
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if(wm !=null ){
            Log.d("wifi", "wifi 操作"+switch);
            wm.setWifiEnabled(switch);
        }
    }
    
    /**
     * 判断网络是否连通
     *
     * @param ctx
     * @return
     */
    public static boolean isNetworkAvailable(Context ctx) {
        ConnectivityManager cm = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null)
            Log.i("net", info.toString());
        return (info != null && info.isAvailable());

    }



    /**获得当前sim卡 运营商类型
     *
     * @return
     */
    public int getSimType(){
        int type =SIM_TYPE_NOCARD;
        TelephonyManager phoneManager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
       if (phoneManager != null) {
           String imsi = phoneManager.getSubscriberId();
           if(imsi!=null){
                   if(imsi.startsWith("46000") || imsi.startsWith("46002")){
                   //中国移动
                       type = SIM_TYPE_CM;
                   }else if(imsi.startsWith("46001")){
                   //中国联通
                       type = SIM_TYPE_UNI;
                   }else if(imsi.startsWith("46003")){
                   //中国电信
                       type = SIM_TYPE_CT;
                   }
           }
       }
       return type;
    }
   

    /**
     * 获得当前联网类型WIFI OR CMNET OR CMWAP
     *
     * @return
     */
    public int getNetWorkType() {
        int type = NET_TYPE_UNCONNECT;
        ConnectivityManager m_ConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (m_ConnectivityManager != null) {
            NetworkInfo networkInfo = m_ConnectivityManager
                    .getActiveNetworkInfo();
            if (networkInfo != null) {
                switch (networkInfo.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    type= NET_TYPE_WIFI;
                    break;
                case ConnectivityManager.TYPE_MOBILE:
                    String apn = networkInfo.getExtraInfo();
                    if (apn != null && apn.indexOf("wap") != -1) {
                        type= NET_TYPE_WAP;
                        break;
                    }else{                        
                        type= NET_TYPE_NET;
                        break;
                    }
                }

            }
        }
        return type;
    }


    /**
     * 根据apn名字获取APN ID
     *
     * @param name
     * @return
     */
    public int getApnId(String apnName) {
        String tag = "getApnId";
        String projection[] = { "_id,apn,type,current,mnc" };// current不为空表示可以使用的APN, current为1的才会显示
        int id = -1;
        Cursor cr = null;
        String apn = null;
        String current = null;
        try {
            cr = resolver.query(APN_TABLE_URI, projection, null, null, null);
            if (cr != null) {
                cr.moveToLast();
                do {
                    apn = cr.getString(cr.getColumnIndex("apn"));
                    current = cr.getString(cr.getColumnIndex("current"));
                    if (apn != null && current != null && "1".equals(current)
                            && apn.equals(apnName)
                    // && mnc.equals("02")
                    ){
                        id = cr.getInt((cr.getColumnIndex("_id")));
                        break;
                    }
                } while (cr.moveToPrevious());
                cr.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.toString());
        } finally {
            if (cr != null)
                cr.close();
        }
        return id;
    }


    /**
     * 将选中的接入点设置为当前接入点
     */
    public boolean SetDefaultAPN(int id) {
        boolean res = false;
        ContentValues values = new ContentValues();
        values.put("apn_id", id);
        Cursor c = null;
        try {
            resolver.update(PREFERRED_APN_URI, values, null, null);
            c = resolver.query(PREFERRED_APN_URI,
                    new String[] { "name", "apn" }, "_id=" + id, null, null);
            if (c != null) {
                res = true;
                c.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.toString());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return res;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值