開源中國用戶端筆記記錄

來源:互聯網
上載者:User

標籤:開源中國   應用   安卓   手機   類   

1、判斷是否連網

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

2、判斷網路類型

/** * 擷取當前網路類型 * @return 0:沒有網路   1:WIFI網路   2:WAP網路    3:NET網路 */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_MOBILE) {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、對比手機版本

/** * 判斷目前的版本是否相容目標版本的方法 * @param VersionCode * @return */public static boolean isMethodsCompat(int VersionCode) {int currentVersion = android.os.Build.VERSION.SDK_INT;return currentVersion >= VersionCode;}

4、App安裝包資訊

/** * 擷取App安裝包資訊 * @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、儲存應用基本資料

public Properties get() {FileInputStream fis = null;Properties props = new Properties();try{//讀取files目錄下的config//fis = activity.openFileInput(APP_CONFIG);//讀取app_config目錄下的configFile dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);fis = new FileInputStream(dirConf.getPath() + File.separator + APP_CONFIG);props.load(fis);}catch(Exception e){}finally{try {fis.close();} catch (Exception e) {}}return props;}private void setProps(Properties p) {FileOutputStream fos = null;try{//把config建在files目錄下//fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE);//把config建在(自訂)app_config的目錄下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、讀取手機及應用相關資訊

String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;String VERSION = android.os.Build.VERSION.RELEASE;// 系統版本String MODEL = android.os.Build.MODEL;// 手機型號textView.setText("versionName" + versionName + "\n" + "versionCode"+ versionCode + "\n" + "系統版本" + VERSION + "\n" + "手機型號"+ MODEL);


開源中國用戶端筆記記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.