MyException - 我的异常网
当前位置:我的异常网» Android » 网络连接状态的监听跟判断(android,wifi,Gprs)

网络连接状态的监听跟判断(android,wifi,Gprs)

www.MyException.Cn  网友分享于:2013-02-21  浏览:565次
网络连接状态的监听和判断(android,wifi,Gprs)

 

1.获取网络连接状态

 

  ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo net=connectivityManager.getActiveNetworkInfo();

 if(net==null){

           Log.i("test", "无网络连接");

       }else{

           Log.i("test", "网络连接类型为"+net.getTypeName());

       }

 

2.连接状态监听:

1) 编写BroadcastReceiver

public class NetworkReceiver extends BroadcastReceiver

{   @Override

    public void onReceive(Context context, Intent intent)

    {

        ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo net=connectivityManager.getActiveNetworkInfo();

       if(net==null){

           Log.i("test", "net==null");

       }else{

           Log.i("test", "net="+net.getTypeName());

       }

    }

 

 

}

2).配置xml

 

<receiver android:name=".NetworkReceiver">

<intent-filter>

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

</intent-filter>

</receiver>

3).加入权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

3.无连接时引导用户跳转到设置页面

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

 

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有