public class NetstateActivity extends Activity {
/** Called when the activity is first created. */
public int NET_ETHERNET = 1;
public int NET_WIFI = 2;
public int NET_NOCONNECT = 0;
private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
ConnectivityManager.CONNECTIVITY_ACTION)
|| intent.getAction().equals(
"android.net.conn.CONNECTIVITY_CHANGE")) {
switch (isNetworkAvailable(NetstateActivity.this)) {
case 1:
Log.v("-----------networktest----------", "有线");
break;
case 2:
Log.v("-----------networktest----------", "无线");
break;
case 0:
Log.v("-----------networktest----------", "无网络");
break;
default:
break;
}
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
}
private int isNetworkAvailable(Context context) {
ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ethNetInfo = connectMgr
.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifiNetInfo = connectMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (ethNetInfo != null && ethNetInfo.isConnected()) {
return NET_ETHERNET;
} else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
return NET_WIFI;
} else {
return NET_NOCONNECT;
}
}
/** Called when the activity is first created. */
public int NET_ETHERNET = 1;
public int NET_WIFI = 2;
public int NET_NOCONNECT = 0;
private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
ConnectivityManager.CONNECTIVITY_ACTION)
|| intent.getAction().equals(
"android.net.conn.CONNECTIVITY_CHANGE")) {
switch (isNetworkAvailable(NetstateActivity.this)) {
case 1:
Log.v("-----------networktest----------", "有线");
break;
case 2:
Log.v("-----------networktest----------", "无线");
break;
case 0:
Log.v("-----------networktest----------", "无网络");
break;
default:
break;
}
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
}
private int isNetworkAvailable(Context context) {
ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ethNetInfo = connectMgr
.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifiNetInfo = connectMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (ethNetInfo != null && ethNetInfo.isConnected()) {
return NET_ETHERNET;
} else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
return NET_WIFI;
} else {
return NET_NOCONNECT;
}
}
}
AndroidManifest.xml加入 :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />