是否可以在Android 4.4上以编程方式编写APN? 我注意到Telephony.Carriers可用于API级别19。
答案 0 :(得分:0)
我有一个具有APN写入功能的软件。从Android 4.0开始,只有拥有系统权限才能编写APN设置。我的软件设计为系统软件,因此我可以编写APN和其他设置。但是,此功能已不再适用于Android 4.4,我不知道为什么。
在阅读Telephony.Carriers类之后,我找不到一种新的编写APN的方法。它只是一个告诉你信息的类,但不能改变它们。
我写APN的代码是:
public class APN {
public static final int AUTH_TYPE_UNKNOW = -1;
public static final int AUTH_TYPE_NONE = 0;
public static final int AUTH_TYPE_PAP = 1;
public static final int AUTH_TYPE_CHAP = 2;
public static final int AUTH_TYPE_PAP_OR_CHAP = 3;
private int id;
private String name;
private String user;
private String password;
private String apn;
private String mcc;
private String mnc;
private String type;
private String server;
private String proxy;
private String port;
private String mmsProxy;
private String mmsPort;
private String mmsc;
private String current;
private int authType;
public APN() {
id = -1;
name = "";
user = "";
password = "";
apn = "";
mcc = "";
mnc = "";
type = "default,supl";
server = "";
proxy = "";
port = "";
mmsProxy = "";
mmsPort = "";
mmsc = "";
current = "";
authType = AUTH_TYPE_NONE;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getApn() {
return apn;
}
public void setApn(String apn) {
this.apn = apn;
}
public String getMcc() {
return mcc;
}
public void setMcc(String mcc) {
this.mcc = mcc;
}
public String getMnc() {
return mnc;
}
public void setMnc(String mnc) {
this.mnc = mnc;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getProxy() {
return proxy;
}
public void setProxy(String proxy) {
this.proxy = proxy;
}
public String getMmsProxy() {
return mmsProxy;
}
public void setMmsProxy(String mmsProxy) {
this.mmsProxy = mmsProxy;
}
public String getMmsPort() {
return mmsPort;
}
public void setMmsPort(String mmsPort) {
this.mmsPort = mmsPort;
}
public String getMmsc() {
return mmsc;
}
public void setMmsc(String mmsc) {
this.mmsc = mmsc;
}
public String getCurrent() {
return current;
}
public void setCurrent(String current) {
this.current = current;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public int getAuthType() {
return authType;
}
public void setAuthType(int authType) {
this.authType = authType;
}
}
和
public class APNUtils {
private static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
private static final Uri APN_PREFER_URI = Uri.parse("content://telephony/carriers/preferapn");
private static ContentValues prepareValues(APN apn) {
ContentValues values = new ContentValues();
if (!apn.getName().trim().equals(""))
values.put("name", apn.getName());
if (!apn.getApn().trim().equals(""))
values.put("apn", apn.getApn());
if (!apn.getMcc().trim().equals(""))
values.put("mcc", apn.getMcc());
if (!apn.getMnc().trim().equals(""))
values.put("mnc", apn.getMnc());
if (!apn.getMcc().trim().equals("") && !apn.getMnc().trim().equals(""))
values.put("numeric", apn.getMcc() + apn.getMnc());
if (!apn.getUser().trim().equals(""))
values.put("user", apn.getUser());
if (!apn.getPassword().trim().equals(""))
values.put("password", apn.getPassword());
if (!apn.getServer().trim().equals(""))
values.put("server", apn.getServer());
if (!apn.getProxy().trim().equals(""))
values.put("proxy", apn.getProxy());
if (!apn.getPort().trim().equals(""))
values.put("port", apn.getPort());
if (!apn.getMmsProxy().trim().equals(""))
values.put("mmsproxy", apn.getMmsProxy());
if (!apn.getMmsPort().trim().equals(""))
values.put("mmsport", apn.getMmsPort());
if (!apn.getMmsc().trim().equals(""))
values.put("mmsc", apn.getMmsc());
if (!apn.getType().trim().equals(""))
values.put("type", apn.getType());
if (!apn.getCurrent().trim().equals(""))
values.put("current", apn.getCurrent());
values.put("authtype", apn.getAuthType());
return values;
}
public static int createNewApn(Context context, APN apn, boolean setAsDefaultAPN) {
Logs.infoLog("APNutils.createNewApn.");
int apnid = -1;
try {
if (apn != null) {
Logs.infoLog("APNutils.createNewApn. Reading APN list.");
Uri APN_URI = Uri.parse("content://telephony/carriers");
ContentResolver resolver = context.getContentResolver();
Logs.infoLog("APNutils.createNewApn. Creating new registry based on parameters.");
ContentValues values = prepareValues(apn);
Logs.infoLog("APNutils.createNewApn. Inserting new APN.");
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if(newRow != null) {
Logs.infoLog("APNutils.createNewApn. Getting new ID.");
c = resolver.query(newRow, null, null, null, null);
int tableIndex = c.getColumnIndex("_id");
c.moveToFirst();
apnid = c.getShort(tableIndex);
} else
Logs.warningLog("APNutils.createNewApn. New APN was not found. Inserting failed?");
if(c != null){
c.close();
}
if (apnid > -1 && setAsDefaultAPN) {
Logs.infoLog("APNutils.createNewApn. Setting new APN as default.");
ContentValues v = new ContentValues(1);
v.put("apn_id", apnid);
context.getContentResolver().update(APN_PREFER_URI, v, null, null);
}
} else
Logs.warningLog("APNutils.createNewApn. Invalid apn (null).");
} catch (Exception e) {
Logs.errorLog("createNewApn: error", e);
}
Logs.infoLog("APNutils.createNewApn. Returning ID " + String.valueOf(apnid));
return apnid;
}
public static boolean updateApn(Context context, int id, APN apn) {
Logs.infoLog("APNutils.updateApn.");
if (apn != null) {
try {
Logs.infoLog("APNutils.updateApn. Reading APN list.");
Uri APN_URI = Uri.parse("content://telephony/carriers");
ContentResolver resolver = context.getContentResolver();
Logs.infoLog("APNutils.updateApn. Creating new registry based on parameters.");
ContentValues values = prepareValues(apn);
Logs.infoLog("APNutils.updateApn. Inserting new APN.");
int result = resolver.update(APN_URI, values, "_id = " + String.valueOf(id), null);
if (result != -1) {
Logs.infoLog("APNutils.updateApn. APN updated.");
return true;
} else {
Logs.warningLog("APNutils.updateApn. Invalid ID (" + String.valueOf(id) + ").");
return false;
}
} catch (Exception e) {
Logs.errorLog("APNUtils.updateApn error: ", e);
return false;
}
} else {
Logs.warningLog("APNutils.updateApn. Invalid apn (null).");
return false;
}
}
public static boolean verifyApn(Context context, String apn) {
Logs.infoLog("APNutils.verifyApn.");
return getApn(context, apn) > -1;
}
public static int getApn(Context context, String apn) {
Logs.infoLog("APNutils.getApn.");
int result = -1;
Logs.infoLog("APNutils.getApn. Looking for APN " + apn);
String columns[] = new String[] { "_ID", "NAME" };
String where = "name = ?";
String wargs[] = new String[] { apn };
String sortOrder = null;
Cursor cur = context.getContentResolver().query(APN_TABLE_URI, columns, where, wargs, sortOrder);
if (cur != null) {
int tableIndex = cur.getColumnIndex("_id");
if (cur.moveToFirst()) {
Logs.infoLog("APNutils.getApn. APN found.");
result = cur.getShort(tableIndex);
}
cur.close();
}
if (result == -1)
Logs.warningLog("APNutils.getApn. APN not found.");
return result;
}
public static boolean setPreferredApn(Context context, String apn) {
Logs.infoLog("APNutils.setPreferredApn.");
boolean changed = false;
Logs.infoLog("APNutils.setPreferredApn. Looking for APN " + apn);
String columns[] = new String[] { "_ID", "NAME" };
String where = "name = ?";
String wargs[] = new String[] { apn };
String sortOrder = null;
Cursor cur = context.getContentResolver().query(APN_TABLE_URI, columns, where, wargs, sortOrder);
if (cur != null) {
if (cur.moveToFirst()) {
Logs.infoLog("APNutils.setPreferredApn. APN found. Setting as default.");
ContentValues values = new ContentValues(1);
values.put("apn_id", cur.getLong(0));
if (context.getContentResolver().update(APN_PREFER_URI, values, null, null) == 1) {
Logs.infoLog("APNutils.setPreferredApn. APN marked as default.");
changed = true;
}
}
cur.close();
}
if (!changed)
Logs.warningLog("APNutils.setPreferredApn. APN not found or could not be marked as default.");
return changed;
}
public static APN[] getAllApnList(Context context) {
Logs.infoLog("APNutils.getAllApnList.");
APN[] result = null;
Uri contentUri = Uri.parse("content://telephony/carriers/");
Cursor cursor = null;
try
{
cursor = context.getContentResolver().query(contentUri,
new String[] {"_ID", "name", "apn", "mcc", "mnc", "numeric", "user", "password", "server", "proxy",
"port", "mmsproxy", "mmsport", "mmsc", "type", "current", "authtype "}, null, null, null);
if (cursor != null)
{
result = new APN[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
APN apn = new APN();
apn.setId(cursor.getInt(0));
apn.setName(cursor.getString(1));
apn.setApn(cursor.getString(2));
apn.setMcc(cursor.getString(3));
apn.setMnc(cursor.getString(4));
apn.setUser(cursor.getString(6));
apn.setPassword(cursor.getString(7));
apn.setServer(cursor.getString(8));
apn.setProxy(cursor.getString(9));
apn.setPort(cursor.getString(10));
apn.setMmsProxy(cursor.getString(11));
apn.setMmsPort(cursor.getString(12));
apn.setMmsc(cursor.getString(13));
apn.setType(cursor.getString(14));
apn.setCurrent(cursor.getString(15));
apn.setAuthType(cursor.getInt(16));
result[i] = apn;
i++;
}
}
}
catch (Exception ex)
{
//Handle exceptions here
return null;
}
finally
{
if (cursor != null)
cursor.close();
}
return result;
}
}