android中检測网络连接状态简单总结

相应差点儿没有不跟网络打交道的android应用,那么在实际中就需求检測手机是否有网络连接。甚至须要推断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,以下给出一些经常使用的推断。假设要知道是否有网络、以及是採用wifi连接的还是3G连接的。调用以下相应方法模型就OK了。代码例如以下:

TestNetworkActivity:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package
com.home.testnetwork; 
   
import
java.util.List; 
   
import
android.app.Activity; 
import
android.content.Context; 
import
android.location.LocationManager; 
import
android.net.ConnectivityManager; 
import
android.net.NetworkInfo; 
import
android.os.Bundle; 
import
android.view.View; 
import
android.view.View.OnClickListener; 
import
android.widget.Button; 
import
android.widget.EditText; 
   
public
class
TestNetworkActivity
extends
Activity
implements
OnClickListener { 
    
private
Button checkBtn; 
    
private
EditText netText; 
    
private
EditText wifiText; 
    
private
EditText net3gText; 
    
private
EditText gpsText; 
   
    
@Override 
    
protected
void
onCreate(Bundle savedInstanceState) { 
        
super
.onCreate(savedInstanceState); 
        
setContentView(R.layout.main); 
        
checkBtn = (Button) findViewById(R.id.main_btn_check); 
        
checkBtn.setOnClickListener(
this
); 
        
wifiText = (EditText) findViewById(R.id.main_et_wifi); 
        
net3gText = (EditText) findViewById(R.id.main_et_3g); 
        
gpsText = (EditText) findViewById(R.id.main_et_GPS); 
        
netText = (EditText) findViewById(R.id.main_et_net); 
    
   
    
/**
     
* 检測网络是否连接
     
     
* @return
     
*/ 
    
private
boolean
isNetConnected() { 
        
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        
if
(cm !=
null
) { 
            
NetworkInfo[] infos = cm.getAllNetworkInfo(); 
            
if
(infos !=
null
) { 
                
for
(NetworkInfo ni : infos) { 
                    
if
(ni.isConnected()) { 
                        
return
true
                    
                
            
        
        
return
false
    
   
    
/**
     
* 检測wifi是否连接
     
     
* @return
     
*/ 
    
private
boolean
isWifiConnected() { 
        
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        
if
(cm !=
null
) { 
            
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 
            
if
(networkInfo !=
null 
                    
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
                
return
true
            
        
        
return
false
    
   
    
/**
     
* 检測3G是否连接
     
     
* @return
     
*/ 
    
private
boolean
is3gConnected() { 
        
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        
if
(cm !=
null
) { 
            
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 
            
if
(networkInfo !=
null 
                    
&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { 
                
return
true
            
        
        
return
false
    
   
    
/**
     
* 检測GPS是否打开
     
     
* @return
     
*/ 
    
private
boolean
isGpsEnabled() { 
        
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        
List<String> accessibleProviders = lm.getProviders(
true
); 
        
for
(String name : accessibleProviders) { 
            
if
(
"gps"
.equals(name)) { 
                
return
true
            
        
        
return
false
    
   
    
@Override 
    
public
void
onClick(View v) { 
        
if
(v == checkBtn) { 
            
netText.setText(isNetConnected() +
""
); 
            
wifiText.setText(isWifiConnected() +
""
); 
            
net3gText.setText(is3gConnected() +
""
); 
            
gpsText.setText(isGpsEnabled() +
""
); 
        
    

 

 
布局xml:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android" 
    
android:layout_width=
"match_parent" 
    
android:layout_height=
"match_parent" 
    
android:orientation=
"vertical"
   
    
<Button 
        
android:id=
"@+id/main_btn_check" 
        
android:layout_width=
"match_parent" 
        
android:layout_height=
"wrap_content" 
        
android:text=
"检測网络"
/> 
   
    
<LinearLayout 
        
android:layout_width=
"match_parent" 
        
android:layout_height=
"wrap_content"
   
        
<TextView 
            
android:layout_width=
"wrap_content" 
            
android:layout_height=
"wrap_content" 
            
android:text=
"网络是否连接:"
/> 
   
        
<EditText 
            
android:id=
"@+id/main_et_net" 
            
android:layout_width=
"match_parent" 
            
android:layout_height=
"wrap_content" 
            
android:layout_marginLeft=
"10dp" 
            
android:enabled=
"false"
/> 
    
</LinearLayout> 
   
    
<LinearLayout 
        
android:layout_width=
"match_parent" 
        
android:layout_height=
"wrap_content"
   
        
<TextView 
            
android:layout_width=
"wrap_content" 
            
android:layout_height=
"wrap_content" 
            
android:text=
"wifi是否连接:"
/> 
   
        
<EditText 
            
android:id=
"@+id/main_et_wifi" 
            
android:layout_width=
"match_parent" 
            
android:layout_height=
"wrap_content" 
            
android:layout_marginLeft=
"10dp" 
            
android:enabled=
"false"
/> 
    
</LinearLayout> 
   
    
<LinearLayout 
        
android:layout_width=
"match_parent" 
        
android:layout_height=
"wrap_content"
   
        
<TextView 
            
android:layout_width=
"wrap_content" 
            
android:layout_height=
"wrap_content" 
            
android:text=
"3G是否连接:"
/> 
   
        
<EditText 
            
android:id=
"@+id/main_et_3g" 
            
android:layout_width=
"match_parent" 
            
android:layout_height=
"wrap_content" 
            
android:layout_marginLeft=
"10dp" 
            
android:enabled=
"false"
/> 
    
</LinearLayout> 
   
    
<LinearLayout 
        
android:layout_width=
"match_parent" 
        
android:layout_height=
"wrap_content"
   
        
<TextView 
            
android:layout_width=
"wrap_content" 
            
android:layout_height=
"wrap_content" 
            
android:text=
"GPS是否打开:"
/> 
   
        
<EditText 
            
android:id=
"@+id/main_et_GPS" 
            
android:layout_width=
"match_parent" 
            
android:layout_height=
"wrap_content" 
            
android:layout_marginLeft=
"10dp" 
            
android:enabled=
"false"
/> 
    
</LinearLayout> 
   
</LinearLayout>

技术分享