[Android Trick 3] Getting the network status and entering the settings

Source: Internet
Author: User

Keywords: network status, system settings, system time, and time formatting first determine the network status of the system/*** determine the network connection status * @ return true, available; false, */private boolean isOpenNetwork () {ConnectivityManager connManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); if (connManager. getActiveNetworkInfo ()! = Null) {return connManager. getActiveNetworkInfo (). isAvailable ();} return false;} PS: You must add the network access permission [html] <uses-permission android: name = "android. permission. INTERNET "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_NETWORK_STATE "/> if there is no network, enter the setting interface [java] if (isOpenNetwork () = true) {} else {AlertDialog. builder builder = new AlertDialog. builder (SomeActivity. this); buil Der. setTitle ("title "). setMessage ("message"); builder. setPositiveButton (resources. getString (R. string. OK), new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {<span style = "white-space: pre"> </span> Intent intent = null; try {String sdkVersion = android. OS. build. VERSION. SDK; if (Integer. valueOf (sdkVersion)> 10) {intent = new Intent (android. pr Ovider. settings. ACTION_SETTINGS);} else {intent = new Intent (); ComponentName comp = new ComponentName ("com. android. settings "," com. android. settings. settings "); intent. setComponent (comp); intent. setAction ("android. intent. action. VIEW ");} SomeActivity. this. startActivity (intent);} catch (Exception e) {Log. v ("LOG", "open network settings failed, please check... "); <span style =" white-space: pre "> </Span> e. printStackTrace (); <span style = "white-space: pre" ></ span> }}}). setNegativeButton (resources. getString (R. string. no), new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. cancel (); finish ();}}). show ();} Here we will extend it through android. provider. settings can get a lot of information we need related to system Settings, such as whether the system time is 24 or 12 [java] ContentResolver cv = this. getContentResolver (); S Tring strTimeFormat = android. provider. settings. system. getString (cv, android. provider. settings. system. TIME_12_24); if (strTimeFormat. equals ("24") {Log. I ("activity", "24 .. This blog is quite complicated, so you can format the time and get the system time. [java] SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM "); // The Time Format String date = sdf. format (new java. util. date (); Use Calendar to obtain [java] Calendar c = Calendar. getInstance (); system date: year = c. get (Calendar. YEAR) month = c. grt (Calendar. MONTH) day = c. get (Calendar. DAY_OF_MONTH) obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY); minute = c. get (Calendar. MINUTE) Calendar c = Calen Dar. getInstance (); system date: year = c. get (Calendar. YEAR) month = c. grt (Calendar. MONTH) day = c. get (Calendar. DAY_OF_MONTH) obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY); minute = c. get (Calendar. MINUTE) use Time to obtain [java] Time t = new Time (); // or Time t = new Time ("GMT + 8"); plus Time Zone information. T. setToNow (); // obtain the system time. Int year = t. year; int month = t. month; int date = t. monthDay; int hour = t. hour; // 0-23 int minute = t. minute; int second = t. second;

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.