Android實現APN設置專網並切換默認網絡

*最近接觸到了銀聯Pos通這些業務,需要使用專網進行數據報文通訊,在這裏自己寫了一個工具類,進行配置APN專網
參數,並進行默認切換到自己添加進去的APN,實現專網數據通訊。
注意:爲了防止在本地重複添加同一個APN數據,在每次保存之前先進行判斷,沒有檢查到對應專網名的數據時進行本地添加否則直接進行專網切換即可。*


下面貼出工具類源代碼:



import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Created by hizha on 2018/4/17.
 */

public class APN {

    //取得全部的APN列表:
    public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
    //    取得當前設置的APN:
    public static final Uri preferapn = Uri.parse("content://telephony/carriers/preferapn");
    //取得current=1的APN
    public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/current");

    private Context context;
    public static boolean hasAPN;

    public APN(Context context){
        this.context = context;
    }

    // 新增一個cmnet接入點
    //    public void APN() {
    //
    //        checkAPN();
    //    }

    public int addAPN() {
        int id = -1;
        String NUMERIC = getSIMInfo();
        if (NUMERIC == null) {
            return -1;
        }
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "專用APN");                                  //apn中文描述
        values.put("apn", "xxx.ln");                                     //apn名稱
        values.put("type", "default");                            //apn類型
        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", "1116");                                         //端口
        values.put("mmsproxy", "");                                     //彩信代理
        values.put("mmsport", "");                                      //彩信端口
        values.put("user", "");                                         //用戶名
        values.put("server","1.1.1.1");                                       //服務器
        values.put("password", "");                                     //密碼
        values.put("mmsc", "");                                          //MMSC
        Cursor c = null;
        Uri newRow = resolver.insert(APN_URI, values);
        if (newRow != null) {
            c = resolver.query(newRow, null, null, null, null);
            int idIndex = c.getColumnIndex("_id");
            c.moveToFirst();
            id = c.getShort(idIndex);
        }
        if (c != null)
            c.close();
        return id;
    }

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

    // 設置接入點
    public void SetAPN(int id) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", id);
        resolver.update(APN_URI, values, null, null);
//        resolver.update(CURRENT_APN_URI, values, null, null);
    }

    public void checkAPN() {
        // 檢查當前連接的APN
        Cursor cr = context.getContentResolver().query(APN_URI, null, null, null, null);
        while (cr != null && cr.moveToNext()) {
            if (cr.getString(cr.getColumnIndex("apn")).equals("dlums.ln")) {
                Log.d("TAG", "apn: " + cr.getString(cr.getColumnIndex("apn")));

                int apnId = cr.getShort(cr.getColumnIndex("_id"));
                APN.hasAPN = true;
                Log.d("TAG", "id: " +apnId);
                setDefaultApn(apnId);
                break;
            }
        }
    }


    /**
     * 根據apnId將設置的APN選中
     *
     * @param apnId
     * @return
     */
    public boolean setDefaultApn(int apnId) {
        boolean res = false;
        Log.d("TAG","setDefaultApn:" + apnId);
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", apnId);

        try {
            resolver.update(preferapn, values, null, null);
            Cursor c = resolver.query(preferapn, new String[]{"name",
                    "apn"}, "_id=" + apnId, null, null);
            if (c != null) {
                res = true;
                c.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return res;
    }
}

使用的時候直接如下:

      APN apn = new APN(this);
        apn.checkAPN();
        if (!APN.hasAPN) {
            apn.SetAPN(apn.addAPN());
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章