判断网络连接(要验证)

引用:http://www.oschina.net/code/snippet_163910_6189

[代码] [Java]代码

01 public class ConnectionChangeReceiver extends
02 BroadcastReceiver  
03 {  
04 @Override  
05 public void onReceive( Context context, Intent intent )  
06 {  
07 ConnectivityManager connectivityManager =
08 (ConnectivityManager) context.getSystemService
09 ( Context.CONNECTIVITY_SERVICE );  
10 NetworkInfo activeNetInfo = connectivityManager.
11 getActiveNetworkInfo();  
12 NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
13 ( ConnectivityManager.TYPE_MOBILE );  
14 if ( activeNetInfo != null )  
15 {  
16 Toast.makeText( context, "Active Network Type : " +
17 activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();  
18 }  
19 if( mobNetInfo != null )  
20 {  
21 Toast.makeText( context, "Mobile Network Type : " +
22 mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();  
23 }  
24 }  
25 }

[代码] [XML]代码

01 < !-- Needed to check when the network connection changes -->  
02 uses-permission android:name="android.permission.
03 ACCESS_NETWORK_STATE"/>  
04 receiver android:name="com.blackboard.androidtest.
05 receiver.ConnectionChangeReceiver"  
06 android:label="NetworkConnection">  
07 intent-filter>  
08 action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>  
09 < /intent-filter>  
10 < /receiver>
posted @ 2011-09-23 14:40  镇水古月  阅读(243)  评论(0编辑  收藏  举报