Android determines the network status and opens the Network Settings dialog box when there is no network.
When using Android to connect to the network, it is not always possible to connect to the network. At this time, we 'd better judge the network status when the program starts, if no network exists, you will be reminded to set it.
To determine the network status, you must first have the corresponding permissions. The permission code is as follows:
That is, access to the network status is allowed:
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE">
The following is the judgment code:
Code
Private boolean NetWorkStatus (){
Boolean netSataus = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );
CwjManager. getActiveNetworkInfo ();
If (cwjManager. getActiveNetworkInfo ()! = Null ){
NetSataus = cwjManager. getActiveNetworkInfo (). isAvailable ();
}
If (! NetSataus ){
Builder B = new AlertDialog. Builder (this). setTitle ("no available network ")
. SetMessage ("do you want to set the network? ");
B. setPositiveButton ("yes", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int whichButton ){
Intent mIntent = new Intent ("/");
ComponentName comp = new ComponentName (
"Com. android. settings ",
"Com. android. settings. WirelessSettings ");
MIntent. setComponent (comp );
MIntent. setAction ("android. intent. action. VIEW ");
StartActivityForResult (mIntent, 0); // if you need to perform the operation again after the setting is complete, you can rewrite the operation code.
}
}). SetNeutralButton ("no", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int whichButton ){
Dialog. cancel ();
}
}). Show ();
}
Return netSataus;
}