您的位置:首页 > 理论基础 > 计算机网络

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

2011-10-05 15:43 609 查看
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 (转载注明)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: