Android判断网络状态方法详解
Android 判断网络 状态这一应用技巧在实际应中是比较重要的。那么,在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开恩?今天我们就针对这一应用技 巧进行一个详细的分析。
Android 判断网络状态这一应用技巧在实际应中是比较重要的。那么,在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开恩?今天我们就针对这一应用技巧进行一个详细的分析。
1. public class ConnectionChangeReceiver extends
BroadcastReceiver
2. {
3. @Override
4. public void onReceive( Context context, Intent intent )
5. {
6. ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE );
7. NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();
8. NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE );
9. if ( activeNetInfo != null )
10. {
11. Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
12. }
13. if( mobNetInfo != null )
14. {
15. Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
16. }
17. }
18. }
19. < !-- Needed to check when the network connection changes -->
20. < uses-permission android:name="android.permission.
ACCESS_NETWORK_STATE"/>
21. < receiver android:name="com.blackboard.androidtest.
receiver.ConnectionChangeReceiver"
22. android:label="NetworkConnection">
23. < intent-filter>
24. < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
25. < /intent-filter>
26. < /receiver>
1. public class ConnectionChangeReceiver extends
BroadcastReceiver
2. {
3. @Override
4. public void onReceive( Context context, Intent intent )
5. {
6. ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE );
7. NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();
8. NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE );
9. if ( activeNetInfo != null )
10. {
11. Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
12. }
13. if( mobNetInfo != null )
14. {
15. Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
16. }
17. }
18. }
19. < !-- Needed to check when the network connection changes -->
20. < uses-permission android:name="android.permission.
ACCESS_NETWORK_STATE"/>
21. < receiver android:name="com.blackboard.androidtest.
receiver.ConnectionChangeReceiver"
22. android:label="NetworkConnection">
23. < intent-filter>
24. < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
25. < /intent-filter>
26. < /receiver>