android 网络广播 类似QQ动态检查网络
private ConnectivityManager mConnectivityManager; private NetworkInfo netInfo; 在onCreate 注册广播 IntentFilter mFilter = new IntentFilter(); mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(myNetReceiver, mFilter); 一定要在onDestroy()方法注销广播 @Override public void onDestroy() { super.onDestroy(); if(myNetReceiver!=null){ unregisterReceiver(myNetReceiver); } } rl_error_item是布局文件类似QQ列表显示的未连接网络状态XML布局 private BroadcastReceiver myNetReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); netInfo = mConnectivityManager.getActiveNetworkInfo(); if(netInfo != null && netInfo.isAvailable()) { //网络连接 String name = netInfo.getTypeName(); if(netInfo.getType()==ConnectivityManager.TYPE_WIFI || netInfo.getType()==ConnectivityManager.TYPE_ETHERNET || netInfo.getType()==ConnectivityManager.TYPE_MOBILE){ //3g网络 rl_error_item.setVisibility(View.GONE); } } else { //网络断开 rl_error_item.setVisibility(View.VISIBLE); } } } };
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。