APN簡述
APN決定了手機通過哪種接入方式來訪問網(wǎng)絡,用來標識GPRS的業(yè)務種類。
APN分為兩大類:
WAP業(yè)務。
WAP以外的服務,比如:連接因特網(wǎng)。
從運營商角度看,APN就是一個邏輯名字,APN一般都部署在GGSN設備上或者邏輯連接到GGSN上,用戶使用GPRS上網(wǎng)時,都通過GGSN代理出去到外部網(wǎng)絡,因此,APN設置、過濾、統(tǒng)計等,就成為一個對GPRS計費、GPRS資費有重要參考價值的參數(shù)之一(因為APN可以區(qū)分一個業(yè)務或者外部網(wǎng)絡)。
APN的完整說明在3GPP規(guī)范TS23.003 Clause 9中進行了詳細定義。
寫入條件
1.應用必須系統(tǒng)級簽名
2.插入SIM卡
3.添加權(quán)限:<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
具體實現(xiàn)
Android系統(tǒng)中,對于APN網(wǎng)絡的API是隱藏的,因此獲取手機的APN設置,需要通過ContentProvider來進行數(shù)據(jù)庫查詢,查詢的URI地址是:
取得全部的APN列表:content://telephony/carriers;
取得當前設置的APN:content://telephony/carriers/preferapn;
取得current=1的APN:content://telephony/carriers/current;
工具類代碼實現(xiàn):
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;
public class APNUtil {
public Context context;
/**
* 當前連接APN
* */
private Uri APN_URI_NOW =Uri.parse("content://telephony/carriers/current");
/**
* 所有APN
* */
private Uri APN_URI = Uri.parse("content://telephony/carriers");
private Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
// 新增一個cmnet接入點
public APNUtil(Context context){
this.context = context;
}
public int addAPN(String apnName, String apn) {
int id = -1;
String NUMERIC = getSIMInfo();
if (NUMERIC == null) {
return -1;
}
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
//apn中文描述
values.put("name", apnName);
//apn名稱
values.put("apn", apn);
//apn類型
values.put("type", "default");
values.put("numeric", NUMERIC);
values.put("mcc", NUMERIC.substring(0, 3));
values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));
//代理
values.put("proxy", "");
//端口
values.put("port", "");
//彩信代理
values.put("mmsproxy", "");
//彩信端口
values.put("mmsport", "");
//用戶名
values.put("user", "");
//服務器
values.put("server", "");
//密碼
values.put("password", "");
//MMSC
values.put("mmsc", "");
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();
}
return id;
}
protected String getSIMInfo() {
TelephonyManager iPhoneManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return iPhoneManager.getSimOperator();
}
// 設置接入點
public void SetAPN(int id) {
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
resolver.update(CURRENT_APN_URI, values, null, null);
}
public Boolean checkAPN(String apn) {
// 檢查系統(tǒng)已經(jīng)寫入的APN
Cursor cr = context.getContentResolver().query(APN_URI, null, null, null, null);
while (cr != null && cr.moveToNext()) {
// Log.d("apn", "APN: "+ cr.getString(cr.getColumnIndex("apn")));
if(cr.getString(cr.getColumnIndex("apn")).equals(apn)){
return true;
}
}
return false;
}
}
工具類的調(diào)用:
public void addAPN(){
APNUtil apn = new APNUtil(context);
Boolean hasCMIOT = apn.checkAPN("CMIOT");
Boolean hasCMMTM = apn.checkAPN("CMMTM");
Log.d("apn", "hasCMIOT: "+hasCMIOT);
Log.d("apn", "hasCMMTM: "+hasCMMTM);
if(! hasCMIOT || !hasCMMTM){
int simState = NetUtil.getSimState(this);
//如果有SIM卡
if (simState == TelephonyManager.SIM_STATE_READY) {
if(!hasCMIOT) {
apn.addAPN("移動物聯(lián)網(wǎng)卡APN", "CMIOT");
}else if(!hasCMMTM){
apn.addAPN("移動物聯(lián)網(wǎng)卡APN","CMMTM");
}
}else {
Log.d("apn", "no SIMCard ");
}
}
}