Display and disappear of qq network status bar of a mobile phone; display when there is no network; automatically disappear when there is a network; click the network bar to set the network, qq

Source: Internet
Author: User
Tags eventbus

Display and disappear of qq network status bar of a mobile phone; display when there is no network; automatically disappear when there is a network; click the network bar to set the network, qq

Follow finddreams, share and make progress together:
Http://blog.csdn.net/finddreams/article/details/44647843

We all know that mobile QQ has such a function, that is, when we are disconnected from the network, when there is no network, there will be a warning on the top of the chat list, it says, "The farthest distance in the world is no network, check settings", and click this horizontal bar to jump to the page for setting the network. After we have set the network, this warning bar automatically disappears.
Today, we will imitate how the following functions are implemented. There are not many things, and there is a picture of the truth. Please refer:

1. First, we will define the broadcast netcycler used to detect the network status.

/*** @ Description: Receive of network status * @ 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 ("network status:" + isConnected); System. out. println ("wifi status:" + NetUtils. isWifiConnected (context); System. out. println ("mobile network status:" + NetUtils. isMobileConnected (context); System. out. println ("network connection type:" + NetUtils. getConnectedType (context); if (isConnected) {Toast. makeText (context, "connected to the network", Toast. LENGTH_LONG ). show (); EventBus. getDefault (). post (new NetEvent (true);} else {EventBus. getDefault (). post (new NetEvent (false); Toast. makeText (context, "disconnected network", Toast. LENGTH_LONG ). show ();}}}}

In this class, we use a NetUtils class to obtain whether the current network status is available. If true is returned, the current network status is available. If false is returned, the opposite is true.

// Determine the network connection status 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 ;}

At the same time, we can see that EventBus. getDefault (). post (new NetEvent (true); this Code may not be familiar to beginners of EventBus. If you do not know what this means, you can first understand the EventBus open-source framework, using EventBus can easily complete communication between Android components and solve the problem of message transmission decoupling between modules. I will not go into details here. If you want to know more, you can refer to the use of EventBus written by other bloggers.

2. register the broadcast receiver in the Activity and subscribe to the EventBus event. The Code is as follows:

/*** @ Description: Display Control of the network status bar * @ author http://blog.csdn.net/finddreams */public class MainActivity extends Activity {private NewsAdapter adapter; private comment list <RecentChat> chats = new comment list <RecentChat> (); private netpolicer mer er; private ListView listView; private RelativeLayout no_network_rl; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstance State); setContentView (R. layout. activity_main); initReceive (); initView (); EventBus. getDefault (). register (this);} private void initView () {chats. add (new RecentChat ("Zhang Zetian", "Learn every day", "16:30", ""); chats. add (new RecentChat ("", "Learn every day", "17:30", ""); chats. add (new RecentChat ("Han xiaozhu", "Learn every day", "Yesterday", ""); chats. add (new RecentChat ("Jing Tian", "Learn every day", "Monday", ""); chats. add (new RecentChat ("Liu Yifei", "Study every day", "", ""); chats. add (new RecentChat ("Deng Ziqi", "Learn every day", "Monday", ""); listView = (ListView) findViewById (R. id. lv_news); adapter = new NewsAdapter (this, chats, listView); listView. setAdapter (adapter); no_network_rl = (RelativeLayout) findViewById (Response _ view_rl);} private void initReceive () {mReceiver = new NetReceiver (); IntentFilter mFilter = new IntentFilter (); mFilter. addAct Ion (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. Click the horizontal line of the network warning to go to the network settings page. However, different Android versions have different calling methods. Therefore, you need to differentiate the current mobile phone version:

/*** Set Network * @ 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. After talking about so much code, let's take a look at how activity_main.xml is laid out.

<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. All right, it's easy to display and disappear the QQ network status bar of a mobile phone. In this way, similar effects can be achieved in your project.

Source Code address see: https://github.com/finddreams/NetStateBar welcome star

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.