引用:http://www.oschina.net/code/snippet_163910_6189
[代码] [Java]代码
01 |
public class ConnectionChangeReceiver extends |
05 |
public void onReceive( Context context, Intent intent ) |
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 ) |
16 |
Toast.makeText( context, "Active Network Type : " + |
17 |
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); |
19 |
if ( mobNetInfo != null ) |
21 |
Toast.makeText( context, "Mobile Network Type : " + |
22 |
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); |
[代码] [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" > |
08 |
< action android:name = "android.net.conn.CONNECTIVITY_CHANGE" /> |