Notes for open source Chinese clients

Source: Internet
Author: User

1. Determine if the network is connected

Public boolean isNetworkConnected () {ConnectivityManager cm = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo ni = cm. getActiveNetworkInfo (); return ni! = Null & ni. isConnectedOrConnecting ();}

2. Determine the network type

/*** Get the current network type * @ return 0: No network 1: WIFI network 2: WAP network 3: NET Network */public int getNetworkType () {int netType = 0; connectivityManager connectivityManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo (); if (networkInfo = null) {return netType;} int nType = networkInfo. getType (); if (nType = ConnectivityManager. TYPE_MOB ILE) {String extraInfo = networkInfo. getExtraInfo (); if (! StringUtils. isEmpty (extraInfo) {if (extraInfo. toLowerCase (). equals ("cmnet") {netType = NETTYPE_CMNET;} else {netType = NETTYPE_CMWAP ;}} else if (nType = ConnectivityManager. TYPE_WIFI) {netType = NETTYPE_WIFI;} return netType ;}

3. Compare the mobile phone version

/*** Method for determining whether the current version is compatible with the Target version * @ param VersionCode * @ return */public static boolean isMethodsCompat (int VersionCode) {int currentVersion = android. OS. build. VERSION. SDK_INT; return currentVersion >=versioncode ;}

4. App installation package information

/*** Get App installation package information * @ return */public PackageInfo getPackageInfo () {PackageInfo info = null; try {info = getPackageManager (). getPackageInfo (getPackageName (), 0);} catch (NameNotFoundException e) {e. printStackTrace (System. err);} if (info = null) info = new PackageInfo (); return info ;}

5. Store basic application information

Public Properties get () {FileInputStream FCM = null; Properties props = new Properties (); try {// read the config // FCM = activity in the files directory. openFileInput (APP_CONFIG); // read configFile dirConf = mContext in the app_config directory. getDir (APP_CONFIG, Context. MODE_PRIVATE); FCM = new FileInputStream (dirConf. getPath () + File. separator + APP_CONFIG); props. load (FCM);} catch (Exception e) {} finally {try {FCM. close () ;}catch (Exception e) {}} return props;} private void setProps (Properties p) {FileOutputStream fos = null; try {// create config in the files Directory // fos = activity. openFileOutput (APP_CONFIG, Context. MODE_PRIVATE); // create the config in the (custom) app_config directory File dirConf = mContext. getDir (APP_CONFIG, Context. MODE_PRIVATE); File conf = new File (dirConf, APP_CONFIG); fos = new FileOutputStream (conf); p. store (fos, null); fos. flush ();} catch (Exception e) {e. printStackTrace ();} finally {try {fos. close () ;}catch (Exception e ){}}}

6. Read mobile phone and application-related information

String versionName = getPackageManager (). getPackageInfo (getPackageName (), 0 ). versionName; int versionCode = getPackageManager (). getPackageInfo (getPackageName (), 0 ). versionCode; String VERSION = android. OS. build. VERSION. RELEASE; // System version String MODEL = android. OS. build. MODEL; // The textView of the mobile phone MODEL. setText ("versionName" + versionName + "\ n" + "versionCode" + versionCode + "\ n" + "system VERSION" + VERSION + "\ n" + "mobile phone model" + MODEL );


Notes for open source Chinese clients

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.