APN管理

作者: 奥利奥真好吃 | 来源:发表于2018-07-09 17:07 被阅读0次

手机放入sim卡后可以对apn进行管理,主要是数据库的操作。由于当时要做的是一个IPC的方案,所以为了解决aidl对参数类的限制,需要自定义如下的aidl类型:

package com.xxx.service;

import android.os.Parcel;
import android.os.Parcelable;

public class ApnInfo implements Parcelable {
    private String apnName;
    private String apn;
    private String userName;
    private String password;
    private String proxy;
    private String port;
    
    public ApnInfo(){
        
    }
    
    public ApnInfo(Parcel parcel){
        readFromParcel(parcel);
    }
    
    
    public String getApnName() {
        return apnName;
    }

    public void setApnName(String apnName) {
        this.apnName = apnName;
    }

    public String getApn() {
        return apn;
    }

    public void setApn(String apn) {
        this.apn = apn;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getProxy() {
        return proxy;
    }

    public void setProxy(String proxy) {
        this.proxy = proxy;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return "ApnInfo [apnName=" + apnName + ", apn=" + apn + ", userName="
                + userName + ", password=" + password + ", proxy=" + proxy
                + ", port=" + port + "]";
    }

    private void readFromParcel(Parcel parcel) {
        // TODO Auto-generated method stub
        this.apnName=parcel.readString();
        this.apn=parcel.readString();
        this.userName=parcel.readString();
        this.password=parcel.readString();
        this.proxy=parcel.readString();
        this.port=parcel.readString();
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeString(this.apnName);
        dest.writeString(this.apn);
        dest.writeString(this.userName);
        dest.writeString(this.password);
        dest.writeString(this.proxy);
        dest.writeString(this.port);
    }
    public static final Parcelable.Creator<ApnInfo> CREATOR = new Creator<ApnInfo>() {

        @Override
        public ApnInfo[] newArray(int size) {
            // TODO Auto-generated method stub
            return new ApnInfo[size];
        }

        @Override
        public ApnInfo createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new ApnInfo(source);
        }
    };
}

然后定义一个同名的aidl类,将其声明为可在aidl中进行传递的类:

package com.xxx.service;
parcelable ApnInfo;

