我们在这篇文章中将会通过一段代码的解读来详细分析Android判断网络状态的应用技巧,以帮助大家判断网络的连接状态。
Android 判断网络状态这一应用技巧在实际应中是比较重要的。那么,在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开恩?今天我们就针对这一应用技巧进行一个详细的分析。
- public class ConnectionChangeReceiver extends
BroadcastReceiver- {
- @Override
- public void onReceive( Context context, Intent intent )
- {
- ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE );- NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();- NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE );- if ( activeNetInfo != null )
- {
- Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();- }
- if( mobNetInfo != null )
- {
- Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();- }
- }
- }
- < !-- Needed to check when the network connection changes -->
- < uses-permission android:name="android.permission.
ACCESS_NETWORK_STATE"/>- < receiver android:name="com.blackboard.androidtest.
receiver.ConnectionChangeReceiver"- android:label="NetworkConnection">
- < intent-filter>
- < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
- < /intent-filter>
- < /receiver>
Android判断网络状态的具体应用方法就为大家介绍到这里。
【编辑推荐】