Android联网方式判断详解

工作闲余时间学习了下其它项目中的联网方式判断,很简单的..今天有时间就放博客里面供自己记录,有需要的朋友可以同小马交流交流,希望大家多多指点。。谢谢。。
   主类:
1. package com.xiaoma.www;
2. 
3. import android.app.Activity;
4. import android.content.Context;
5. import android.content.Intent;
6. import android.database.Cursor;
7. import android.net.ConnectivityManager;
8. import android.net.NetworkInfo;
9. import android.net.NetworkInfo.State;
10. import android.net.Uri;
11. import android.os.Bundle;
12. import android.widget.TextView;
13. 
14. /**  
15. * @Title: JudgeNewWayDemoActivity.java
16. * @Package com.xiaoma.www
17. * @Description: 判断联网方式测试类
18. * @author MZH
19. */
20. public class JudgeNewWayDemoActivity extends Activity {
21.     
22.     private TextView text;
23.     
24.     /** Called when the activity is first created. */
25.     @Override
26.     public void onCreate(Bundle savedInstanceState) {
27.         super.onCreate(savedInstanceState);
28.         setContentView(R.layout.main);
29.         
30.         /**
31.          * 下面列举几个可直接跳到联网设置的意图,供大家学习
32.          */
33.         //startActivity(new Intent(android.provider.Settings.ACTION_APN_SETTINGS));
34.         //startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
35.         
36.         //用下面两种方式设置网络
37.         //startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
38.         //startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
39.         init();
40.     }
41.     
42.     /**
43.      * 初始化实现
44.      */
45.     private void init(){
46.         text = (TextView)findViewById(R.id.waytext);
47.         /**获得系统级联网管理员对象*/
48.         ConnectivityManager manager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
49.         NetworkInfo info = manager.getActiveNetworkInfo();
50.         if(info == null){  //无网情况下
51.             //跳转到网络设置页面
52.             startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
53.         }else{   //有网情况下
54.             if(info.isAvailable()){ //网络可用时
55.                 /**是手机自带的联网方式*/
56.                 if(info.getType() == ConnectivityManager.TYPE_MOBILE){
57.                         if(info.getState() == State.CONNECTED){
58.                             text.setText(String.valueOf("手机网络可用并已连接"+"\n"+"连接网络方式为:\n"+info.getType()+",MOBILE"));
59.                         }
60.    
61.                 /**WIFI联网方式*/
62.                 }else{
63.                     text.setText(String.valueOf("连接网络方式为:"+info.getType()+",WI-FI"));
64.                     startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
65.                 }
66.             }else{
67.                 text.setText(String.valueOf("手机网络不可用"));
68.             }
69.             
70.         }
71.     }
72.     
73.     /**
74.      * 临时写来练练的方法,大家可以跳过
75.      */
76.     private void kkk(){
77.         Uri uri = Uri.parse("content://telephony/carriers");
78.         Cursor cr = getContentResolver().query(uri, null, null, null, null);     
79.         while(cr!=null && cr.moveToNext()){   
80.             // APN id  
81.             String id = cr.getString(cr.getColumnIndex("_id"));    
82.             // APN name   
83.             String apn = cr.getString(cr.getColumnIndex("apn"));  
84.             // do other things...  
85.         }  
86.     }
87.     
88.     
89.     /**
90.      * 移动或联通ctwap cmwap 判断  这个方法朋友们可根据自己的需要使用,小马
91.      * 只是记在这个地方,供自己记录的,有需要的朋友可以交流讨论下..吼吼
92.      */
93.     @SuppressWarnings("unused")
94.     private void initWapState() {
95.         //ConnectivityManager: Class that answers queries about the state of network connectivity.
96.         ConnectivityManager conMan = (ConnectivityManager) 
97.             getSystemService(Context.CONNECTIVITY_SERVICE);
98.         NetworkInfo netInfo = conMan.getActiveNetworkInfo();
99.         if (netInfo != null) {
100.             String extraInfo = netInfo.getExtraInfo();//Report the extra information about the network state
101.             if (extraInfo != null) {
102.                 if (extraInfo.equals("cmwap")) {
103.                     
104.                 }else if(extraInfo.equals("ctwap") || extraInfo.equals("#777")){
105.                    
106.                 }
107.             }
108.         }
109.     }
110.     
111.     
112. }
   WIFI控制类:
