編輯:關於Android編程
android的總內存大小信息存放在系統的/proc/meminfo文件裡面,可以通過讀取這個文件來獲取這些信息:
public void getTotalMemory() { String str1 = "/proc/meminfo"; String str2=""; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); while ((str2 = localBufferedReader.readLine()) != null) { Log.i(TAG, "---" + str2); } } catch (IOException e) { } }運行信息如下: 第一行是總內存大小(即用戶可以使用的ram的大小)
11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemTotal: 204876 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree: 4596 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers: 16020 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached: 82508 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached: 64 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active: 137104 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive: 41056 kB 11-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty: 88 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback: 0 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped: 38296 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab: 5768 kB 11-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables: 8184 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce: 0 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB 11-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocChunk: 674820 kB獲取當前剩余內存(ram)大小的方法 :
public long getAvailMemory() { ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return mi.availMem; }
二、Rom大小
public long[] getRomMemroy() { long[] romInfo = new long[2]; //Total rom memory romInfo[0] = getTotalInternalMemorySize(); //Available rom memory File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); romInfo[1] = blockSize * availableBlocks; getVersion(); return romInfo; } public long getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; }
網上傳的很多都是用getRootDirectory()取得的,我測試之後發現取得的數值不對。要根據getDataDirectory();取得。
三、sdCard大小 注意類型,不然相乘之後會有溢出。
public long[] getSDCardMemory() { long[] sdCardInfo=new long[2]; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { File sdcardDir = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(sdcardDir.getPath()); long bSize = sf.getBlockSize(); long bCount = sf.getBlockCount(); long availBlocks = sf.getAvailableBlocks(); sdCardInfo[0] = bSize * bCount;//總大小 sdCardInfo[1] = bSize * availBlocks;//可用大小 } return sdCardInfo; }四、電池電量
private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra("level", 0); // level加%就是當前電量了 } };然後在activity的oncreate()方法中注冊
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
adb shell (connect to your Android device shell) cat /proc/cpuinfo (read the CPU info)
private String getInfo() { StringBuffer sb = new StringBuffer(); sb.append("abi: ").append(Build.CPU_ABI).append("\n"); if (new File("/proc/cpuinfo").exists()) { try { BufferedReader br = new BufferedReader(new FileReader(new File("/proc/cpuinfo"))); String aLine; while ((aLine = br.readLine()) != null) { sb.append(aLine + "\n"); } if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
public String[] getVersion(){ String[] version={"null","null","null","null"}; String str1 = "/proc/version"; String str2; String[] arrayOfString; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); version[0]=arrayOfString[2];//KernelVersion localBufferedReader.close(); } catch (IOException e) { } version[1] = Build.VERSION.RELEASE;// firmware version version[2]=Build.MODEL;//model version[3]=Build.DISPLAY;//system version return version; }版本信息裡面還包括型號等信息。
public String[] getOtherInfo(){ String[] other={"null","null"}; WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if(wifiInfo.getMacAddress()!=null){ other[0]=wifiInfo.getMacAddress(); } else { other[0] = "Fail"; } other[1] = getTimes(); return other; } private String getTimes() { long ut = SystemClock.elapsedRealtime() / 1000; if (ut == 0) { ut = 1; } int m = (int) ((ut / 60) % 60); int h = (int) ((ut / 3600)); return h + " " + mContext.getString(R.string.info_times_hour) + m + " " + mContext.getString(R.string.info_times_minute); }最後貼一個格式化數據的方法: 保留兩位小數。
public String formatSize(long size) { String suffix = null; float fSize=0; if (size >= 1024) { suffix = "KB"; fSize=size / 1024; if (fSize >= 1024) { suffix = "MB"; fSize /= 1024; } if (fSize >= 1024) { suffix = "GB"; fSize /= 1024; } } else { fSize = size; } java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); StringBuilder resultBuffer = new StringBuilder(df.format(fSize)); if (suffix != null) resultBuffer.append(suffix); return resultBuffer.toString(); }
錄制音頻的步驟: 1、創建錄音對象 2、指定錄音設備(初始化狀態) 3、設置錄制音頻的碼率 4、設置錄制音頻的編碼格式 5、設置錄制音頻存放的位置 6、准備錄音(准備狀態
本文介紹微信自動搶紅包的實現方法,主要實現以下幾個功能: 1.自動拆開屏幕上出現的紅包
今天用了AS混淆項目,工程使用了fastjson解析數據,混淆出現以下問題:Warning:com.alibaba.fastjson.support.jaxrs.Fast
最近一直在研究android wear SDK,總體感受來說就是和現有的android 其他的開發SDK還是有很多新的東西。例如手機終端與手表端的通信機制,手表端的UI規