Mobile network availability: This is only a small demo, usually in the process of development, used in fact is more, such as: There are some news clients when you have the network, the picture can not be viewed, or we commonly used QQ, when you do not have the network, The change will give you a hint on your head: the furthest distance in the world is no network, please check the settings
Therefore, the detection network This function is actually used to listen to, but also a simple small function
1.:
In the case of network open, the network is available:
When you close the network:
2. Example source code
Package Com.zengtao.classwork;import Android.content.context;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Actionbaractivity {private Button bt_checknetwork;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Bt_checknetwork = (Button) Findviewbyid (r.id.bt_checknetwork); bt_ Checknetwork.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//1. Get system service Connectivitymanager cm = (Connectivitymanager) MainActivity.this.getSystemService (context.connectivity_ SERVICE);//2. Get net information Networkinfo info = cm.getactivenetworkinfo ();//3. Determine if the network is available if (info! = NULL && Info.isconnected ()) {Toast.maketext (mainactivity.this, "Network available", Toast.length_Short). Show ();} else {toast.maketext (mainactivity.this, "Network is not currently available, please check the settings! ", Toast.length_short). Show ();}});}}
3. Parsing
1. The above parts detect some situations, such as: whether the mobile phone flight mode, mobile phone network data is closed, whether the phone is in the WiFi situation and so on
2. These also do their own small demo play, hope for everyone to transplant project useful
Android detects if the mobile network is available