《设置net》

来源:互联网 发布:金融大数据研究院 编辑:程序博客网 时间:2024/06/03 15:57

   工程需要,需要当客户无论选择的是net还是wap时,我们的程序都是走net路线,所以需要在软件中编程设置APN,方法如下:

 

//插入一个新的APN,

  public int InsertAPN(String name, String apn_addr)
   {
       int id = -1;
       ContentResolver resolver = this.getContentResolver();
       ContentValues values = new ContentValues();
       values.put("name", name);
       values.put("apn", apn_addr);
      
       /*

          下面是最重要的三个因素,当在模拟器中操作的时候

       mcc=”310”   mnc=”260”     所以numeric=”310260”

       这时候在模拟器中才回显示新增加的APN,当在真机中时,就需要将参数设置如下,(找了一天才找出来)

    */
       values.put("mcc", "460");
       values.put("mnc", "02");
       values.put("numeric", "46002");
      
       Cursor c = null;
       try
       {
           Uri newRow = resolver.insert(APN_URI, values);
           if(newRow != null)
           {
               c = resolver.query(newRow, null, null, null, null);
                                           
               // Obtain the apn id
               int idindex = c.getColumnIndex("_id");
               c.moveToFirst();
               id = c.getShort(idindex);
               Log.d(TAG, "New ID: " + id + ": Inserting new APN succeeded!");
           }
       }
       catch(SQLException e)
       {
           Log.d(TAG, e.getMessage());
       }

       if(c !=null )
           c.close();
       return id;
   }
  
  
//  Set an APN to be the default
   /*


         */
    public boolean SetDefaultAPN(int id)
    {
        boolean res = false;
        ContentResolver resolver = this.getContentResolver();
        ContentValues values = new ContentValues();
       
        //See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
        //content://telephony/carriers/preferapn URI mapping
        values.put("apn_id", id);
        try
        {
            resolver.update(CURRENT_APN_URI, values, null, null);
            Cursor c = resolver.query(
                    CURRENT_APN_URI,
                    new String[]{"name","apn"},
                    "_id="+id,
                    null,
                    null);
            if(c != null)
            {
                res = true;
                c.close();
            }
        }
        catch (SQLException e)
        {
            Log.d(TAG, e.getMessage());
        }
         return res;
    }

 

 

 

 

也可以用下面三个方法查看已经存在的APN的参数,方便自己的填写

private String getMCC() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String numeric = tm.getSimOperator();
String mcc = numeric.substring(0, 3);
Log.i("MCC is", mcc);
return mcc;
}

private String getMNC() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String numeric = tm.getSimOperator();
String mnc = numeric.substring(3, numeric.length());
Log.i("MNC is", mnc);
return mnc;
}

private String getSimOperator() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String SimOperator = tm.getSimOperator();
return SimOperator;
}

 

 

 

                   用下面的方法可以查看当前的APN设置为什么

//先读取APN的链接方式
/* ConnectivityManager conManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
String currentAPN = info.getExtraInfo();
*/

 

                wap的设置参数

     ContentValues values = new ContentValues();
values.put("name", "cmwap");
values.put("apn", "cmwap");
values.put("proxy", "10.0.0.172");

values.put("port", "80");
values.put("mmsproxy", "10.0.0.172");
values.put("mmsport", "80");
values.put("user", "");
values.put("server", "");
values.put("password", "");
values.put("mmsc", "http://mmsc.monternet.com");
values.put("type", "");
values.put("mcc", "460");
  values.put("mnc", "02");
  values.put("numeric", "46002");/* 


values.put("name", "CMNET");
values.put("apn", "cmnet");
values.put("mcc", "460");

  values.put("mnc", "02");

values.put("type", "default");
  values.put("numeric", "46002");*/

原创粉丝点击