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;
}
}