Android determines the network status and opens the Network Settings dialog box when there is no network.

Source: Internet
Author: User

When using Android to connect to the network, it is not always possible to connect to the network. At this time, we 'd betterProgramMake a judgment on the network status when starting. If there is no network, immediately remind the user to set it.

To determine the network status, you must first have the corresponding permissions. The following are the permissions:Code:

That is, access to the network status is allowed:

 
  <Uses-PermissionAndroid: Name= "Android. Permission. access_network_state"> </Uses-Permission>

 

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

 

The above code can be used to determine the network status! How can I set the network by myself? I haven't figured it out yet. I'll try again later!

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.