Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155963
  • 博文数量: 47
  • 博客积分: 2318
  • 博客等级: 大尉
  • 技术积分: 471
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-25 12:09
文章分类

全部博文(47)

文章存档

2012年(17)

2011年(28)

2010年(2)

分类: LINUX

2011-10-05 15:35:09

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两种方式在网络连接部分代码很不一样
例:站址
net方式:
URL url = new URL("");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();

wap方式:
URL url = new URL("");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setRequestProperty("X-Online-Host", "");
因此,编写程序时就要检测当前的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的权限
(责任编辑:admin)
原贴来自: (转载注明)
阅读(4660) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

vsyour2011-10-07 00:46:21

黄学强: 我想问一下,为什么我的手机的接入点设置为WAP而有些应用软件就无法联网了呢?.....
某些软件只适应cmnet?

黄学强2011-10-05 23:14:33

我想问一下,为什么我的手机的接入点设置为WAP而有些应用软件就无法联网了呢?