在Android中以编程方式设置APN

时间:2015-05-13 11:54:31

标签: android android-intent android-contentprovider device-driver device-admin

我正在Android中以编程方式设置APN。当我运行代码时,我得到Security Exception :android.permission.WRITE_APN_SETTINGS。如果我在清单中提到了这个权限,我就会收到像这样的权限只有SYSTEM APPS的错误。  你可以帮我解决这个问题。 参考link

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("tmobile"); 
setPreferredAPN(identity);

3 个答案:

答案 0 :(得分:1)

从android Pie(Api 28)中,您可以通过DevicePolicyManager配置APN,但是您需要按照addOverrideApn文档中的说明将应用设置为DeviceOwner。

如何配置DeviceOwner进行测试 https://source.android.com/devices/tech/admin/testing-setup

在您的情况下可能不可行

答案 1 :(得分:0)

试试这个

  

窗口 - >偏好 - > Android - > Lint错误检查。

从列表中找到 ProtectedPermission ,并将严重性设置为错误以外的内容(例如信息)。这样你的项目仍然可以编译。

来源:Permission is only granted to system app, in Manifest

答案 2 :(得分:0)

这是Android 4.2的回归,请参阅此https://stackoverflow.com/a/13454563/5146667

此时(API 23)我能够阅读“apn”&未经许可,“类型”属性,可能是其他一些,但不是“当前”,我非常需要!