之后,我们就可以直接在aidl中传递这个自定义类型ApnInfo,最后就是相应service的实现:

    private static final Uri APN_URI = Uri.parse("content://telephony/carriers");      
    private static final Uri PREFERAPN_URI = Uri.parse("content://telephony/carriers/preferapn");

   private Uri getUriForCurrSubId(Uri uri) {
        SubscriptionInfo mSubscriptionInfo = SubscriptionManager.from(mContext).getActiveSubscriptionInfo(SubscriptionManager.getDefaultDataSubscriptionId());
        int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId()
                : SubscriptionManager.INVALID_SUBSCRIPTION_ID;
        if (SubscriptionManager.isValidSubscriptionId(subId)) {
            return Uri.withAppendedPath(uri, "subId/" + String.valueOf(subId));
        } else {
            return uri;
        }
    }
    private String getSelectedApnKey() {
        String key = null;
        Cursor cursor = mContext.getContentResolver().query(getUriForCurrSubId(PREFERAPN_URI),
                new String[] {"_id"}, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            key = cursor.getString(0);
        }
        cursor.close();
        return key;
    }   
    //添加APN信息
    public boolean addApn(ApnInfo apnInfo){
        // TODO Auto-generated method stub
        Log.d(TAG,"start addApn");
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String numeric=tm.getSubscriberId().substring(0, 5);
        ContentValues values = new ContentValues();
        values.put("name", apnInfo.getApnName());
        values.put("apn", apnInfo.getApn());
        values.put("type","default");
        values.put("numeric",numeric);
        values.put("mcc", numeric.substring(0, 3));
        values.put("mnc", numeric.substring(3, 5));
        values.put("proxy", apnInfo.getProxy());
        values.put("port", apnInfo.getPort());
        values.put("mmsproxy","");
        values.put("mmsport","");
        values.put("user",apnInfo.getUserName());
        values.put("server","");
        values.put("password",apnInfo.getPassword());
        values.put("mmsc","");      
        Uri newUri = mContext.getContentResolver().insert(APN_URI, values);
        if (newUri != null){
            Log.d(TAG, "addApn: " + newUri);
            return true;
        }
        Log.d(TAG, "addApn failed");
        return false;   
    }
    //删除APN信息
    public boolean deleteApn(String apnName){
        // TODO Auto-generated method stub
        Log.d(TAG,"start deleteApn");
        int count=mContext.getContentResolver().delete(APN_URI,"name=?",new String[]{apnName});
        return count>0;
    }   
    //获得当前的APN信息
    public ApnInfo getCurrApn(){
        Log.d(TAG,"start getCurrApn");
        ApnInfo apnInfo=new ApnInfo();
        int pos = Integer.parseInt(getSelectedApnKey());
        Uri PreferApnUri = ContentUris.withAppendedId(APN_URI, pos);
        Cursor cr = mContext.getContentResolver().query(PreferApnUri, null, null, null, null);
        Log.d(TAG, "getCurrentApn : cr=" + cr);
        while(cr!=null && cr.moveToNext()){
            apnInfo.setApnName(cr.getString(cr.getColumnIndex("name")));
            apnInfo.setApn(cr.getString(cr.getColumnIndex("apn")));
            apnInfo.setUserName(cr.getString(cr.getColumnIndex("user")));
            apnInfo.setPassword(cr.getString(cr.getColumnIndex("password")));
            apnInfo.setProxy(cr.getString(cr.getColumnIndex("proxy")));
            apnInfo.setPort(cr.getString(cr.getColumnIndex("port")));
        }
        if(cr!=null){
            cr.close();
        }
        return apnInfo;     
    }
    //设置当前的APN
    public boolean setCurrApn(String apnName){  
        Log.d(TAG,"start setCurrApn");  
        int count=-1;
        ContentResolver resolver = mContext.getContentResolver();   
        Cursor cursor= mContext.getContentResolver().query(APN_URI,new String[] {"_id"},"name=?",new String[]{apnName},null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            ContentValues values = new ContentValues();
            values.put("apn_id", cursor.getInt(0));
            count=resolver.update(getUriForCurrSubId(PREFERAPN_URI), values, null, null);
        }
        if (cursor != null) {
            cursor.close();
        }
        return count>0;     
    }
    //查询所有已有的APN信息
    public List<ApnInfo> getAllApn(){   
        Log.d(TAG,"start getAllApn");
        List<ApnInfo> list = new ArrayList();
        CarrierConfigManager configManager = (CarrierConfigManager)
                mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
        PersistableBundle b = configManager.getConfig();
        boolean mHideImsApn = b.getBoolean(CarrierConfigManager.KEY_HIDE_IMS_APN_BOOL);     
        final TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        final String mccmnc = tm.getSubscriberId().substring(0, 5);
        Log.d(TAG, "mccmnc = " + mccmnc);
        StringBuilder where = new StringBuilder("numeric=\"" + mccmnc +
                "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL)) AND user_visible!=0");

        if (mHideImsApn) {
            where.append(" AND NOT (type='ims')");
        }

        Cursor cr = mContext.getContentResolver().query(APN_URI, null, where.toString(), null, null);  
        while (cr != null && cr.moveToNext()) { 
            ApnInfo apnInfo=new ApnInfo();
            apnInfo.setApnName(cr.getString(cr.getColumnIndex("name")));
            apnInfo.setApn(cr.getString(cr.getColumnIndex("apn")));
            apnInfo.setUserName(cr.getString(cr.getColumnIndex("user")));
            apnInfo.setPassword(cr.getString(cr.getColumnIndex("password")));
            apnInfo.setProxy(cr.getString(cr.getColumnIndex("proxy")));
            apnInfo.setPort(cr.getString(cr.getColumnIndex("port")));       
            Log.d(TAG, "apnInfo=" + apnInfo);  
            list.add(apnInfo);
        }
        if(cr!=null){
            cr.close();
        }
        Log.d(TAG, "getApnList---list" + list);  
        return list;        
    }

没有什么可讲的,就是数据库的相关操作。

相关文章

  • APN管理

    手机放入sim卡后可以对apn进行管理,主要是数据库的操作。由于当时要做的是一个IPC的方案,所以为了解决aidl...

  • 三、Android 网络评分机制

    在前两节简单介绍了连接管理的大致框架,数据链接的准备工作,包括APN的初始化与默认APN使能,DcTracker的...

  • Android系统apn介绍

    快速入口: Apn_change完整时序图 1. APN概念 APN全称是Access Point Name,中文...

  • APN学习系列Ⅱ — 物联网卡

    此文为转载,出处:物联网卡 APN 接入方式相关问题 导读: 物联网专网APN根据客户属性类型分为全网APN和本地...

  • APN

    APN指一种网络接入技术,是通过手机上网时必须配置的一个参数,它决定了手机通过哪种接入方式来访问网络。 对于手机用...

  • #APN

    从APN的模式下逃离了两年,以为终于要从此摆脱了,可曾想到兜兜转转还是回到了这个模式。从那个地方离开,你后悔了吗?...

  • Android应用层写入APN

    APN简述 APN决定了手机通过哪种接入方式来访问网络,用来标识GPRS的业务种类。APN分为两大类:WAP业务。...

  • APN的基础知道-安卓

    APN的基础知识 在安卓开发中,尤其是网络开发,可能会经常接触到APN,下面讲解一下相关的基础知识。 一、APN简...

  • app上传app Store The session's

    关闭APN,重新上传即可

  • APN设置

    由于移动运营商网络的限制,个别手机型号可能存在不兼容问题,如果您拿到卡后,充值后不能正常使用的,请尝试以下解决方法...

网友评论

      本文标题:APN管理

      本文链接:https://www.haomeiwen.com/subject/jsetpftx.html