Check and remind the user to set up the network (2)

Source: Internet
Author: User
// 1 do not forget to add permissions // 2 main. XML is very simple. There is only one button. The // 3 mainactivity is omitted here: Package COM. test; import android. app. activity; import android. app. alertdialog; import android. content. componentname; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android.net. connectivitymanager; import android. OS. bundle; import android. view. view; import android. widget. button; // <! -- Network permission --> // <uses-Permission Android: Name = "android. permission. access_network_state "/> // <uses-Permission Android: Name =" android. permission. internet "/> public class checenetactivity extends activity {private button; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. button); button. set Onclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {checknetwork (); // start checking the network});} public Boolean checknetwork () {Boolean flag = false; connectivitymanager = (connectivitymanager) This. getsystemservice (context. connectivity_service); If (Manager. getactivenetworkinfo ()! = NULL) {flag = manager. getactivenetworkinfo (). isavailable ();} If (! Flag) {alertdialog. builder = new alertdialog. builder (this); builder. seticon (Android. r. drawable. ic_dialog_alert); builder. settitle ("network status"); builder. setmessage ("the current network is unavailable. Are you sure you want to set it? "); Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {componentname comp = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); intent mintent = new intent (); mintent. setcomponent (COMP); mintent. setaction ("android. intent. action. view "); startactivity (mintent) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {dialog. cancel () ;}}); builder. create (); builder. show () ;}return flag ;}}

 

Note: This judgment applies only to codes before 3.0. For the corrected code, see:

Http://blog.csdn.net/lfdfhl/article/details/8365396

 

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.