Android detects if the mobile network is available

Source: Internet
Author: User

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

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.