仿手机QQ网络状态条的显示与消失,没网络时显示,有网络时自动消失 ,点击网络条设置网络
关注finddreams,一起分享,一起进步:
http://blog.csdn.net/finddreams/article/details/44647843
我们都知道手机QQ上有这样一个功能,就是当我们断网,没有网络的时候,聊天列表的上方就会出现一个警告的横条,上面写着”世界上最遥远的距离就是没网,检查设置”,然后点击这个横条就会跳转到设置网络的界面,等我们把网络设置好了之后,这个警告横条也就自动的消失了。
今天咱们就来模仿以下这样的功能是如何实现的,话不多少,有图有真相,请看:
1.首先,我们来定义用来检测网络状态的广播NetReceiver
/**
* @Description:网络状态的Receive
* @author http://blog.csdn.net/finddreams
*/
public class NetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
boolean isConnected = NetUtils.isNetworkConnected(context);
System.out.println("网络状态:" + isConnected);
System.out.println("wifi状态:" + NetUtils.isWifiConnected(context));
System.out.println("移动网络状态:" + NetUtils.isMobileConnected(context));
System.out.println("网络连接类型:" + NetUtils.getConnectedType(context));
if (isConnected) {
Toast.makeText(context, "已经连接网络", Toast.LENGTH_LONG).show();
EventBus.getDefault().post(new NetEvent(true));
} else {
EventBus.getDefault().post(new NetEvent(false));
Toast.makeText(context, "已经断开网络", Toast.LENGTH_LONG).show();
}
}
}
}
在这个类里面我们用了NetUtils类里面一个用于得到当前网络状态是否可用的结果。如果返回的是true就是代表当前网络状态是可用的,false则反之。
// 判断网络连接状态
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
同时我们可以看到,EventBus.getDefault().post(new NetEvent(true)); 这句代码,可能不了解EventBus的初学者不知道这是什么意思,那就可以先去了解一下EventBus这个开源框架,使用EventBus可以很方便的完成Android各个组件之间通信,解决了模块之间消息传递解耦合的问题。在这里我就不过多赘叙了,想了解的朋友可以去参考其他博主写的关于EventBus的使用。
2.然后就是在Activity中注册这个广播接收者,同时也订阅EventBus事件,具体代码如下:
/**
* @Description:网络状态条的显示控制
* @author http://blog.csdn.net/finddreams
*/
public class MainActivity extends Activity {
private NewsAdapter adapter;
private LinkedList<RecentChat> chats = new LinkedList<RecentChat>();
private NetReceiver mReceiver;
private ListView listView;
private RelativeLayout no_network_rl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initReceive();
initView();
EventBus.getDefault().register(this);
}
private void initView() {
chats.add(new RecentChat("章泽天", "好好学习天天向上", "16:30", ""));
chats.add(new RecentChat("宋茜", "好好学习天天向上", "17:30", ""));
chats.add(new RecentChat("韩孝珠", "好好学习天天向上", "昨天", ""));
chats.add(new RecentChat("景甜", "好好学习天天向上", "星期一", ""));
chats.add(new RecentChat("刘亦菲", "好好学习天天向上", "17:30", ""));
chats.add(new RecentChat("邓紫棋", "好好学习天天向上", "星期一", ""));
listView = (ListView) findViewById(R.id.lv_news);
adapter = new NewsAdapter(this, chats, listView);
listView.setAdapter(adapter);
no_network_rl = (RelativeLayout) findViewById(R.id.net_view_rl);
}
private void initReceive() {
mReceiver = new NetReceiver();
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, mFilter);
}
public void onEventMainThread(NetEvent event) {
setNetState(event.isNet());
}
public void setNetState(boolean netState) {
if (no_network_rl != null) {
no_network_rl.setVisibility(netState ? View.GONE : View.VISIBLE);
no_network_rl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
NetUtils.startToSettings(MainActivity.this);
}
});
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
3.点击网络警告的横条我们会进入网络设置界面,但是不同的Android版本有不同的调用方法,所以要进行区分当前手机的版本号:
/**
* 设置网络
* @param paramContext
*/
public static void startToSettings(Context paramContext) {
if (paramContext == null)
return;
try {
if (Build.VERSION.SDK_INT > 10) {
paramContext.startActivity(new Intent(
"android.settings.SETTINGS"));
return;
}
} catch (Exception localException) {
localException.printStackTrace();
return;
}
paramContext.startActivity(new Intent(
"android.settings.WIRELESS_SETTINGS"));
}
}
4.说了这么多代码,接下来看看activity_main.xml是如何布局的呢
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="50dp" >
<Button
android:id="@+id/title_btn_left"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@color/common_title"
android:textColor="@color/blue"
android:textSize="16sp" />
<TextView
android:id="@+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/title_btn_right"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@color/common_title" />
<LinearLayout
android:id="@+id/common_constact"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ll_top_bg"
android:orientation="horizontal" >
<Button
android:id="@+id/constact_group"
style="@style/top_group"
android:text="@string/group" />
<Button
android:id="@+id/constact_all"
style="@style/top_all"
android:text="@string/all" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp" >
<LinearLayout
android:id="@+id/ll_constact_serach"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="@drawable/acm_inputbox"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_search"
android:textColor="@color/gray_font" />
</LinearLayout>
</RelativeLayout>
<include layout="@layout/layout_netbar" />
<ListView
android:id="@+id/lv_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:fadingEdgeLength="0dp" />
</LinearLayout>
5.好吧,仿手机QQ网络状态条的显示与消失到这基本搞定了,是不是觉得很容易啊,这样你的项目中也可以实现类似的效果了。
源码地址见:https://github.com/finddreams/NetStateBar 欢迎star
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。