13,100
社区成员
发帖
与我相关
我的任务
分享
<receiver android:name="com.contacts.receiver.NetWorkReceiver">
<intent-filter android:priority="1000" >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
public class NetWorkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(AbstractDao.Receiver.NETRECEIVE)) {
Toast.makeText(context, "您的网络发生了变化!", Toast.LENGTH_LONG).show();
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();//获取网络的连接情况
if(networkInfo != null && networkInfo.isAvailable()) {
String name = networkInfo.getTypeName();
Toast.makeText(context, "当前网络名称:" + name, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "没有可用网络!", Toast.LENGTH_LONG).show();
}
}
}
private ConnectivityManager connectivityManager;
private NetworkInfo networkInfo;
}