<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