Android Determines network connection status

Source: Internet
Author: User

    • Required Permissions

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.INTERNET"/>

    • In the interface, there is a button, click the button to determine the current network connection status, respectively, in connection with the network status and the status of the network is not connected, prompting the relevant information. The code is as follows:

public class Mainactivity extends Activity {

Private button button;//corresponding buttons
Private Connectivitymanager connetivitymanager;//
Private Notificationmanager notificationmanager;//notify users of network status
Private Broadcastreceiver Broadcastreceiver = new Broadcastreceiver () {

        @Override
        public void OnReceive (Context Contex, Intent Intent) {
           // TODO auto-generated Method Stub
           //Network condition
            networkinfo mobileinfo = Connetivitymanager
                    . Getnetworkinfo (Connectivitymanager.type_mobile);
            networkinfo wifiinfo = Connetivitymanager
                    . Getnetworkinfo (Connectivitymanager.type_wifi);

Notification.builder Builder = new Notification.builder (
Mainactivity.this);
Builder.setcontenttitle ("hint message");
Builder.setsmallicon (R.drawable.ic_launcher);
if (!mobileinfo.isconnected () &&!wifiinfo.isconnected ()) {
Builder.setcontenttext ("Network status is Abnormal");
} else {
Builder.setcontenttext ("Network Normal");
}
Notificationmanager.notify (1001, Builder.build ());
}
};

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.mainactivity);
Button = (button) Findviewbyid (R.id.button1);
Button.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (connectivitymanager.connectivity_action);
Registerreceiver (Broadcastreceiver, Intentfilter);
Connetivitymanager = (Connectivitymanager) getsystemservice (Context.connectivity_service);
Notificationmanager = (Notificationmanager) getsystemservice (Context.notification_service);
}
});
}

Uninstalling the Broadcast
@Override
protected void OnPause () {
TODO auto-generated Method Stub
Super.onpause ();
if (broadcastreceiver! = null) {
Unregisterreceiver (Broadcastreceiver);
}
}
}

Android Determines network connection status

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.