顶
0踩
1.点击“Network”将显示本机的无线及网络信息。
2.点击"WAP"将自动设接入点为CMWAP。
3.点击“GPRS”将自动设接入点为GPRS。
代码出处:http://blog.csdn.net/sodino/article/details/5916641
标签:
AndroidSDK
cmwapGPRS
代码片段(6)
[代码][Java]代码
viewsource
print?
001 | package lab.sodino.network; |
002 | import java.net.InetAddress; |
003 | import java.net.NetworkInterface; |
004 | import java.net.SocketException; |
005 | import java.net.UnknownHostException; |
006 | import java.util.Enumeration; |
007 | import android.app.Activity; |
008 | import android.content.ContentResolver; |
009 | import android.content.ContentValues; |
010 | import android.content.Context; |
011 | import android.database.Cursor; |
012 | import android.net.ConnectivityManager; |
013 | import android.net.NetworkInfo; |
014 | import android.net.Uri; |
015 | import android.net.wifi.WifiInfo; |
016 | import android.net.wifi.WifiManager; |
017 | import android.os.Bundle; |
018 | import android.util.Log; |
019 | import android.view.Menu; |
020 | import android.view.MenuItem; |
021 | import android.view.View; |
022 | import android.view.View.OnClickListener; |
023 | import android.widget.Button; |
024 | import android.widget.TextView; |
026 | *1.点击"Network"将输出本机所处的网络环境。2.点击"WAP"将设定移动网络接入点为CMWAP。3.点击"GPRS"将设定
|
027 | *移动网络接入点为CMNET。注:自定义移动网络接入点的前提是“设置”→“无线和网络”→“移动网络”处已打勾。
|
029 | public class NetworkAct extends
Activity{ |
031 | private
static final UriALL_APN_URI=Uri
|
032 | .parse( "content://telephony/carriers" ); |
034 | private
static final UriPREFERRED_APN_URI=Uri
|
035 | .parse( "content://telephony/carriers/preferapn" ); |
036 | private
TextViewtextView; |
037 | private
ButtonbtnShowNetInfo; |
038 | private
ButtonbtnSetCMWAP; |
039 | private
ButtonbtnSetGPRS; |
040 | private
BtnClickListenerbtnListener; |
041 | private
ContentValuescvWAP; |
042 | private
ContentValuescvGPRS; |
043 | public
void onCreate(BundlesavedInstanceState){
|
044 | super .onCreate(savedInstanceState); |
045 | setContentView(R.layout.main); |
046 | textView=(TextView)findViewById(R.id.infoPanel); |
047 | textView.setBackgroundColor( 0xffffffff ); |
048 | textView.setTextColor( 0xff0000ff ); |
049 | textView.setTextSize( 15 .0f); |
050 | textView.setScrollBarStyle(TextView.SCROLLBARS_OUTSIDE_OVERLAY); |
051 | btnListener=
new BtnClickListener(); |
052 | btnShowNetInfo=(Button)findViewById(R.id.showInfo); |
053 | btnShowNetInfo.setOnClickListener(btnListener); |
054 | btnSetCMWAP=(Button)findViewById(R.id.setCMWAP); |
055 | btnSetCMWAP.setOnClickListener(btnListener); |
056 | btnSetGPRS=(Button)findViewById(R.id.setGPRS); |
057 | btnSetGPRS.setOnClickListener(btnListener); |
060 | private
void initAPNValues(){
|
061 | cvWAP=
new ContentValues(); |
062 | cvWAP.put( "name" , "移动梦网" );
|
063 | cvWAP.put( "apn" , "cmwap" );
|
065 | cvWAP.put( "type" , "default" );
|
066 | cvWAP.put( "proxy" , "10.0.0.172" );
|
067 | cvWAP.put( "port" , "80" ); |
068 | cvWAP.put( "mmsproxy" , "10.0.0.172" );
|
069 | cvWAP.put( "mmsport" , "80" ); |
070 | cvWAP.put( "mmsprotocol" , "2.0" ); |
071 | cvWAP.put( "mmsc" , "http://mmsc.monternet.com" ); |
072 | cvWAP.put( "mcc" , "460" ); |
073 | cvWAP.put( "mnc" , "02" ); |
074 | cvWAP.put( "numeric" , "46002" );
|
075 | cvGPRS=
new ContentValues(); |
076 | cvGPRS.put( "name" , "GPRS" );
|
077 | cvGPRS.put( "apn" , "cmnet" );
|
079 | cvGPRS.put( "type" , "default" );
|
080 | //cvGPRS.put("proxy","10.0.0.172"); |
081 | //cvGPRS.put("port","80"); |
082 | //cvGPRS.put("mmsproxy","10.0.0.172"); |
083 | //cvGPRS.put("mmsport","80"); |
084 | cvGPRS.put( "mmsprotocol" , "2.0" ); |
085 | //cvGPRS.put("mmsc","http://mmsc.monternet.com"); |
086 | cvGPRS.put( "mcc" , "460" ); |
087 | cvGPRS.put( "mnc" , "02" ); |
088 | cvGPRS.put( "numeric" , "46002" );
|
090 | private
void showNetworkInfo(){
|
094 | textView.append( "/nListDefaultAccessPointName:/n" ); |
095 | listAllAPNs(PREFERRED_APN_URI); |
096 | textView.append( "/nListallAccessPointName:/n" ); |
097 | listAllAPNs(ALL_APN_URI); |
099 | private
void getLocalAddress(){
|
102 | iAdd=InetAddress.getLocalHost(); |
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 | StringhostName=iAdd.getHostName(); |
113 | InetAddress[]adds=
null ; |
114 | adds=InetAddress.getAllByName(hostName); |
116 | for
( int i=
0 ;i<adds.length;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" ); |
129 | } catch (UnknownHostExceptione1){
|
133 | for
(Enumeration<NetworkInterface>en=NetworkInterface
|
134 | .getNetworkInterfaces();en.hasMoreElements();){
|
135 | NetworkInterfaceintf=en.nextElement(); |
136 | for
(Enumeration<InetAddress>enumIpAddr=intf |
137 | .getInetAddresses();enumIpAddr.hasMoreElements();){
|
138 | InetAddressinetAddress=enumIpAddr.nextElement(); |
139 | //if(!inetAddress.isLoopbackAddress()){
|
140 | textView.append( "HostAddress=" |
141 | +inetAddress.getHostAddress()+ "/n" ); |
145 | } catch (SocketExceptionex){
|
146 | Log.e( "WifiPreferenceIpAddress" ,ex.toString()); |
149 | private
void getWifiAddress(){
|
150 | WifiManagerwifi=(WifiManager)getSystemService(Context.WIFI_SERVICE); |
151 | WifiInfoinfo=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" ); |
160 | private
void getNetworkInfo(){
|
162 | ConnectivityManagermag=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
163 | textView.append( "/nActive:/n" ); |
164 | NetworkInfoinfo=mag.getActiveNetworkInfo(); |
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" ); |
171 | textView.append( "/nWifi:/n" ); |
172 | NetworkInfowifiInfo=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 | NetworkInfomobInfo=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" ); |
186 | private
void listAllAPNs(UriapnUri){
|
187 | ContentResolvercontentResolver=getContentResolver(); |
188 | Cursorcursor=contentResolver.query(apnUri, null , null , null , null ); |
190 | Stringtemp=
"Count=" +cursor.getCount()+ "ColumnCount=" |
191 | +cursor.getColumnCount()+ "/n" ; |
192 | textView.append(temp); |
194 | while
(cursor.moveToNext()){ |
196 | int
position=cursor.getPosition(); |
197 | textView.append( "/n"
+key+ "=" +String.valueOf(position)
|
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" );
|
218 | private
void appendDBColumn(Cursorcursor,Stringkey){
|
220 | Stringvalue=cursor.getString(cursor.getColumnIndex(key)); |
221 | textView.append(key+ "=" +value+
"/n" ); |
223 | System.out.println( "[sodino]"
+e); |
226 | private
void setNetworkFeature(){
|
228 | ConnectivityManagerconnectivityMag=(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" ); |
237 | private
void setDefaultAPN(ContentValuesvalue){
|
238 | int
_id=findAPNId(value); |
242 | textView.append(value.get( "name" )+ "_id=" +_id+
"/n" ); |
243 | ContentValuesvalues=
new ContentValues(); |
244 | values.put( "apn_id" ,_id); |
245 | ContentResolverresolver=getContentResolver(); |
246 | int
updateRow=resolver.update(PREFERRED_APN_URI,values,
null , null ); |
247 | textView.append( "updateRow="
+updateRow+ "/n" ); |
248 | textView.append( "Set"
+value.get( "name" ) |
249 | + "asdefaultnetwroksuccessed!!/n" ); |
251 | private
int findAPNId(ContentValuescv){
|
253 | ContentResolvercontentResolver=getContentResolver(); |
254 | Cursorcursor=contentResolver.query(ALL_APN_URI, null , null , null , |
257 | while
(cursor.moveToNext()){ |
258 | if
(cursor.getString(cursor.getColumnIndex( "name" )).equals( |
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" )); |
271 | private
int insertAPN(ContentValuesvalue){
|
273 | ContentResolverresolver=getContentResolver(); |
274 | UrinewRow=resolver.insert(ALL_APN_URI,value); |
276 | Cursorcursor=resolver.query(newRow, null , null , null , null ); |
277 | int
idIdx=cursor.getColumnIndex( "_id" ); |
279 | apn_Id=cursor.getShort(idIdx); |
280 | System.out.println( "[sodino]InsertNewid:"
+apn_Id); |
284 | public
boolean onCreateOptionsMenu(Menumenu){
|
288 | public
boolean onOptionsItemSelected(MenuItemitem){
|
289 | if
(item.getTitle().equals( "finish" )){
|
294 | /**将10进制整数形式转换成127.0.0.1形式的IP地址*/ |
295 | private
static StringformatIP4( long
longIP){ |
296 | StringBuffersb=
new StringBuffer( "" ); |
297 | sb.append(String.valueOf(longIP>>> 24 )); |
299 | sb.append(String.valueOf((longIP&
0x00FFFFFF )>>>
16 )); |
301 | sb.append(String.valueOf((longIP&
0x0000FFFF )>>>
8 )); |
303 | sb.append(String.valueOf(longIP&
0x000000FF ));
|
306 | private
class BtnClickListener implements OnClickListener{
|
307 | public
void onClick(Viewv){
|
309 | if
(v==btnShowNetInfo){ |
311 | } else if
(v==btnSetCMWAP){ |
313 | btnSetCMWAP.setEnabled( false ); |
314 | btnSetGPRS.setEnabled( true ); |
315 | } else if
(v==btnSetGPRS){ |
316 | setDefaultAPN(cvGPRS); |
317 | btnSetGPRS.setEnabled( false ); |
318 | btnSetCMWAP.setEnabled( true ); |
[代码][XML]代码
viewsource
print?
01 | <? xml
version = "1.0"
encoding = "utf-8" ?> |
02 | < LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:orientation = "horizontal" > |
12 | android:id = "@+id/showInfo" |
13 | android:layout_width = "wrap_content" |
14 | android:layout_height = "wrap_content" |
15 | android:layout_weight = "2" |
19 | android:id = "@+id/setCMWAP" |
20 | android:layout_width = "wrap_content" |
21 | android:layout_height = "wrap_content" |
22 | android:layout_weight = "1" |
26 | android:id = "@+id/setGPRS" |
27 | android:layout_width = "wrap_content" |
28 | android:layout_height = "wrap_content" |
29 | android:layout_weight = "1" |
34 | android:layout_width = "fill_parent" |
35 | android:layout_height = "wrap_content" > |
37 | android:id = "@+id/infoPanel" |
38 | android:layout_width = "fill_parent" |
39 | android:layout_height = "wrap_content" |
[代码][XML]代码
viewsource
print?
01 | <? xml
version = "1.0"
encoding = "utf-8" ?> |
02 | < manifest
xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | package = "lab.sodino.network"
android:versionCode = "1" |
04 | android:versionName = "1.0" > |
05 | < application
android:icon = "@drawable/icon"
android:label = "@string/app_name" > |
06 | < activity
android:label = "@string/app_name"
android:name = ".NetworkAct" > |
08 | < action
android:name = "android.intent.action.MAIN"
/> |
09 | < category
android:name = "android.intent.category.LAUNCHER"
/> |
13 | < uses-sdk
android:minSdkVersion = "4"
/> |
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 > |
21 | < uses-permission
android:name = "android.permission.CHANGE_NETWORK_STATE" ></ uses-permission > |
[图片]7303_1286788707A168.jpg
[图片]7303_1286788707dRbs.jpg
[图片]7303_129784630836V1.jpg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理