Android应用层修改APN

public class SetAPN {

    private static Uri APN_LIST_URI = Uri.parse("content://telephony/carriers");
    private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

    protected static String getSIMInfo() {
        TelephonyManager iPhoneManager = (TelephonyManager) MyApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
        return iPhoneManager.getSimOperator();
    }

//    public static int AddXFTApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
//        MLog.e("添加一个新的apn ==> APN");
//        int apnId = -1;
//        String NUM = "46003";
//        String NUMERIC = getSIMInfo();
//        if (NUMERIC == null) {
//            return -1;
//        }
//        if(NUMERIC.equals(NUM)){
//            NUMERIC = "46011";
//        }
//        ContentResolver resolver = MyApp.getApp().getContentResolver();
//        ContentValues values = new ContentValues();
//
        values.put("name","China Telecom"); //apn中文描述
        values.put("carrier","Mytest");
//        values.put("name","");
//        values.put("apn", apn);  //apn名称
//        values.put("type", "");
//        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", user);
//        values.put("server", "");
//        values.put("password", password);
//        values.put("localized_name", "APN_NAME_CTLTE");
//        values.put("read_only", "true");
//        values.put("visit_area", "460");
//        values.put("protocol", protocol);
//        values.put("roaming_protocol", r_protocol);
//        values.put("mmsc", "");
//        values.put("authtype", authtype);
//        Cursor c = null;
//        // 获取新添加的apn的ID
//        try {
//
//            Uri newRow = resolver.insert(APN_LIST_URI, values);
//            if (newRow != null) {
//                c = resolver.query(newRow, null, null, null, null);
//                int idindex = c.getColumnIndex("_id");
//                c.moveToFirst();
//                apnId = c.getShort(idindex);
//                MLog.e("ID = " + apnId);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        if (c != null)
//            c.close();
//        return apnId;
//    }

    public static void setDefaultApn(int apnId) {
        ContentResolver resolver = MyApp.getApp().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", apnId);
        MLog.e("apnId = " + apnId);
        //更新当前状态APN信息
        resolver.update(PREFERRED_APN_URI, values, "apn_id=?", new String[]{apnId + ""});
    }


    public static int AddXFTApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
        ContentResolver resolver = MyApp.getApp().getContentResolver();
        String NUM = "46003";
        String NUMERIC = getSIMInfo();
        if (NUMERIC == null) {
            return -1;
        }
        if (NUMERIC.equals(NUM)) {
            NUMERIC = "46011";
        }
        MLog.d("查询到的SIM:" + NUMERIC);
        ContentValues values = new ContentValues();
        values.put("name", "");
        values.put("apn", apn);  //apn名称
        values.put("type", "");
        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", user);
        values.put("server", "");
        values.put("password", password);
        values.put("localized_name", "APN_NAME_CTLTE");
        values.put("read_only", "true");
        values.put("visit_area", "460");
        values.put("protocol", protocol);
        values.put("roaming_protocol", r_protocol);
        values.put("mmsc", "");
        values.put("authtype", authtype);
        try {
            //更新数据库,替换当前数据库APN信息
            int id = resolver.update(APN_LIST_URI, values, "numeric=?", new String[]{NUMERIC});
            MLog.d("修改:" + id);
            return id;

        } catch (Exception e) {
            e.printStackTrace();
            MLog.d("异常:" + e.toString());
        }
        return -2;
    }

//封装的AIDL接口
    public static void setApn(String apn, String user, String password, String protocol, String r_protocol, String authtype) {
        setDefaultApn(AddXFTApn(apn, user, password, protocol, r_protocol, authtype));
    }

}

 

修改Android设备的APN(接入点)信息,你可以使用`ContentResolver`和`ContentValues`来更新APN数据库中的数据。 以下是一个示例,展示如何使用Kotlin修改APN信息: ```kotlin import android.content.ContentResolver import android.content.ContentValues import android.net.Uri fun updateApnSettings(contentResolver: ContentResolver, apnId: Long, apnName: String, apnType: String, apnProxy: String, apnPort: String) { val contentValues = ContentValues().apply { put("apn", apnName) put("type", apnType) put("proxy", apnProxy) put("port", apnPort) } val updateUri = Uri.parse("content://telephony/carriers/$apnId") contentResolver.update(updateUri, contentValues, null, null) } ``` 在上面的示例中,`updateApnSettings()`函数接受一个`ContentResolver`对象、APN的ID、要修改APN名称、APN类型、APN代理和APN端口作为参数。它使用`ContentValues`对象来存储要更新的APN数据。 然后,我们通过将APN的ID附加到`content://telephony/carriers/`URL上来构建要更新的APN的URI。最后,我们使用`ContentResolver`的`update()`方法来执行更新操作。 以下是如何使用上述示例中的函数来更新APN信息: ```kotlin val contentResolver = context.contentResolver val apnId = 12345L // 要修改APN的ID val apnName = "New APN Name" val apnType = "default" val apnProxy = "proxy.example.com" val apnPort = "8080" updateApnSettings(contentResolver, apnId, apnName, apnType, apnProxy, apnPort) ``` 在上面的示例中,我们首先获取一个`ContentResolver`对象,然后指定要修改APN的ID、新的APN名称、APN类型、APN代理和APN端口。最后,我们调用`updateApnSettings()`函数来更新APN信息。 请注意,修改APN信息可能需要特定的权限(例如,WRITE_APN_SETTINGS权限),请确保你的应用程序具有所需的权限。 希望这个示例能帮助你修改Android设备的APN信息!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值