美文网首页android功能调用
Android判断网络状态方法详解

Android判断网络状态方法详解

作者: 读行游 | 来源:发表于2015-09-14 11:06 被阅读323次

    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.st.makeText( context, "Active

    Network Type : " +

    tiveNetInfo.getTypeName(),Toast.LENGTH_SHORT ).show();

    12. }

    13. mobNetInfo != null )

    14. {

    15.st.makeText( context, "Mobile

    Network Type : " +

    bNetInfo.getTypeName(),Toast.LENGTH_SHORT ).show();

    16. }

    17.}

    18.}

    19.< !-- Needed to check when thenetwork 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.< actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>

    25.< /intent-filter>

    26.< /receiver>

    Android判断网络状态的具体应用方法就为大家介绍到这里。接下来给大家介绍一款我比较常用的APP测试工具:www.ineice.com

    相关文章

      网友评论

        本文标题:Android判断网络状态方法详解

        本文链接:https://www.haomeiwen.com/subject/ilxmcttx.html