Skip to content

Instantly share code, notes, and snippets.

@filpgame
Last active August 23, 2019 19:13
Show Gist options
  • Save filpgame/e7962b6208786b7d7ebf39e3e625e907 to your computer and use it in GitHub Desktop.
Save filpgame/e7962b6208786b7d7ebf39e3e625e907 to your computer and use it in GitHub Desktop.
Setting APN

Configurando APN no android

Para adicionar, remover e setar APNs, você precisa usar os ContentResolver para gerenciar a base de APNs no SO.

Fizemos uma abstração do seu uso com os snippets abaixo e, para usá-lo você precisa fazer como exemplo a seguir:

ApnHandler apnHandler = new ApnHandler(context);
ApnInfo apnInfo = new ApnInfo("OPERADORA", "operadora.com.br", "user", "password"))
boolean success = apnHandler.configureApn(apnInfo);
public class ApnHandler {
private final static Uri APN_LIST_URI = Uri.parse("content://telephony/carriers");
private final static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
private Context context;
public ApnHandler(Context context) {
this.context = context;
}
public boolean configureApn(ApnInfo apnInfo) {
boolean ret;
ContentValues contentValues;
try {
contentValues = NetworkUtils.getContentValuesForApnInfo(context, apnInfo);
} catch (SimCardNotFoundException e) {
e.printStackTrace();
return false;
}
ret = findApn(apnInfo.getApn());
if (!ret) {
int apnId = addNewApn(context, contentValues);
if (apnId != -1) {
ret = setApn(apnId) == 1;
}
}
return ret;
}
private int addNewApn(Context context, ContentValues contentValues) {
int id = -1;
ContentResolver contentResolver = context.getContentResolver();
Uri newUri = contentResolver.insert(APN_LIST_URI, contentValues);
Cursor c = null;
if (newUri != null) {
c = contentResolver.query(newUri, null, null, null, null);
int idIndex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idIndex);
}
if (c != null) {
c.close();
}
return id;
}
private int setApn(long id) {
ContentResolver contentResolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put("apn_id", id);
return contentResolver.update(PREFERRED_APN_URI, contentValues, null, null);
}
private boolean findApn(String name) {
boolean found = false;
String columns[] = new String[] { "_id", "apn"};
String where = "apn = ?";
String wargs[] = new String[] {name};
String sortOrder = null;
Cursor cur = context.getContentResolver().query(APN_LIST_URI, columns, where, wargs, sortOrder);
if (cur != null) {
if (cur.moveToFirst()) {
Log.d("ApnHandler", "APN " + cur.getString(1));
found = true;
setApn(cur.getLong(0));
}
cur.close();
}
return found;
}
}
public class ApnInfo {
private String carrierName;
private String apn;
private String user;
private String password;
public ApnInfo(String carrierName, String apn, String user, String password) {
this.carrierName = carrierName;
this.apn = apn;
this.user = user;
this.password = password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment