android CMWAP CMNET

android CMWAP CMNET

CMWAP和CMNET是中国移动为其划分的两个GPRS接入方式。

CMWAP
CMWAP接入时只能访问GPRS网络内的IP(10...*)网络, 而无法通过路由访问Internet,
CMWAP浏览Internet是通过WAP网关协议或它提供的HTTP代理服务实现。
CMWAP需要设置代理才能连接Internet,接入方式下正常工作需满足:
1、应用程序的网络请求基于HTTP协议。
2、应用程序支持HTTP代理协议或WAP网关协议。

CMNET
可以用GPRS访问Internet

//———–cmwap———–

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.172",80));
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection(proxy);

NetworkInfo方法
getDetailedState():获取详细状态。
getExtraInfo():获取附加信息。
getReason():获取连接失败的原因。
getType():获取网络类型(一般为MOBILE或WIFI)。
getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
isAvailable():判断该网络是否可用。
isConnected():判断是否已经连接。
isConnectedOrConnecting():判断是否已经连接或正在连接。
isFailover():判断是否连接失败。
isRoaming():判断是否漫游

//移动CMNET方式
getExtraInfo 的值是cmnet
//移动CMWAP方式
getExtraInfo 的值是cmwap 需在代理的情况下访问普通的网站)

//联通3gwap方式
getExtraInfo 的值是3gwap
//联通3gnet方式
getExtraInfo 的值是3gnet
//联通uniwap方式
getExtraInfo 的值是uniwap
//联通uninet方式
getExtraInfo 的值是uninet

public static int getAPNType(Context context){ 
    private static final int CMNET=1; 
    private static final int CMWAP=2; 
    private static final int WIFI=3;
    int netType = -1;  

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    if(networkInfo==null){ 
       return netType; 

    } 
    int nType = networkInfo.getType(); 
    if(nType==ConnectivityManager.TYPE_MOBILE){ 
       Log.d("getAPNType", "networkInfo.getExtraInfo() =  "+networkInfo.getExtraInfo()); 

       if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) { 
          netType = CMNET; 
       } else{ 
          netType = CMWAP; 
       } 
    } 
    else if(nType==ConnectivityManager.TYPE_WIFI){ 
       netType = WIFI; 
    } 
    return netType; 
} 

Uri PREFERRED_APN_URI, APN_TABLE_URI;
String APN_Id; 

//获取当前APN(access point name)属性 
private boolean getCurrentAPN(){ 
    PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); 
    Cursor cursor_current = this.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); 
    if(cursor_current != null && cursor_current.moveToFirst()){ 
       String proxy = cursor_current.getString(cursor_current.getColumnIndex("proxy")); 
       String apn = cursor_current.getString(cursor_current.getColumnIndex("apn")); 
       String port = cursor_current.getString(cursor_current.getColumnIndex("port")); 
       String current = cursor_current.getString(cursor_current.getColumnIndex("current")); 
       if(proxy == null || apn == null || port == null || current == null || proxy.equals("") || port.equals("")){ 
          return false; 
       } 
       if((proxy.equals("10.0.0.172") || proxy.equals("010.000.000.172")) && port.equals("80") && apn.equals("cmwap") && current.equals("1")){ 
          return true; 
       } 
    } 
    return false;           
} 

//检查是否存在cmwap网络 
private boolean checkHasWapAPN(){ 
    APN_TABLE_URI = Uri.parse("content://telephony/carriers"); 
    Cursor cursor_need = this.getContentResolver().query(APN_TABLE_URI, null, null, null, null); 

    while(cursor_need != null && cursor_need.moveToNext()) { 
        String id = cursor_need.getString(cursor_need.getColumnIndex("_id"));      
        String port = cursor_need.getString(cursor_need.getColumnIndex("port"));  
        String proxy = cursor_need.getString(cursor_need.getColumnIndex("proxy")); 
        String current = cursor_need.getString(cursor_need.getColumnIndex("current")); 
        String mmsc = cursor_need.getString(cursor_need.getColumnIndex("mmsc")); 
        if(proxy == null || port == null || current == null){ 
            continue; 
        } 
        if((proxy.equals("10.0.0.172") || proxy.equals("010.000.000.172")) && port.equals("80") && current.equals("1") && mmsc == null){ 
            APN_Id = id; 
            return true; 
        } 
    } 
    return false;          
} 

//设置为cmwap网络 
public boolean setAPN(int id){                           
    boolean res = false; 
    ContentResolver resolver = this.getContentResolver(); 
    ContentValues values = new ContentValues(); 
    values.put("apn_id", id); 
    try{ 
       resolver.update(PREFERRED_APN_URI, values, null, null); 
       Cursor c = resolver.query(PREFERRED_APN_URI, new String[]{"name", "apn"}, "_id=" + id, null, null); 
       if(c != null){ 
          res = true; 
          c.close(); 
       } 
    }catch(SQLException e){ 
       e.printStackTrace();
    } 
    return res; 
}

//添加cmwap网络 
private int addCmwapAPN(){ 
    ContentResolver cr = this.getContentResolver(); 
    ContentValues cv = new ContentValues(); 
    cv.put("name", "cmwap"); 
    cv.put("apn", "cmwap"); 
    cv.put("proxy", "010.000.000.172"); 
    cv.put("port", "80"); 
    cv.put("current", 1); 

    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
    String imsi =tm.getSubscriberId();  //取出IMSI
/*
getSubscriberId()
Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
getSimSerialNumber()
Returns the serial number of the SIM, if applicable.
getLine1Number()
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone.
getDeviceId()
Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.


String imei = tm.getDeviceId();         //取出IMEI
String tel  = tm.getLine1Number();      //取出MSISDN,很可能为空
String imei = tm.getSimSerialNumber();  //取出ICCID
String imsi = tm.getSubscriberId();     //取出IMSI

IMSI:international mobiles subscriber identity国际移动用户号码标识,GSM必须写在卡内相关文件中; 
MSISDN: mobile subscriber ISDN用户号码,这个是我们说的139,136那个号码; 
ICCID: ICC identity集成电路卡标识,这个是唯一标识一张卡片物理号码的; 
IMEI:international mobile Equipment identity手机唯一标识码;
*/
    if(imsi != null){ 
       if(imsi.startsWith("46000")){ 
           cv.put("numeric", "46000"); 
           cv.put("mcc", "460"); 
           cv.put("mnc", "00"); 
       } else if(imsi.startsWith("46002")){ 
           cv.put("numeric", "46002"); 
           cv.put("mcc", "460"); 
           cv.put("mnc", "02"); 
       } 
    } 

    Cursor c = null; 
    try{ 
       Uri newRow = cr.insert(APN_TABLE_URI, cv); 
       if(newRow != null){ 
          c = cr.query(newRow, null, null, null, null); 
          c.moveToFirst(); 
          String id = c.getString(c.getColumnIndex("_id")); 
          setAPN(Integer.parseInt(id)); 
          return Integer.parseInt(id); 
       }             
    }catch(SQLException e){ 
       e.printStackTrace();
    } 
    finally{ 
       if(c != null){ 
          c.close(); 
       } 
    }        
    return 0;           
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值