Android自定义移动网络接入点

1.点击“Network”将显示本机的无线及网络信息。

2.点击"WAP"将自动设接入点为CMWAP。

3.点击“GPRS”将自动设接入点为GPRS。

 

 

[java]  view plain copy
  1. package lab.sodino.network;  
  2. import java.net.InetAddress;  
  3. import java.net.NetworkInterface;  
  4. import java.net.SocketException;  
  5. import java.net.UnknownHostException;  
  6. import java.util.Enumeration;  
  7. import android.app.Activity;  
  8. import android.content.ContentResolver;  
  9. import android.content.ContentValues;  
  10. import android.content.Context;  
  11. import android.database.Cursor;  
  12. import android.net.ConnectivityManager;  
  13. import android.net.NetworkInfo;  
  14. import android.net.Uri;  
  15. import android.net.wifi.WifiInfo;  
  16. import android.net.wifi.WifiManager;  
  17. import android.os.Bundle;  
  18. import android.util.Log;  
  19. import android.view.Menu;  
  20. import android.view.MenuItem;  
  21. import android.view.View;  
  22. import android.view.View.OnClickListener;  
  23. import android.widget.Button;  
  24. import android.widget.TextView;  
  25. /** 
  26.  * 1.点击"Network"将输出本机所处的网络环境。 2.点击"WAP"将设定 移动网络接入点为CMWAP。 3.点击"GPRS"将设定 
  27.  * 移动网络接入点为CMNET。 注:自定义移动网络接入点的前提是“设置”→“无线和网络”→“移动网络”处已打勾。 
  28.  */  
  29. public class NetworkAct extends Activity {  
  30.     /** 全部的APN */  
  31.     private static final Uri ALL_APN_URI = Uri  
  32.             .parse("content://telephony/carriers");  
  33.     /** 当前default的APN记录。 */  
  34.     private static final Uri PREFERRED_APN_URI = Uri  
  35.             .parse("content://telephony/carriers/preferapn");  
  36.     private TextView textView;  
  37.     private Button btnShowNetInfo;  
  38.     private Button btnSetCMWAP;  
  39.     private Button btnSetGPRS;  
  40.     private BtnClickListener btnListener;  
  41.     private ContentValues cvWAP;  
  42.     private ContentValues cvGPRS;  
  43.     public void onCreate(Bundle savedInstanceState) {  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.main);  
  46.         textView = (TextView) findViewById(R.id.infoPanel);  
  47.         textView.setBackgroundColor(0xffffffff);  
  48.         textView.setTextColor(0xff0000ff);  
  49.         textView.setTextSize(15.0f);  
  50.         textView.setScrollBarStyle(TextView.SCROLLBARS_OUTSIDE_OVERLAY);  
  51.         btnListener = new BtnClickListener();  
  52.         btnShowNetInfo = (Button) findViewById(R.id.showInfo);  
  53.         btnShowNetInfo.setOnClickListener(btnListener);  
  54.         btnSetCMWAP = (Button) findViewById(R.id.setCMWAP);  
  55.         btnSetCMWAP.setOnClickListener(btnListener);  
  56.         btnSetGPRS = (Button) findViewById(R.id.setGPRS);  
  57.         btnSetGPRS.setOnClickListener(btnListener);  
  58.         initAPNValues();  
  59.     }  
  60.     private void initAPNValues() {  
  61.         cvWAP = new ContentValues();  
  62.         cvWAP.put("name""移动梦网");  
  63.         cvWAP.put("apn""cmwap");  
  64.         // 需要设置为默认接入点则为default  
  65.         cvWAP.put("type""default");  
  66.         cvWAP.put("proxy""10.0.0.172");  
  67.         cvWAP.put("port""80");  
  68.         cvWAP.put("mmsproxy""10.0.0.172");  
  69.         cvWAP.put("mmsport""80");  
  70.         cvWAP.put("mmsprotocol""2.0");  
  71.         cvWAP.put("mmsc""http://mmsc.monternet.com");  
  72.         cvWAP.put("mcc""460");  
  73.         cvWAP.put("mnc""02");  
  74.         cvWAP.put("numeric""46002");  
  75.         cvGPRS = new ContentValues();  
  76.         cvGPRS.put("name""GPRS");  
  77.         cvGPRS.put("apn""cmnet");  
  78.         // 需要设置为默认接入点则为default  
  79.         cvGPRS.put("type""default");  
  80.         // cvGPRS.put("proxy", "10.0.0.172");  
  81.         // cvGPRS.put("port", "80");  
  82.         // cvGPRS.put("mmsproxy", "10.0.0.172");  
  83.         // cvGPRS.put("mmsport", "80");  
  84.         cvGPRS.put("mmsprotocol""2.0");  
  85.         // cvGPRS.put("mmsc", "http://mmsc.monternet.com");  
  86.         cvGPRS.put("mcc""460");  
  87.         cvGPRS.put("mnc""02");  
  88.         cvGPRS.put("numeric""46002");  
  89.     }  
  90.     private void showNetworkInfo() {  
  91.         getLocalAddress();  
  92.         getWifiAddress();  
  93.         getNetworkInfo();  
  94.         textView.append("/nList Default Access Point Name:/n");  
  95.         listAllAPNs(PREFERRED_APN_URI);  
  96.         textView.append("/nList all Access Point Name:/n");  
  97.         listAllAPNs(ALL_APN_URI);  
  98.     }  
  99.     private void getLocalAddress() {  
  100.         InetAddress iAdd = null;  
  101.         try {  
  102.             iAdd = InetAddress.getLocalHost();  
  103.             String line = "";  
  104.             // line = "HostName=" + iAdd.getHostName() + "/n";  
  105.             // textView.append(line);  
  106.             // line = "CanonicalHostName=" + iAdd.getCanonicalHostName() + "/n";  
  107.             // textView.append(line);  
  108.             // line = "HostAddress=" + iAdd.getHostAddress() + "/n";  
  109.             // textView.append(line);  
  110.             // textView.append("/n");  
  111.             String hostName = iAdd.getHostName();  
  112.             if (hostName != null) {  
  113.                 InetAddress[] adds = null;  
  114.                 adds = InetAddress.getAllByName(hostName);  
  115.                 if (adds != null) {  
  116.                     for (int i = 0; i < adds.length; i++) {  
  117.                         iAdd = adds[i];  
  118.                         line = "HostName=" + iAdd.getHostName() + "/n";  
  119.                         textView.append(line);  
  120.                         line = "CanonicalHostName="  
  121.                                 + iAdd.getCanonicalHostName() + "/n";  
  122.                         textView.append(line);  
  123.                         line = "HostAddress=" + iAdd.getHostAddress() + "/n";  
  124.                         textView.append(line);  
  125.                         textView.append("/n");  
  126.                     }  
  127.                 }  
  128.             }  
  129.         } catch (UnknownHostException e1) {  
  130.             e1.printStackTrace();  
  131.         }  
  132.         try {  
  133.             for (Enumeration<NetworkInterface> en = NetworkInterface  
  134.                     .getNetworkInterfaces(); en.hasMoreElements();) {  
  135.                 NetworkInterface intf = en.nextElement();  
  136.                 for (Enumeration<InetAddress> enumIpAddr = intf  
  137.                         .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  138.                     InetAddress inetAddress = enumIpAddr.nextElement();  
  139.                     // if (!inetAddress.isLoopbackAddress()) {  
  140.                     textView.append("HostAddress="  
  141.                             + inetAddress.getHostAddress() + "/n");  
  142.                     // }  
  143.                 }  
  144.             }  
  145.         } catch (SocketException ex) {  
  146.             Log.e("WifiPreference IpAddress", ex.toString());  
  147.         }  
  148.     }  
  149.     private void getWifiAddress() {  
  150.         WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  151.         WifiInfo info = wifi.getConnectionInfo();  
  152.         textView.append("HiddenSSID=" + info.getHiddenSSID() + "/n");  
  153.         textView.append("IpAddress=" + formatIP4(info.getIpAddress()) + "/n");  
  154.         textView.append("LinkSpeed=" + info.getLinkSpeed() + "/n");  
  155.         textView.append("NetworkId=" + info.getNetworkId() + "/n");  
  156.         textView.append("Rssi=" + info.getRssi() + "/n");  
  157.         textView.append("SSID=" + info.getSSID() + "/n");  
  158.         textView.append("MacAddress=" + info.getMacAddress() + "/n");  
  159.     }  
  160.     private void getNetworkInfo() {  
  161.         // 此处输出可用网络类型  
  162.         ConnectivityManager mag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  163.         textView.append("/nActive:/n");  
  164.         NetworkInfo info = mag.getActiveNetworkInfo();  
  165.         if (info != null) {  
  166.             textView.append("ExtraInfo=" + info.getExtraInfo() + "/n");  
  167.             textView.append("SubtypeName=" + info.getSubtypeName() + "/n");  
  168.             textView.append("TypeName=" + info.getTypeName() + "/n");  
  169.             textView.append("Type=" + info.getType() + "/n");  
  170.         }  
  171.         textView.append("/nWifi:/n");  
  172.         NetworkInfo wifiInfo = mag  
  173.                 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
  174.         textView.append("ExtraInfo=" + wifiInfo.getExtraInfo() + "/n");  
  175.         textView.append("SubtypeName=" + wifiInfo.getSubtypeName() + "/n");  
  176.         textView.append("TypeName=" + wifiInfo.getTypeName() + "/n");  
  177.         textView.append("Type=" + wifiInfo.getType() + "/n");  
  178.         NetworkInfo mobInfo = mag  
  179.                 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  180.         textView.append("/nMobile:/n");  
  181.         textView.append("ExtraInfo=" + mobInfo.getExtraInfo() + "/n");  
  182.         textView.append("SubtypeName=" + mobInfo.getSubtypeName() + "/n");  
  183.         textView.append("TypeName=" + mobInfo.getTypeName() + "/n");  
  184.         textView.append("Type=" + mobInfo.getType() + "/n");  
  185.     }  
  186.     private void listAllAPNs(Uri apnUri) {  
  187.         ContentResolver contentResolver = getContentResolver();  
  188.         Cursor cursor = contentResolver.query(apnUri, nullnullnullnull);  
  189.         if (cursor != null) {  
  190.             String temp = "Count=" + cursor.getCount() + " ColumnCount="  
  191.                     + cursor.getColumnCount() + "/n";  
  192.             textView.append(temp);  
  193.             String key = "";  
  194.             while (cursor.moveToNext()) {  
  195.                 key = "position";  
  196.                 int position = cursor.getPosition();  
  197.                 textView.append("/n" + key + "=" + String.valueOf(position)  
  198.                         + "/n");  
  199.                 key = "_id";  
  200.                 int id = cursor.getShort(cursor.getColumnIndex(key));  
  201.                 textView.append(key + "=" + String.valueOf(id) + "/n");  
  202.                 appendDBColumn(cursor, "name");  
  203.                 appendDBColumn(cursor, "apn");  
  204.                 appendDBColumn(cursor, "type");  
  205.                 appendDBColumn(cursor, "proxy");  
  206.                 appendDBColumn(cursor, "port");  
  207.                 appendDBColumn(cursor, "mmsproxy");  
  208.                 appendDBColumn(cursor, "mmsport");  
  209.                 appendDBColumn(cursor, "mmsprotocol");  
  210.                 appendDBColumn(cursor, "mmsc");  
  211.                 appendDBColumn(cursor, "current");  
  212.                 appendDBColumn(cursor, "mcc");  
  213.                 appendDBColumn(cursor, "mnc");  
  214.                 appendDBColumn(cursor, "numeric");  
  215.             }  
  216.         }  
  217.     }  
  218.     private void appendDBColumn(Cursor cursor, String key) {  
  219.         try {  
  220.             String value = cursor.getString(cursor.getColumnIndex(key));  
  221.             textView.append(key + "=" + value + "/n");  
  222.         } catch (Exception e) {  
  223.             System.out.println("[sodino] " + e);  
  224.         }  
  225.     }  
  226.     private void setNetworkFeature() {  
  227.         // 经测试,start和stop都无效。  
  228.         ConnectivityManager connectivityMag = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  229.         int stop = connectivityMag.stopUsingNetworkFeature(  
  230.                 ConnectivityManager.TYPE_WIFI, "*");  
  231.         textView.append("stop=" + String.valueOf(stop) + "/n");  
  232.         int start = connectivityMag.startUsingNetworkFeature(  
  233.                 ConnectivityManager.TYPE_MOBILE,  
  234.                 ConnectivityManager.EXTRA_NETWORK_INFO);  
  235.         textView.append("start=" + String.valueOf(start) + "/n");  
  236.     }  
  237.     private void setDefaultAPN(ContentValues value) {  
  238.         int _id = findAPNId(value);  
  239.         if (_id == -1) {  
  240.             _id = insertAPN(value);  
  241.         }  
  242.         textView.append(value.get("name") + " _id=" + _id + "/n");  
  243.         ContentValues values = new ContentValues();  
  244.         values.put("apn_id", _id);  
  245.         ContentResolver resolver = getContentResolver();  
  246.         int updateRow = resolver.update(PREFERRED_APN_URI, values, nullnull);  
  247.         textView.append("updateRow=" + updateRow + "/n");  
  248.         textView.append("Set " + value.get("name")  
  249.                 + " as default netwrok successed!!/n");  
  250.     }  
  251.     private int findAPNId(ContentValues cv) {  
  252.         int id = -1;  
  253.         ContentResolver contentResolver = getContentResolver();  
  254.         Cursor cursor = contentResolver.query(ALL_APN_URI, nullnullnull,  
  255.                 null);  
  256.         if (cursor != null) {  
  257.             while (cursor.moveToNext()) {  
  258.                 if (cursor.getString(cursor.getColumnIndex("name")).equals(  
  259.                         cv.get("name"))  
  260.                         && cursor.getString(cursor.getColumnIndex("apn"))  
  261.                                 .equals(cv.get("apn"))  
  262.                         && cursor.getString(cursor.getColumnIndex("numeric"))  
  263.                                 .equals(cv.get("numeric"))) {  
  264.                     id = cursor.getShort(cursor.getColumnIndex("_id"));  
  265.                     break;  
  266.                 }  
  267.             }  
  268.         }  
  269.         return id;  
  270.     }  
  271.     private int insertAPN(ContentValues value) {  
  272.         int apn_Id = -1;  
  273.         ContentResolver resolver = getContentResolver();  
  274.         Uri newRow = resolver.insert(ALL_APN_URI, value);  
  275.         if (newRow != null) {  
  276.             Cursor cursor = resolver.query(newRow,null,null,null,null);  
  277.             int idIdx = cursor.getColumnIndex("_id");  
  278.             cursor.moveToFirst();  
  279.             apn_Id = cursor.getShort(idIdx);  
  280.             System.out.println("[sodino] Insert New id:" + apn_Id);  
  281.         }  
  282.         return apn_Id;  
  283.     }  
  284.     public boolean onCreateOptionsMenu(Menu menu) {  
  285.         menu.add("finish");  
  286.         return true;  
  287.     }  
  288.     public boolean onOptionsItemSelected(MenuItem item) {  
  289.         if (item.getTitle().equals("finish")) {  
  290.             finish();  
  291.         }  
  292.         return false;  
  293.     }  
  294.     /** 将10进制整数形式转换成127.0.0.1形式的IP地址 */  
  295.     private static String formatIP4(long longIP) {  
  296.         StringBuffer sb = new StringBuffer("");  
  297.         sb.append(String.valueOf(longIP >>> 24));  
  298.         sb.append(".");  
  299.         sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16));  
  300.         sb.append(".");  
  301.         sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8));  
  302.         sb.append(".");  
  303.         sb.append(String.valueOf(longIP & 0x000000FF));  
  304.         return sb.toString();  
  305.     }  
  306.     private class BtnClickListener implements OnClickListener {  
  307.         public void onClick(View v) {  
  308.             textView.setText("");  
  309.             if (v == btnShowNetInfo) {  
  310.                 showNetworkInfo();  
  311.             } else if (v == btnSetCMWAP) {  
  312.                 setDefaultAPN(cvWAP);  
  313.                 btnSetCMWAP.setEnabled(false);  
  314.                 btnSetGPRS.setEnabled(true);  
  315.             } else if (v == btnSetGPRS) {  
  316.                 setDefaultAPN(cvGPRS);  
  317.                 btnSetGPRS.setEnabled(false);  
  318.                 btnSetCMWAP.setEnabled(true);  
  319.             }  
  320.         }  
  321.     }  
  322. }  

 

 

 

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal">  
  11.         <Button  
  12.             android:id="@+id/showInfo"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_weight="2"  
  16.             android:text="Network"  
  17.         ></Button>  
  18.         <Button  
  19.             android:id="@+id/setCMWAP"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_weight="1"  
  23.             android:text="WAP"  
  24.         ></Button>  
  25.         <Button  
  26.             android:id="@+id/setGPRS"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_weight="1"  
  30.             android:text="GPRS"  
  31.         ></Button>  
  32.     </LinearLayout>  
  33.     <ScrollView  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content">  
  36.         <TextView  
  37.             android:id="@+id/infoPanel"    
  38.             android:layout_width="fill_parent"   
  39.             android:layout_height="wrap_content"  
  40.         />  
  41.     </ScrollView>  
  42. </LinearLayout>  

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="lab.sodino.network" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:label="@string/app_name" android:name=".NetworkAct">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.     </application>  
  13.     <uses-sdk android:minSdkVersion="4" />  
  14.     <!-- 查看网络信息时需要以下权限 -->  
  15.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
  16.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  
  17.     <!-- 设置APN时需要上面的ACCESS_NETWORK_STATE及以下权限 -->  
  18.     <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>  
  19.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  20.     <!-- 设置网络类型时将要使用 -->  
  21.     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>  
  22. </manifest>   

 

 

界面如下:

Network    WAP   GPRS

本文内容归CSDN博客博主Sodino 所有

转载请注明出处: http://blog.csdn.net/sodino/archive/2010/09/30/5916641.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值