添加APN网络接入点,设置指定网络为默认网络

0
网上看了很多资料,再加上自己的理解和探索,终于实现了添加接入点设置和将当前网络设置为自己指定的网络接入点。废话不多说,上源码,大家分享下:(在moto-xt800,CDMA下进行了测试)

1.准备工作:

AndroidManifest。xml文件里添加以下三个权限设置:
1<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
2  
3<uses-permission android:name="android.permission.INTERNET"></uses-permission>
4  
5<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

开始上代码了,很期待吧?

2.新建一个类ApnNode,算是属性设置吧
001public class ApnNode {
002  
003  private   String name ;
004  
005     private  String apn ;
006  
007     private String proxy ;
008  
009     private  String port;
010      
011     private  String user;
012      
013     private  String server;
014      
015     private  String password;
016      
017     private  String mmsc;
018  
019     private  String mmsproxy;
020  
021     private  String mmsport; 
022  
023     private  String mcc;
024  
025     private  String mnc;
026  
027     private  String numeric;
028      
029     private String type;
030  
031     /**
032      * @return the name
033      */
034     public String getName() {
035         return name;
036     }
037  
038     /**
039      * @param name the name to set
040      */
041     public void setName(String name) {
042         this.name = name;
043     }
044  
045     /**
046      * @return the apn
047      */
048     public String getApn() {
049         return apn;
050     }
051  
052     /**
053      * @param apn the apn to set
054      */
055     public void setApn(String apn) {
056         this.apn = apn;
057     }
058  
059     /**
060      * @return the proxy
061      */
062     public String getProxy() {
063         return proxy;
064     }
065  
066     /**
067      * @param proxy the proxy to set
068      */
069     public void setProxy(String proxy) {
070         this.proxy = proxy;
071     }
072  
073     /**
074      * @return the port
075      */
076     public String getPort() {
077         return port;
078     }
079  
080     /**
081      * @param port the port to set
082      */
083     public void setPort(String port) {
084         this.port = port;
085     }
086  
087     /**
088      * @return the user
089      */
090     public String getUser() {
091         return user;
092     }
093  
094     /**
095      * @param user the user to set
096      */
097     public void setUser(String user) {
098         this.user = user;
099     }
100  
101     /**
102      * @return the server
103      */
104     public String getServer() {
105         return server;
106     }
107  
108     /**
109      * @param server the server to set
110      */
111     public void setServer(String server) {
112         this.server = server;
113     }
114  
115     /**
116      * @return the password
117      */
118     public String getPassword() {
119         return password;
120     }
121  
122     /**
123      * @param password the password to set
124      */
125     public void setPassword(String password) {
126         this.password = password;
127     }
128  
129     /**
130      * @return the mmsc
131      */
132     public String getMmsc() {
133         return mmsc;
134     }
135  
136     /**
137      * @param mmsc the mmsc to set
138      */
139     public void setMmsc(String mmsc) {
140         this.mmsc = mmsc;
141     }
142  
143     /**
144      * @return the mmsproxy
145      */
146     public String getMmsproxy() {
147         return mmsproxy;
148     }
149  
150     /**
151      * @param mmsproxy the mmsproxy to set
152      */
153     public void setMmsproxy(String mmsproxy) {
154         this.mmsproxy = mmsproxy;
155     }
156  
157     /**
158      * @return the mmsport
159      */
160     public String getMmsport() {
161         return mmsport;
162     }
163  
164     /**
165      * @param mmsport the mmsport to set
166      */
167     public void setMmsport(String mmsport) {
168         this.mmsport = mmsport;
169     }
170  
171     /**
172      * @return the mcc
173      */
174     public String getMcc() {
175         return mcc;
176     }
177  
178     /**
179      * @param mcc the mcc to set
180      */
181     public void setMcc(String mcc) {
182         this.mcc = mcc;
183     }
184  
185     /**
186      * @return the mnc
187      */
188     public String getMnc() {
189         return mnc;
190     }
191  
192     /**
193      * @param mnc the mnc to set
194      */
195     public void setMnc(String mnc) {
196         this.mnc = mnc;
197     }
198  
199     /**
200      * @return the numeric
201      */
202     public String getNumeric() {
203         return numeric;
204     }
205  
206     /**
207      * @param numeric the numeric to set
208      */
209     public void setNumeric(String numeric) {
210         this.numeric = numeric;
211     }
212  
213     /**
214      * @return the type
215      */
216     public String getType() {
217         return type;
218     }
219  
220     /**
221      * @param type the type to set
222      */
223     public void setType(String type) {
224         this.type = type;
225     }
226}

