Android devices determine network connection status

Source: Internet
Author: User

In Android development, it is sometimes necessary to determine the current network connection status before making a network request. This article describes how to get the current network connection status of your Android device!


Required permissions (added in the Androidmanifest.xml file):

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

Java code (mainactivity.java file)

Package Com.example.androidtest;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.networkinfo.state;import Android.os.bundle;import Android.app.activity;import Android.content.Context Import Android.util.log;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Boolean bflag = Isnetworkavailable (This.getapplicationcontext ()), if (bflag) {log.i ("Network state-", "Connected");} else {log.i ("Network state-", "Disconnected");}} /** * Determine network connection Status * * @param CTX * @return */public boolean isnetworkavailable (Context ctx) {Boolean bflag = False;if (CTX!) = null) {Connectivitymanager conman = (Connectivitymanager) ctx.getsystemservice (Context.connectivity_service); Conman! = NULL) {//WiFi Networknetworkinfo ninfow = Conman.getnetworkinfo (Connectivitymanager.type_wifi); if (NInfoW! = N ull) {state WiFi = NinfOw.getstate (); if (state.connected = = WiFi) {bflag = true;}} if (!bflag) {//mobile Networknetworkinfo ninfom = Conman.getnetworkinfo (connectivitymanager.type_mobile); if (NInfoM! = NULL) {state mobile = ninfom.getstate (); if (state.connected = = Mobile) {bflag = true;}}}} return bflag;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}

Isnetworkavailable method First do WiFi connection status, if WiFi is not connected, then do 2g/3g/4g ... Mobile phone network connection status judgment.




Android devices determine network connection status

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.