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));