如果里面的函数名不清楚的话,网上找APN设置,一大堆的资料

3.主界面里的代码为:
001public class ApnNew extends Activity {
002 private Button setbtn;
003 private Button insertbtn;
004 private Button apnexist;
005  
006 @Override
007 public void onCreate(Bundle savedInstanceState){
008  super.onCreate(savedInstanceState);
009  setContentView(R.layout.apnnew);
010  setbtn = (Button)this.findViewById(R.id.setApn);
011  setbtn.setOnClickListener(setlis);
012  insertbtn = (Button)this.findViewById(R.id.insertApn);
013  insertbtn.setOnClickListener(insertlis);
014  apnexist = (Button)this.findViewById(R.id.Apnexist);
015  apnexist.setOnClickListener(existlis);
016 }
017   
018 private OnClickListener setlis = new OnClickListener(){
019  
020  @Override
021  public void onClick(View v) {
022   setCmwapAPN();
023     
024  }
025    
026 };
027   
028 private OnClickListener insertlis = new OnClickListener(){
029  
030  @Override
031  public void onClick(View v) {
032   InsetAPN();
033   if(apnd_id != 0)
034    Toast.makeText(ApnNew.this, String.valueOf(apnd_id), Toast.LENGTH_SHORT).show();
035   else
036    Toast.makeText(ApnNew.this, "insertAPN is unsuccess", Toast.LENGTH_SHORT).show();
037     
038  }
039    
040 };
041   
042 private OnClickListener existlis = new OnClickListener(){
043  
044  @Override
045  public void onClick(View v) {
046   int ri = checkAPN();
047   if(ri == -1)
048    Toast.makeText(ApnNew.this, "不存在此APN", Toast.LENGTH_SHORT).show();
049   else
050    Toast.makeText(ApnNew.this, "存在此APN", Toast.LENGTH_SHORT).show();
051     
052  }
053    
054 };
055   
056  /**
057     * 入口
058     * 设置指定的APN接入点
059     * */
060    public void setCmwapAPN()
061 {
062  try
063  {
064   if (!isCmwap())
065   {
066    int net_type = getNetWorkType();
067    m_oldNetWorkType = net_type;
068    if ((phoneSettedApnID = checkAPN()) != -1)
069    {
070     SetDefaultAPN(phoneSettedApnID);
071    }
072    else
073    {
074     InsetAPN();
075     SetDefaultAPN(apnd_id);
076    }
077  
078    Thread.sleep(8000L);
079  
080  
081   }
082  }
083  catch (Exception e)
084  {
085   Log.i("setCmwapAPN error", e.getMessage());
086  }
087 }
088    private int apnd_id = 0;
089    int m_oldApnId = -1;
090 int m_oldNetWorkType = -1;
091    private int phoneSettedApnID = -1;//当前APNid
092    private static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
093  
094 private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
095    private String getMCC()
096 {
097  TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
098  String numeric = tm.getSimOperator();
099  String mcc = numeric.substring(0, 3);
100  Log.i("MCC  is", mcc);
101  return mcc;
102 }
103  
104 private String getMNC()
105 {
106  TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
107  String numeric = tm.getSimOperator();
108  String mnc = numeric.substring(3, numeric.length());
109  Log.i("MNC  is", mnc);
110  return mnc;
111 }
112 private String getSimOperator()
113 {
114  TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
115  String SimOperator = tm.getSimOperator();
116  return SimOperator;
117 }
118 public ApnNode getDefaultAPN()
119 {
120  String id = "";
121  String apn = "";
122  String proxy = "";
123  String name = "";
124  String port = "";
125  String type = "";
126  String mcc = "";
127  String mnc = "";
128  String numeric = "";
129  ApnNode apnNode = new ApnNode();
130  //        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
131  Cursor mCursor = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
132  if (mCursor == null)
133  {
134   // throw new Exception("Non prefer apn exist");
135   return null;
136  
137  }
138  while (mCursor != null && mCursor.moveToNext())
139  {
140   id = mCursor.getString(mCursor.getColumnIndex("_id"));
141   name = mCursor.getString(mCursor.getColumnIndex("name"));
142   apn = mCursor.getString(mCursor.getColumnIndex("apn")).toLowerCase();
143   proxy = mCursor.getString(mCursor.getColumnIndex("proxy"));
144   port = mCursor.getString(mCursor.getColumnIndex("port"));
145   mcc = mCursor.getString(mCursor.getColumnIndex("mcc"));
146   mnc = mCursor.getString(mCursor.getColumnIndex("mnc"));
147   numeric = mCursor.getString(mCursor.getColumnIndex("numeric"));
148   Log.d("getDefaultAPN", "default Apn info:" + id + "_" + name + "_" + apn + "_" + proxy + "_" + proxy);
149  
150  }
151  phoneSettedApnID = Integer.valueOf(id);
152  apnNode.setName(name);
153  apnNode.setApn(apn);
154  apnNode.setProxy(proxy);
155  apnNode.setPort(port);
156  apnNode.setMcc(mcc);
157  apnNode.setMnc(mnc);
158  apnNode.setNumeric(numeric);
159  return apnNode;
160 }
161    public boolean isCurretApn()
162 {
163  ApnNode apnNode = new ApnNode();
164  apnNode.setName("中国电信CTNET");
165  apnNode.setApn("ctnet");
166  apnNode.setProxy("10.0.0.200");
167  apnNode.setPort("80");
168  apnNode.setMcc(getMCC());
169  apnNode.setMnc(getMNC());
170  apnNode.setNumeric(getSimOperator());
171  ApnNode checkApn = getDefaultAPN();
172  if ((apnNode.getApn().equals(checkApn.getApn()) && apnNode.getMcc().equals(checkApn.getMcc()) && apnNode.getMnc().equals(checkApn.getMnc()) && apnNode.getNumeric().equals(
173    checkApn.getNumeric()))
174    && (checkApn.getType() == null || "default".equals(checkApn.getType()) || "".equals(checkApn.getType())))//|| (apnNode.getApn().equals(checkApn.getApn()) && checkApn.getProxy().equals("") && checkApn.getPort().equals(""))
175  {
176   return true;
177  }
178  m_oldApnId = phoneSettedApnID;
179  return false;
180 }
181    /**
182  * 获得网络连接管理
183  *
184  * @return
185  */
186 private ConnectivityManager getConnectManager()
187 {
188  
189  ConnectivityManager m_ConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
190  
191  return m_ConnectivityManager;
192 }
193    /**
194  * 获得当前联网类型wifi or mobile
195  *
196  * @return
197  */
198 private int getNetWorkType()
199 {
200  if (getConnectManager() != null)
201  {
202   NetworkInfo networkInfo = getConnectManager().getActiveNetworkInfo();
203   if (networkInfo != null)
204    return networkInfo.getType();
205   return -1;
206  }
207  else
208  {
209   return -1;
210  }
211 }
212    private boolean isCmwap()
213 {
214  int net_type = getNetWorkType();
215  if (net_type == ConnectivityManager.TYPE_MOBILE)
216  {
217   return isCurretApn();
218  }
219  else if (net_type == ConnectivityManager.TYPE_WIFI)
220  {
221   return false;
222  }
223  return false;
224 }
225    public boolean setDefaultApn(int apnId)
226 {
227  boolean res = false;
228  ContentResolver resolver = getContentResolver();
229  ContentValues values = new ContentValues();
230  values.put("apn_id", apnId);
231   
232  
233  try
234  {
235   resolver.update(PREFERRED_APN_URI, values, null, null);
236   Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name", "apn" }, "_id=" + apnId, null, null);
237   if (c != null)
238   {
239    res = true;
240    c.close();
241   }
242  }
243  catch (SQLException e)
244  {
245    
246  }
247  return res;
248  
249 }
250  
251    public void SetDefaultAPN(int id)
252 {
253  setDefaultApn(id);
254  ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
255  cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "*");
256  Cursor cursor = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
257  int rows = cursor.getCount();
258  cursor.moveToFirst();
259  String apn;
260  for (int i = 0; i < rows; i++)
261  {
262   apn = cursor.getString(1);
263   Log.e("----------------", apn);
264   cursor.moveToNext();
265  }
266  
267 }
268    //检查指定APN是否存在
269    public int checkAPN()
270 {
271  ApnNode checkApn = new ApnNode();
272  checkApn.setName("中国电信ctnet");
273  checkApn.setApn("ctnet");
274  checkApn.setMcc(getMCC());
275  checkApn.setMnc(getMNC());
276  checkApn.setNumeric(getSimOperator());
277  return isApnExisted(checkApn);
278 }
279    public int isApnExisted(ApnNode apnNode)
280 {
281  
282  int apnId = -1;
283  //        Uri uri = Uri.parse("content://telephony/carriers/");
284  Cursor mCursor = getContentResolver().query(APN_TABLE_URI, null, null, null, null);
285  while (mCursor != null && mCursor.moveToNext())
286  {
287   apnId = mCursor.getShort(mCursor.getColumnIndex("_id"));
288   String name = mCursor.getString(mCursor.getColumnIndex("name"));
289   String apn = mCursor.getString(mCursor.getColumnIndex("apn"));
290   String type = mCursor.getString(mCursor.getColumnIndex("type"));
291   String proxy = mCursor.getString(mCursor.getColumnIndex("proxy"));
292   String port = mCursor.getString(mCursor.getColumnIndex("port"));
293   String current = mCursor.getString(mCursor.getColumnIndex("current"));
294   String mcc = mCursor.getString(mCursor.getColumnIndex("mcc"));
295   String mnc = mCursor.getString(mCursor.getColumnIndex("mnc"));
296   String numeric = mCursor.getString(mCursor.getColumnIndex("numeric"));
297   Log.e("isApnExisted", "info:" + apnId + "_" + name + "_" + apn + "_" + type + "_" + current + "_" + proxy);
298   if (/*apnNode.getName().equals(name)*/(apnNode.getApn().equals(apn) && apnNode.getMcc().equals(mcc) && apnNode.getMnc().equals(mnc) && apnNode.getNumeric().equals(numeric))
299     && (type == null || "default".equals(type) || "".equals(type)))//|| (apnNode.getApn().equals(apn) && "".equals(proxy) && "".equals(port))
300   {
301    return apnId;
302   }
303   else
304   {
305    apnId = -1;
306   }
307  
308  }
309  return apnId;
310 }
311    public int addNewApn(ApnNode apnNode)
312 {
313  int apnId = -1;
314  ContentResolver resolver = getContentResolver();
315  ContentValues values = new ContentValues();
316  values.put("name", apnNode.getName());
317  values.put("apn", apnNode.getApn());
318  values.put("proxy", apnNode.getProxy());
319  values.put("port", apnNode.getPort());
320  values.put("user", apnNode.getUser());
321  values.put("password", apnNode.getPassword());
322  values.put("mcc", apnNode.getMcc());
323  values.put("mnc", apnNode.getMnc());
324  values.put("numeric", apnNode.getNumeric());
325  // Note: this values need to be update, and for now, it only for XT800.
326  
327  Cursor c = null;
328  try
329  {
330   Uri newRow = resolver.insert(APN_TABLE_URI, values);
331   if (newRow != null)
332   {
333    c = resolver.query(newRow, null, null, null, null);
334      
335    int idindex = c.getColumnIndex("_id");
336    c.moveToFirst();
337    apnId = c.getShort(idindex);
338    Log.d("Robert", "New ID: " + apnId + ": Inserting new APN succeeded!");
339   }
340  }
341  catch (SQLException e)
342  {
343     
344  }
345  
346  if (c != null)
347   c.close();
348  
349  return apnId;
350 }
351    //添加一个APN
352    private void InsetAPN()
353 {
354  ApnNode checkApn = new ApnNode();
355  checkApn.setName("中国电信CTNET");
356  checkApn.setApn("ctnet");
357//  checkApn.setProxy("10.0.0.200");
358//  checkApn.setPort("80");
359  checkApn.setUser("card");
360  checkApn.setPassword("card");
361  checkApn.setMcc(getMCC());
362  checkApn.setMnc(getMNC());
363  checkApn.setNumeric(getSimOperator());
364  apnd_id = addNewApn(checkApn);
365 }
366}

4.主界面的xml文件为:
01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout
03android:id="@+id/widget28"
04android:layout_width="fill_parent"
05android:layout_height="fill_parent"
06android:orientation="vertical"
07xmlns:android="http://schemas.android.com/apk/res/android"
08>
09<Button
10android:id="@+id/setApn"
11android:layout_width="fill_parent"
12android:layout_height="wrap_content"
13android:text="设置Apn"
14>
15</Button>
16<Button
17android:id="@+id/insertApn"
18android:layout_width="fill_parent"
19android:layout_height="wrap_content"
20android:text="插入一个APN"
21>
22</Button>
23<Button
24android:id="@+id/Apnexist"
25android:layout_width="fill_parent"
26android:layout_height="wrap_content"
27android:text="判断指定APN是否存在"
28>
29</Button>
30</LinearLayout>

只要是开发过android的肯定清楚这些代码和界面吧,我也不多解释了,如果哥们你看不懂,那就是说明你还没弄清android开发,先去补补课再来看

文章转自:http://blog.csdn.net/napolun007/article/details/5748595
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值