原创:Android APN 设置 之 cmnet 改 cmwap(一)

直接进入主题... ...

 

package com.android.set.net.apn;

import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;

public class ChangeCurrentAPN {

 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配置信息位置

 public static final String TAG = "ouyangpin";

 // int apnId;

 Cursor cursor_current;
 Cursor cursor_need;
 Context context;

 public HttpReqConnect http;
 int newCreateAPNSet_Id;
 public ChangeCurrentAPN(Context context, HttpReqConnect http) {
  this.context = context;
  this.http = http;
  boolean isCmwap = getDefaultAPN();
  if (!isCmwap) {//当前的接入点不是cmwap接入点
   if (!checkWapAPN()) {//检查当前所有接入点中是否存在cmwap接入点,如果不存在任何cmwap接入点,则创建一个新的
    // 创建一个新的cmwap接入点并返回该新创建的接入点的id值
    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 {
    //从所有满足条件的接入点中选择合适的接入点并设置
    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;
     }
    }
   }
   try {
    Thread.sleep(10000);//因为系统更改APN设置需要一定的时间,但是目前我还不清楚当系统的APN被改变之后系统给出什么提示(到底是发广播还是什么,目前只知道会在通知栏通知一下),所以我这里暂时的让它睡眠10秒,让系统有足够的时间去更改APN,之后我们再去进行联网的操作。
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  } else {//当前的接入点已经是cmwap接入点
   //什么都不做!
   Log.d("chenjianli", "info: current apn set is cmnet,do not need to change .");
  }
 
 }

 String mobileInitalAPNSet_Id;
 // 获取当前apn属性
 private boolean getDefaultAPN() {
  cursor_current = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);
  if (cursor_current.moveToFirst()) {
         String proxy = cursor_current.getString(cursor_current.getColumnIndex("proxy"));
         String apn = cursor_current.getString(cursor_current.getColumnIndex("apn"));
         String port = cursor_current.getString(cursor_current.getColumnIndex("port"));
         String current = cursor_current.getString(cursor_current.getColumnIndex("current"));
   if (proxy.equals("10.0.0.172")|| proxy.equals("010.000.000.172")&&port.equals("80")&&apn.equals("cmwap")&&current.equals("1")) {
    mobileInitalAPNSet_Id = cursor_current.getString(cursor_current.getColumnIndex("_id"));
    return true;
   }
  }
  return false;
 }

 public boolean SetDefaultAPN(int id) {
  boolean res = false;
  ContentResolver resolver = context.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;
 }

 private ArrayList<HashMap<String, String>> cmwapArrayList = new ArrayList<HashMap<String, String>>();
 // 检测是否存在cmwap接入点
 private boolean checkWapAPN() {
  Cursor cursor_need = context.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接入点
  }
 }

 private int addCmwapAPN() {
  ContentResolver cr = context.getContentResolver();
  ContentValues cv = new ContentValues();
  cv.put("name", "mycmwap");
  cv.put("apn", "cmwap");
  cv.put("numeric", "46000");
  cv.put("mcc", "460");
  cv.put("mnc", "00");
  cv.put("proxy", "10.0.0.172");
  cv.put("port", "80");
  cv.put("mmsc", "
http://moternet.com.cn/");
  cv.put("current", 1);

  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"));
    return Integer.parseInt(id);//返回新创建的cmwap接入点的id
   }
  } catch (SQLException e) {
   Log.d(TAG, e.getMessage());
  }

  if (c != null)
   c.close();
  return 0;
 }

}

 

这里在放一个查看当前手机接入点的所有信息,大家可以做个打印:

 

   String[] names = cursor_current.getColumnNames();
   if (names != null) {
    sb.append("当前接入点配置信息:");
    for (int i = 0; i < names.length; ++i) {
     String value = cursor_current.getString(cursor_current.getColumnIndex(names[i]));
     sb.append(names[i]+" = "+value+"\n");
    }
   }

 

另外很重要的一点就是要配置好权限:

 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

你可能感兴趣的:(C++,c,android,C#,mobile)