原文转载自:

https://blog.csdn.net/Anmmei/article/details/77461530

==========照例先说废话,很久不写blog了,本人实际已一度退出码农生涯。转行通信去了。通信虽然没啥钱,但是做后台朝九晚五双休,半梦半醒干活,这才是生活啊。。。。感觉非常适合我这种大龄佛系青年。然而人算不如天算,一场疫情导致迟迟无法复工。人在杭州,房租贼贵,一天天的烧钱包,看着兜里不多的钱,其实有点坐不住,顶着疫情回家,然后年初正彷徨,打算省内继续干通信,未料前公司的合作公司的老板打电话让我去他厂里干活,横竖一时没通信的活可以干,所以继续入坑码农,以上。

最近有需求添加并设置APN。照例百度,找到一篇比较好的,就是上文地址。本人略微改进如下

package com.XXX你懂的;

import android.content.ContentResolver;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.telephony.TelephonyManager;

import android.util.Log;

public class APNUtli {

public static Uri APN_URI = Uri.parse("content://telephony/carriers");

public static Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

public static int addAPN(Context context, String apn) {

int id = -1;

String NUMERIC = getSIMInfo(context);

if (NUMERIC == null) {

return -1;

}

ContentResolver resolver = context.getContentResolver();

ContentValues values = new ContentValues();

values.put("name", apn); //apn中文描述

values.put("apn", apn); //apn名称

values.put("type", "default"); //apn类型

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", ""); //密码

values.put("mmsc", ""); //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;

}

public static String getSIMInfo(Context context) {

TelephonyManager iPhoneManager = (TelephonyManager)context

.getSystemService(Context.TELEPHONY_SERVICE);

return iPhoneManager.getSimOperator();

}

// 设置接入点

public static void setAPN(Context context, int id) {

ContentResolver resolver = context.getContentResolver();

ContentValues values = new ContentValues();

values.put("apn_id", id);

resolver.update(CURRENT_APN_URI, values, null, null);

}

public static int getAPN(Context context, String apn){

ContentResolver resolver = context.getContentResolver();

String apn_condition = String.format("apn like '%%%s%%'", apn);

Cursor c = resolver.query(APN_URI, null, apn_condition, null, null);

// 该项APN存在

if (c != null && c.moveToNext()) {

int id = c.getShort(c.getColumnIndex("_id"));

String name1 = c.getString(c.getColumnIndex("name"));

String apn1 = c.getString(c.getColumnIndex("apn"));

Log.e("APN has exist", id + name1 + apn1);

return id;

}

return -1;

}

}

用法如下:

String apn_name = "testapn567";

int apn_id = APNUtli.getAPN( ZTLHelperService.this, apn_name);

if(apn_id == -1){

apn_id = APNUtli.addAPN( ZTLHelperService.this, apn_name );

if( apn_id == -1){

Log.e(TAG, "设置APN失败");

}

}

if( apn_id != -1){

APNUtli.setAPN(ZTLHelperService.this, apn_id);

}

再次感谢文首楼主不吝分享。本人略作修改,方便有缘人。最近踩坑有点多,网上文章千篇一律。纯转载的事还是少做,起码得有点干货吧。