gpt4 book ai didi

Android 4.2.1 - 以编程方式更新 APN 设置数据库中的列

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:22 26 4
gpt4 key购买 nike

我正在尝试更新位于以下位置的首选 APN 数据库表的“名称”列:content://telephony/carriers/preferapn。但是,我的应用程序的 ContentResolver.update() 始终返回 0,这意味着没有更新任何行。我已经拥有该应用程序的 root 访问权限,并在运行 update() 之前以编程方式确认了它。

可以找到有关表列的详细信息herehere .我还确保引用了 documentation关于如何调用更新功能。

更新方法:

public boolean setAPN(String newAPN, TextView t){

//get URI objects for the tables
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

//Confirm permissions
PackageManager pm = getPackageManager();
if (pm.checkPermission(permission.WRITE_APN_SETTINGS, getPackageName()) == PackageManager.PERMISSION_GRANTED) {

//Update name field
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", newAPN);
long rc = resolver.update(PREFERRED_APN_URI, values, null, null);

//Display the row contents (always has the original fields, doesn't update)
Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
c.moveToFirst();
int index = c.getColumnIndex("_id"); //getting index of required column
Short id = c.getShort(index); //getting APN's id from
index = c.getColumnIndex("name");
String name = c.getString(index);
index = c.getColumnIndex("mcc");
String mcc = c.getString(index);
index = c.getColumnIndex("mnc");
String mnc = c.getString(index);
index = c.getColumnIndex("numeric");
String numeric = c.getString(index);

t.setText(" ID:" + id + "\n" +
" APN Name: " + name + "\n" +
" MCC: " + mcc + "\n" +
" MNC: " + mnc + "\n" +
" Numeric: " + numeric + "\n"

);

} else {
t.setText(" You don't have permission to do this. ");
}

return true;
}

如前所述,我也有权限: list 文件中的“android.permission.WRITE_APN_SETTINGS”:

我是否出于特定目的错误地调用了更新函数?还是使用 update() 是错误的方法?

最佳答案

这个问题已经死了快一个月了,但我为这个问题做了一个解决方法并想分享它。解决方法包括复制不包括名称字段的现有首选 APN 条目,将新 APN 插入运营商列表,然后使用新生成的 ID 将新 APN 设置为首选。

public int InsertAPN(String name){      

//Set the URIs and variables
int id = -1;
boolean existing = false;
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

//Check if the specified APN is already in the APN table, if so skip the insertion
Cursor parser = getContentResolver().query(APN_TABLE_URI, null, null, null, null);
parser.moveToLast();
while (parser.isBeforeFirst() == false){
int index = parser.getColumnIndex("name");
String n = parser.getString(index);
if (n.equals(name)){
existing = true;
Toast.makeText(getApplicationContext(), "APN already configured.",Toast.LENGTH_SHORT).show();
break;
}
parser.moveToPrevious();
}

//if the entry doesn't already exist, insert it into the APN table
if (!existing){

//Initialize the Content Resolver and Content Provider
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();

//Capture all the existing field values excluding name
Cursor apu = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
apu.moveToFirst();
int index;

index = apu.getColumnIndex("apn");
String apn = apu.getString(index);
index = apu.getColumnIndex("type");
String type = apu.getString(index);
index = apu.getColumnIndex("proxy");
String proxy = apu.getString(index);
index = apu.getColumnIndex("port");
String port = apu.getString(index);
index = apu.getColumnIndex("user");
String user = apu.getString(index);
index = apu.getColumnIndex("password");
String password = apu.getString(index);
index = apu.getColumnIndex("server");
String server = apu.getString(index);
index = apu.getColumnIndex("mmsc");
String mmsc = apu.getString(index);
index = apu.getColumnIndex("mmsproxy");
String mmsproxy = apu.getString(index);
index = apu.getColumnIndex("mmsport");
String mmsport = apu.getString(index);
index = apu.getColumnIndex("mcc");
String mcc = apu.getString(index);
index = apu.getColumnIndex("mnc");
String mnc = apu.getString(index);
index = apu.getColumnIndex("numeric");
String numeric = apu.getString(index);

//Assign them to the ContentValue object
values.put("name", name); //the method parameter
values.put("apn", apn);
values.put("type", type);
values.put("proxy", proxy);
values.put("port", port);
values.put("user", user);
values.put("password", password);
values.put("server", server);
values.put("mmsc", mmsc);
values.put("mmsproxy", mmsproxy);
values.put("mmsport", mmsport);
values.put("mcc", mcc);
values.put("mnc", mnc);
values.put("numeric", numeric);

//Actual insertion into table
Cursor c = null;
try{
Uri newRow = resolver.insert(APN_TABLE_URI, values);

if(newRow != null){
c = resolver.query(newRow, null, null, null, null);
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
}
}
catch(SQLException e){}
if(c !=null ) c.close();
}

return id;
}

//Takes the ID of the new record generated in InsertAPN and sets that particular record the default preferred APN configuration
public boolean SetPreferredAPN(int id){

//If the id is -1, that means the record was found in the APN table before insertion, thus, no action required
if (id == -1){
return false;
}

Uri.parse("content://telephony/carriers");
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

boolean res = false;
ContentResolver resolver = this.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){}
return res;
}

然后您可以像这样调用该方法:

int identity = InsertAPN(NEW_APN_NAME); 
SetPreferredAPN(identity);

关于Android 4.2.1 - 以编程方式更新 APN 设置数据库中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24054893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com