android 手机接入点设置与网络状态检查(转自:eggic.com)

  android 手机接入点设置与网络状态检查(转自:eggic.com)

时间:2011-10-04 01:02 来源:未知 作者:vsyour 点击: 57 次

android 手机接入点设置与网络状态检查 手机上网分为wap和net两种方式,使用net手机就会直接连入互联网,而使用wap则会中间多了一个代理 网关,移动联通均是10.0.0.172,端口80 net与wap两

android 手机接入点设置与网络状态检查


手机上网分为wap和net两种方式,使用net手机就会直接连入互联网,而使用wap则会中间多了一个代理
网关,移动联通均是10.0.0.172,端口80
net与wap两种方式在网络连接部分代码很不一样
例:站址www.eggic.com
net方式:
URL url = new URL("http://www.eggic.com");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
wap方式:
URL url = new URL("http://10.0.0.172:80/index.htm");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setRequestProperty("X-Online-Host", "www.eggic.com");
因此,编写程序时就要检测当前的APN类型,判断是wap还是net方式、修改当前的APN.
获取apn信息主要是通过ContentResolver通过指定的uri去查询
content://telephony/carriers  是手机中获取所有apn的uri
content://telephony/carriers/preferapn   是手机默认调用的apn的uri
通过代码:Uri uri = Uri.parse("content://telephony/carriers");   
         Cursor cr = getContentResolver().query(uri, null, null, null, null);   
可以得到当前所有apn信息的游标,遍历返回的游标便可以查看对应apn的id name等具体信息:
while(cr!=null && cr.moveToNext()){   
    String id = cr.getString(cr.getColumnIndex("_id"));   
    String apn = cr.getString(cr.getColumnIndex("name"));  
    String apn = cr.getString(cr.getColumnIndex("apn"));  
    String proxy = cr.getString(cr.getColumnIndex("proxy"));  
}  
(注:里面的 _id 、name apn等都是系统存储apn的数据库中的字段。系统把所有的apn都保存在数据库中,数据库在:/data/data/com.android.providers.telephony/databases/telephony.db)
其中如需要知道该apn是wap 还是 net的话只需要判断proxy字段是否是10.0.0.172即可
开发人员可以根据Uri uri = Uri.parse("content://telephony/carriers/preferapn");得到当前选定的
apn来判断是否符合本程序规定的方式,如果不符合,可以通过以下代码加以设定:
//新增一个3GWap接入点
       {
                ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", "3gwap");
                values.put("apn", "3gwap");
                values.put("mcc", "460");
                values.put("mnc", "01");
                values.put("numeric", "46001");
                Cursor c = null;
                Uri newRow = resolver.insert(APN_URI, values);
                if (newRow != null) {
                        c = resolver.query(newRow, null, null, null, null);
                        int idIndex = c.getColumnIndex("_id");
                        c.moveToFirst();
                        id = c.getShort(idIndex);
                }
                if (c != null)
                        c.close();
          }
   //设置接入点
{
        ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("apn_id", id);
                resolver.update(CURRENT_APN_URI, values, null, null);
}
(注:需要先在xml中申明操作apn的权限
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>)
(责任编辑:admin)

原贴来自: http://eggic.com/article/2011/1003/4.html (转载注明)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vsyour

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值