写了一个关于网络设置的Demo程序,供学习参考。
package com.vpdn; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; import android.telephony.TelephonyManager; import android.util.Log; /** * 手机网络设置工具类 */ public class arcVPDN { private static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers"); private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); private Context gContext= null; private int gDefaultApn= -1; public arcVPDN(Context _context) { gContext= _context; gDefaultApn= Default( ); } /** * 根据手机网络名称判断是否存在 * @param _name * @return */ public boolean Exisit(String _name) { Cursor mCursor = gContext.getContentResolver().query(APN_TABLE_URI, null, null, null, null); while(mCursor != null && mCursor.moveToNext()) { String name = mCursor.getString(mCursor.getColumnIndex("name")); if(_name.equals(name)) return true; } return false; } /** * 添加vpdn * @param _name * @param _apn * @param _user * @param _pwd */ public void AddApn(String _name, String _apn, String _user, String _pwd) { ContentResolver resolver = gContext.getContentResolver( ); ContentValues values = new ContentValues( ); values.put("name", _name); values.put("apn", _apn); TelephonyManager tm = (TelephonyManager)gContext.getSystemService(gContext.TELEPHONY_SERVICE); String numeric = tm.getSimOperator(); String mcc = numeric.substring(0, 3); String mnc = numeric.substring(3, numeric.length()); String SimOperator = tm.getSimOperator(); values.put("user", _user); values.put("password", _pwd); values.put("mcc", mcc); values.put("mnc", mnc); values.put("numeric", SimOperator); Uri newRow = resolver.insert(APN_TABLE_URI, values); } /** * 根据网络名称返回该网络编号 * @param _name * @return */ public int GetApn(String _name) { Cursor mCursor = gContext.getContentResolver().query(APN_TABLE_URI, null, null, null, null); while(mCursor != null && mCursor.moveToNext()) { int apnId = mCursor.getShort(mCursor.getColumnIndex("_id")); String name = mCursor.getString(mCursor.getColumnIndex("name")); if(_name.equals(name)) return apnId; } return -1; } /** * 获取默认网络的编号 * @return */ public int Default( ) { ApnNode apnNode = new ApnNode(); Cursor mCursor = gContext.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if(mCursor == null)return -1; int apnid= -1; while (mCursor != null && mCursor.moveToNext()) { String id = mCursor.getString(mCursor.getColumnIndex("_id")); apnid= Integer.valueOf(id); } return apnid; } /** * 获取默认网络名称 * @return */ public String DefaultAPNName() { ApnNode apnNode = new ApnNode(); Cursor mCursor = gContext.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if(mCursor == null)return ""; while (mCursor != null && mCursor.moveToNext()) { String name = mCursor.getString(mCursor.getColumnIndex("name")); return name; } return ""; } /** * 根据编号设置网络 * @param _apnid */ public void SetDefault(int _apnid) { if(_apnid==gDefaultApn)return; ContentResolver resolver = gContext.getContentResolver(); ContentValues values = new ContentValues(); values.put("apn_id", _apnid); resolver.update(PREFERRED_APN_URI, values, null, null); } public void Release( ) { if(gDefaultApn!=-1) { ContentResolver resolver = gContext.getContentResolver(); ContentValues values = new ContentValues(); values.put("apn_id", gDefaultApn); resolver.update(PREFERRED_APN_URI, values, null, null); } } }
package com.vpdn; public class ApnNode { private String name ; private String apn ; private String proxy ; private String server; private String port; private String user; private String password; private String mmsc; private String mmsproxy; private String mmsport; private String mcc; private String mnc; private String numeric; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getApn() { return apn; } public void setApn(String apn) { this.apn = apn; } public String getProxy() { return proxy; } public void setProxy(String proxy) { this.proxy = proxy; } public String getServer() { return server; } public void setServer(String server) { this.server = server; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMmsc() { return mmsc; } public void setMmsc(String mmsc) { this.mmsc = mmsc; } public String getMmsproxy() { return mmsproxy; } public void setMmsproxy(String mmsproxy) { this.mmsproxy = mmsproxy; } public String getMmsport() { return mmsport; } public void setMmsport(String mmsport) { this.mmsport = mmsport; } public String getMcc() { return mcc; } public void setMcc(String mcc) { this.mcc = mcc; } public String getMnc() { return mnc; } public void setMnc(String mnc) { this.mnc = mnc; } public String getNumeric() { return numeric; } public void setNumeric(String numeric) { this.numeric = numeric; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
package com.vpdn; import android.app.Activity; import android.util.Log; import android.widget.Button; public class vpdn extends Activity { private String Tag = "vpdn"; private arcVPDN aVPDN; private int apnid = -1; private Button vpdnButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); vpdnButton = (Button) this.findViewById(R.id.vpdn); aVPDN = new arcVPDN(this); Log.d(Tag, aVPDN.DefaultAPNName()); Log.d(Tag, aVPDN.GetApn("中国电信互联网设置CTNET")+""); if(!aVPDN.DefaultAPNName().equals("IGRPVPDN")) { vpdnButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //Login(); if(!aVPDN.Exisit("网络名称")){ //调用vpdn设置界面 startActivityForResult(new Intent(android.provider.Settings.ACTION_APN_SETTINGS), 0); } apnid= aVPDN.GetApn("网络名称"); aVPDN.SetDefault(apnid); }}); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/vpdn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name"/> </LinearLayout>