1. package com.xiaoma.www;
2. 
3. import java.util.List;
4. 
5. import android.content.Context;
6. import android.net.wifi.ScanResult;
7. import android.net.wifi.WifiConfiguration;
8. import android.net.wifi.WifiInfo;
9. import android.net.wifi.WifiManager;
10. import android.net.wifi.WifiManager.WifiLock;
11. 
12. /**  
13. * @Title: WifiCheckUtil.java
14. * @Package com.lg.lvguo.util
15. * @Description: WIFI控制类
16. * @author MZH
17. * @version FG1.0
18. */
19. public class WifiCheckUtil {
20.     //定义WifiManager对象  
21.     private WifiManager mWifiManager;  
22.     //定义WifiInfo对象  
23.     private WifiInfo mWifiInfo;  
24.     //扫描出的网络连接列表  
25.     private List<ScanResult> mWifiList;  
26.     //网络连接列表  
27.     private List<WifiConfiguration> mWifiConfiguration;  
28.     //定义一个WifiLock  
29.     WifiLock mWifiLock;  
30.     //构造器  
31.     public  WifiCheckUtil(Context context)  
32.     {  
33.         //取得WifiManager对象  
34.         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
35.         //取得WifiInfo对象  
36.         mWifiInfo = mWifiManager.getConnectionInfo();  
37.     } 
38.     //取到WIFI状态
39.     public int checkWifiState(){
40.         return mWifiManager.getWifiState();
41.     }
42.     //打开WIFI  
43.     public void OpenWifi()  
44.     {  
45.         if (!mWifiManager.isWifiEnabled())  
46.         {  
47.             mWifiManager.setWifiEnabled(true);  
48.               
49.         }  
50.     }  
51.     //关闭WIFI  
52.     public void CloseWifi()  
53.     {  
54.         if (!mWifiManager.isWifiEnabled())  
55.         {  
56.             mWifiManager.setWifiEnabled(false);   
57.         }  
58.     }  
59.     //锁定WifiLock  
60.     public void AcquireWifiLock()  
61.     {  
62.         mWifiLock.acquire();  
63.     }  
64.     //解锁WifiLock  
65.     public void ReleaseWifiLock()  


66.     {  
67.         //判断时候锁定  
68.         if (mWifiLock.isHeld())  
69.         {  
70.             mWifiLock.acquire();  
71.         }  
72.     }  
73.     //创建一个WifiLock  
74.     public void CreatWifiLock()  
75.     {  
76.         mWifiLock = mWifiManager.createWifiLock("Lock");  
77.     }  
78.     //得到配置好的网络  
79.     public List<WifiConfiguration> GetConfiguration()  
80.     {  
81.         return mWifiConfiguration;  
82.     }  
83.     //指定配置好的网络进行连接  
84.     public void ConnectConfiguration(int index)  
85.     {  
86.         //索引大于配置好的网络索引返回  
87.         if(index > mWifiConfiguration.size())  
88.         {  
89.             return;  
90.         }  
91.         //连接配置好的指定ID的网络  
92.         mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true);  
93.     }  
94.     public void StartScan()  
95.     {  
96.         mWifiManager.startScan();  
97.         //得到扫描结果  
98.         mWifiList = mWifiManager.getScanResults();  
99.         //得到配置好的网络连接  
100.         mWifiConfiguration = mWifiManager.getConfiguredNetworks();  
101.     }  
102.     //得到网络列表  
103.     public List<ScanResult> GetWifiList()  
104.     {  
105.         return mWifiList;  
106.     }  
107.     //查看扫描结果  
108.     public StringBuilder LookUpScan()  
109.     {  
110.         StringBuilder stringBuilder = new StringBuilder();  
111.         for (int i = 0; i < mWifiList.size(); i++)  
112.         {  
113.             stringBuilder.append("Index_"+new Integer(i + 1).toString() + ":");  
114.             //将ScanResult信息转换成一个字符串包  
115.             //其中把包括:BSSID、SSID、capabilities、frequency、level  
116.             stringBuilder.append((mWifiList.get(i)).toString());  
117.             stringBuilder.append("/n");  
118.         }  
119.         return stringBuilder;  
120.     }  
121.     //得到MAC地址  
122.     public String GetMacAddress()  
123.     {  
124.         return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();  
125.     }  
126.     //得到接入点的BSSID  
127.     public String GetBSSID()  
128.     {  
129.         return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();  
130.     }  
131.     //得到IP地址  
132.     public int GetIPAddress()  
133.     {  
134.         return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();  
135.     }  
136.     //得到连接的ID  
137.     public int GetNetworkId()  
138.     {  
139.         return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();  
140.     }  
141.     //得到WifiInfo的所有信息包  
142.     public String GetWifiInfo()  
143.     {  
144.         return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();  
145.     }  
146.     //添加一个网络并连接  
147.     public void AddNetwork(WifiConfiguration wcg)  
148.     {  
149.         int wcgID = mWifiManager.addNetwork(wcg);   
150.         mWifiManager.enableNetwork(wcgID, true);   
151.     }  
152.     //断开指定ID的网络  
153.     public void DisconnectWifi(int netId)  
154.     {  
155.         mWifiManager.disableNetwork(netId);  
156.         mWifiManager.disconnect();  
157.     } 
158. }
   全局配置文件:
    
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3.     package="com.xiaoma.www"
4.     android:versionCode="1"
5.     android:versionName="1.0" >
6. 
7.     <uses-sdk android:minSdkVersion="15" />
8. 
9.     <application
10.         android:icon="@drawable/ic_launcher"
11.         android:label="@string/app_name" >
12.         <activity
13.             android:name=".JudgeNewWayDemoActivity"
14.             android:label="@string/app_name" >
15.             <intent-filter>
16.                 <action android:name="android.intent.action.MAIN" />
17. 
18.                 <category android:name="android.intent.category.LAUNCHER" />
19.             </intent-filter>
20.         </activity>
21.     </application>
22.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
23. </manifest>
 

转载于:http://www.2cto.com/kf/201203/123461.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值