Code used to determine whether the network is connected in Android Development

Source: Internet
Author: User

In Android development, especially in development related to network access, you must determine whether the mobile phone is connected to the network. Is the following a clip that determines whether the mobile phone is connected to the network:

Package cn.com. Karl. util;

Import com. kubu. Main. R;

Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. context;
Import Android. content. dialoginterface;
Import Android. content. intent;
Import android.net. connectivitymanager;
Import android.net. networkinfo;
Import Android. provider. settings;
Import Android. widget. textview;

Public class isinternet {

/**
* Determine network conditions
* @ Param context Context
* @ Return false indicates that no network exists. True indicates that a network exists.
*/
Public static Boolean isnetworkavalile (context ){
// Obtain the network status Manager
Connectivitymanager = (connectivitymanager) Context
. Getsystemservice (context. connectivity_service );

If (connectivitymanager = NULL ){
Return false;
} Else {
// Create a network Array
Networkinfo [] net_info = connectivitymanager. getallnetworkinfo ();

If (net_info! = NULL ){
For (INT I = 0; I <net_info.length; I ++ ){
// Determine whether the obtained network status is in the connection status
If (net_info [I]. getstate () = networkinfo. state. Connected ){
Return true;
}
}
}
}
Return false;
}

// If no network exists, the Network Settings dialog box is displayed.
Public static void checknetwork (final activity ){
If (! Isinternet. isnetworkavalible (activity )){
Textview MSG = new textview (activity );
MSG. settext ("No network is available currently. Set the network! ");
New alertdialog. Builder (activity)
. Seticon (R. drawable. Android)
. Settitle ("network status prompt ")
. Setview (MSG)
. Setpositivebutton ("OK ",
New dialoginterface. onclicklistener (){

Public void onclick (dialoginterface dialog,
Int whichbutton ){
// Jump to the settings page
Activity. startactivityforresult (new intent (
Settings. action_wireless_settings ),
0 );
}
}). Create (). Show ();
}
Return;
}
}

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.