android APN cmnet切换cmwap

测试的时候,发现socket一直断,但是我用的是2G卡的,到底是什么原因引起的呢?经过切换APN,发现竟然可以了,移动的太TMD坑爹了

以下是从其他地方搬来的,调用接口

getCurrentApn() ;就OK

public static final Uri APN_TABLE_URI = Uri
 .parse("content://telephony/carriers");// 所有的APN配配置信息位置
 public static final Uri PREFERRED_APN_URI = Uri
 .parse("content://telephony/carriers/preferapn");// 当前的APN
 Cursor cursor_current;
 Cursor cursor_need;
 int newCreateAPNSet_Id;
 private ArrayList<HashMap<String, String>> cmwapArrayList = new ArrayList<HashMap<String, String>>();

    //获取当前的APN类型
 private void getCurrentApn() {
 if (!checkWapAPN()) {// 检查当前所有接入点中是否存在cmwap接入点,如果不存在任何cmwap接入点,则创建一个新的
 // 创建一个新的cmwap接入点并返回该新创建的接入点的id值
 Log.w("Log", "创建新的APN-------------------");
 newCreateAPNSet_Id = addCmwapAPN();
 Log.d("chenjianli", "info: newCreateAPNSet_Id is "
 + newCreateAPNSet_Id);
 boolean fResult = SetDefaultAPN(newCreateAPNSet_Id);// 设置该新创建的接入点为当前接入点
 Log.d("chenjianli", "info: fResult = " + fResult);// 立刻查询fResult的值为false还是true来判断当前接入点是否改变是没有意义的,因为手机改变当前接入点需要一段时间
 Log
 .d(
 "chenjianli",
 "info: current apn set is cmnet,and not have cmwap APN,we create new cmwap APN and then set it to mobile.");
 } else {
 Log.w("Log", "选择现存的APN=-----------------");
 // 从所有满足条件的接入点中选择合适的接入点并设置
 for (int i = 0; i != cmwapArrayList.size(); i++) {
 String type = cmwapArrayList.get(i).get("type");
 String proxy = cmwapArrayList.get(i).get("proxy");
 String port = cmwapArrayList.get(i).get("port");
 if (!type.equals("mms")
 && (proxy.equals("10.0.0.172") || proxy
 .equals("010.000.000.172"))
 && port.equals("80")) {
 String id = cmwapArrayList.get(i).get("_id");
 SetDefaultAPN(Integer.parseInt(id));
 Log
 .d(
 "chenjianli",
 "info: current apn set is cmwap,and have cmwap set that we need,do not need to create new cmwap APN,just changed .");
 return;
 }
 }
 }
 }


 public boolean SetDefaultAPN(int id) {
 boolean res = false;
 ContentResolver resolver = client.getService().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) {
 // Log.d(TAG, "-----" + e.getMessage());
 }
 return res;
 }


 // 设置为cmwap网络
 public boolean setAPN(int id) {


 // 如果wifi是打开的,则关闭


 boolean res = false;
 ContentResolver resolver = client.getService().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) {
 Log.e("lhl", e.getMessage());
 }
 return res;
 }


 // 添加cmwap网络
 TelephonyManager tm;


 private int addCmwapAPN() {
 ContentResolver cr = client.getService().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);


 tm = (TelephonyManager) client.getService().getSystemService(
 Context.TELEPHONY_SERVICE);
 String imsi = tm.getSubscriberId();
 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) {
 Log.e("lhl", e.getMessage());
 } finally {
 if (c != null) {
 c.close();
 }
 }
 return 0;
 }


 private boolean checkWapAPN() {
 Cursor cursor_need = client.getService().getContentResolver().query(
 APN_TABLE_URI, null, "apn = \'cmwap\' and current = 1", null,
 null);
 if (cursor_need != null) {
 while (cursor_need.moveToNext()) {
 HashMap<String, String> map = new HashMap<String, String>();
 map.put("type", cursor_need.getString(cursor_need
 .getColumnIndex("type")));
 map.put("_id", cursor_need.getString(cursor_need
 .getColumnIndex("_id")));
 map.put("proxy", cursor_need.getString(cursor_need
 .getColumnIndex("proxy")));
 map.put("port", cursor_need.getString(cursor_need
 .getColumnIndex("port")));
 cmwapArrayList.add(map);
 }
 return true;// 表示存在cmwap接入点
 } else {
 return false;// 表示不存在cmwap接入点
 }
 }

你可能感兴趣的:(android APN cmnet切换cmwap)