APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务)、CMNET(除了WAP以外的服务目前都用CMNET,比如连接因特网等)。你 在通过手机上网时必须配置的一个参数,它决定了你的手机通过哪种接入方式来访问网络。有的中国移动的业务需要走CMWAP接入点才能够连接网络的,在做这类应用的时候,不可避免地需要判断当前APN, 切换APN,以及成功连接到网络后连接到服务器。
由于Android对于APN的网络API没有公开,不过可以通过对数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
首先要声明相关的Uri:
- Java 代码复制内容到剪贴板
-
-
Uri uri = Uri.parse("content://telephony/carriers");
-
public static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
判断是否存在可用的网络连接:
- Java 代码复制内容到剪贴板
-
-
-
-
-
-
-
public boolean isWIFIAvailable() {
-
for (int i = 0; i < 5; i++) {
- ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo wifiInfo = cwjManager.getActiveNetworkInfo();
-
if (wifiInfo == null) {
-
try {
-
Thread.sleep(1000);
-
} catch (InterruptedException e) {
- e.printStackTrace();
- }
-
continue;
-
} else {
-
if (wifiInfo.getType() == ConnectivityManager.TYPE_WIFI)
-
return true;
- }
- }
-
return false;
- }
-
-
-
-
-
-
public boolean isNetWorkAvailable() {
-
for (int i = 0; i < 5; i++) {
- ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = cwjManager.getActiveNetworkInfo();
-
if (netInfo == null) {
-
try {
-
Thread.sleep(1000);
-
} catch (InterruptedException e) {
- e.printStackTrace();
- }
-
continue;
-
} else {
-
if (netInfo.isAvailable()) {
-
return true;
-
} else
-
continue;
- }
- }
-
return false;
- }
获取MMC、MNC、SimOperator的相关方法(设置APN的时候都有这样的选项):
- Java 代码复制内容到剪贴板
-
-
public String getMCC() {
- TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- String numeric = tm.getSimOperator();
-
String mcc = numeric.substring(0, 3);
-
return mcc;
- }
-
-
public String getMNC() {
- TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- String numeric = tm.getSimOperator();
-
String mnc = numeric.substring(3, numeric.length());
-
return mnc;
- }
-
-
public String getSimOperator() {
- TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- String SimOperator = tm.getSimOperator();
-
return SimOperator;
- }
新建一个APN:
- Java 代码复制内容到剪贴板
-
-
public String bulidAPN() {
- ContentResolver resolver = getContentResolver();
-
ContentValues values = new ContentValues();
-
values.put("name", "AndroidNet_APN");
-
values.put("apn", "#777");
-
values.put("user", "ctnet@mycdma.cn");
-
values.put("password", "vnet.mobi");
-
values.put("mcc", getMCC());
-
values.put("mnc", getMNC());
-
values.put("numeric", getSimOperator());
-
Cursor c = null;
-
String defaultAPNId = "";
-
try {
- Uri newRow = resolver.insert(uri, values);
-
if (newRow != null) {
-
c = resolver.query(newRow, null, null, null, null);
-
int idindex = c.getColumnIndex("_id");
- c.moveToFirst();
- defaultAPNId = c.getString(idindex);
- }
-
} catch (SQLException e) {
-
defaultAPNId = "";
- }
-
if (c != null)
- c.close();
-
return defaultAPNId;
- }
通过判断,获取一个可用的网络连接:
- Java 代码复制内容到剪贴板
-
-
-
-
-
-
-
public HttpURLConnection getConnection(URL url) {
-
- String currentAPNId;
-
- String currentAPNUser;
-
- String currentAPNPassword;
-
HttpURLConnection conn = null;
-
-
if (isWIFIAvailable()) {
-
try {
- conn = (HttpURLConnection) url.openConnection();
-
-
conn.setConnectTimeout(100000);
-
conn.setReadTimeout(100000);
-
} catch (IOException e) {
- e.printStackTrace();
-
conn = null;
-
return conn;
- }
-
return conn;
- }
-
- Cursor mCursor_APN = getContentResolver().query(PREFERRED_APN_URI,
-
null, null, null, null);
-
if (mCursor_APN == null || !mCursor_APN.moveToNext()) {
-
return null;
-
} else {
- currentAPNId = mCursor_APN.getString(mCursor_APN
-
.getColumnIndex("_id"));
- currentAPNUser = mCursor_APN.getString(mCursor_APN
-
.getColumnIndex("user"));
- currentAPNPassword = mCursor_APN.getString(mCursor_APN
-
.getColumnIndex("password"));
- mCursor_APN.close();
- }
-
-
if (!(currentAPNUser.equals("ctnet@mycdma.cn")
-
&& currentAPNPassword.equals("vnet.mobi") || currentAPNUser
-
.equals("ctwap@mycdma.cn")
-
&& currentAPNPassword.equals("vnet.mobi"))) {
-
-
Cursor mCursor_APN_Defalut = getContentResolver().query(uri, null,
-
" user=? and password=?",
-
new String[] { "ctnet@mycdma.cn", "vnet.mobi" }, null);
-
if (mCursor_APN_Defalut == null
- || !mCursor_APN_Defalut.moveToNext()) {
-
-
mCursor_APN_Defalut.close();
- String DefalutAPNId = bulidAPN();
-
if (DefalutAPNId == null || DefalutAPNId.equals("")
-
|| DefalutAPNId.equals(" "))
- {
-
return null;
- }
- ContentResolver resolver = getContentResolver();
-
ContentValues values = new ContentValues();
-
values.put("apn_id", DefalutAPNId);
-
resolver.update(PREFERRED_APN_URI, values, null, null);
-
} else {
- String DefalutAPNId = mCursor_APN_Defalut
-
.getString(mCursor_APN_Defalut.getColumnIndex("_id"));
- ContentResolver resolver = getContentResolver();
-
ContentValues values = new ContentValues();
-
values.put("apn_id", DefalutAPNId);
-
resolver.update(PREFERRED_APN_URI, values, null, null);
- mCursor_APN_Defalut.close();
- }
-
try {
- Thread.sleep(8000L);
-
} catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
-
-
if ((android.net.Proxy.getDefaultHost()) != null) {
-
try {
-
java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
-
new InetSocketAddress(
- android.net.Proxy.getDefaultHost(),
- android.net.Proxy.getDefaultPort()));
- conn = (HttpURLConnection) url.openConnection(p);
-
conn.setConnectTimeout(100000);
-
conn.setReadTimeout(100000);
-
} catch (Exception e) {
-
conn = null;
- }
-
return conn;
- }
-
-
if ((android.net.Proxy.getDefaultHost()) == null) {
-
try {
- conn = (HttpURLConnection) url.openConnection();
-
conn.setConnectTimeout(100000);
-
Log.v("http", "nohttpdaili");
-
} catch (Exception e) {
-
conn = null;
- }
-
return conn;
- }
-
return conn;
-
- }
本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962791,如需转载请自行联系原作者