android系統資訊擷取

來源:互聯網
上載者:User

一、記憶體(ram):

android的總記憶體大小資訊存放在系統的/proc/meminfo檔案裡面,可以通過讀取這個檔案來擷取這些資訊:

Java代碼

publicvoidgetTotalMemory(){

Stringstr1="/proc/meminfo";

Stringstr2="";

try{

FileReaderfr=newFileReader(str1);

BufferedReaderlocalBufferedReader=newBufferedReader(fr,8192);

while((str2=localBufferedReader.readLine())!=null){

Log.i(TAG,"---"+str2);

}

}catch(IOExceptione){

}

}

運行資訊如下:

Java代碼

05-3008:05:14.807:INFO/-SystemInfo-(1519):---MemTotal:204876kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---MemFree:4596kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---Buffers:16020kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---Cached:82508kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---SwapCached:64kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---Active:137104kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---Inactive:41056kB

05-3008:05:14.807:INFO/-SystemInfo-(1519):---SwapTotal:65528kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---SwapFree:65368kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---Dirty:88kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---Writeback:0kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---AnonPages:79672kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---Mapped:38296kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---Slab:5768kB

05-3008:05:14.817:INFO/-SystemInfo-(1519):---SReclaimable:1856kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---SUnreclaim:3912kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---PageTables:8184kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---NFS_Unstable:0kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---Bounce:0kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---CommitLimit:167964kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---Committed_AS:11771920kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---VmallocTotal:761856kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---VmallocUsed:83656kB

05-3008:05:14.827:INFO/-SystemInfo-(1519):---VmallocChunk:674820kB

第一行是總記憶體大小(即使用者可以使用的ram的大小)!

擷取當前剩餘記憶體(ram)大小的方法:

Java代碼

publiclonggetAvailMemory(){

ActivityManageram=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);

ActivityManager.MemoryInfomi=newActivityManager.MemoryInfo();

am.getMemoryInfo(mi);

returnmi.availMem;

}

二、Rom大小

Java代碼

publiclong[]getRomMemroy(){

long[]romInfo=newlong[2];

Fileroot=Environment.getRootDirectory();

StatFssf=newStatFs(root.getPath());

longbSize=sf.getBlockSize();

longbCount=sf.getBlockCount();

longavailBlocks=sf.getAvailableBlocks();

romInfo[0]=bSize*bCount;//總大小

romInfo[1]=bSize*availBlocks;//可用大小

returnromInfo;

}

注意類型,不然相乘之後會有溢出。

三、sdCard大小

Java代碼

publiclong[]getSDCardMemory(){

long[]sdCardInfo=newlong[2];

Stringstate=Environment.getExternalStorageState();

if(Environment.MEDIA_MOUNTED.equals(state)){

FilesdcardDir=Environment.getExternalStorageDirectory();

StatFssf=newStatFs(sdcardDir.getPath());

longbSize=sf.getBlockSize();

longbCount=sf.getBlockCount();

longavailBlocks=sf.getAvailableBlocks();

sdCardInfo[0]=bSize*bCount;//總大小

sdCardInfo[1]=bSize*availBlocks;//可用大小

}

returnsdCardInfo;

}

注意類型,不然相乘之後會有溢出。

四、電池電量

Java代碼

privateBroadcastReceiverbatteryReceiver=newBroadcastReceiver(){

@Override

publicvoidonReceive(Contextcontext,Intentintent){

intlevel=intent.getIntExtra("level",0);

//level加%就是當前電量了

}

};

然後在activity的oncreate()方法中註冊

Java代碼

1.registerReceiver(batteryReceiver,newIntentFilter(Intent.ACTION_BATTERY_CHANGED));

registerReceiver(batteryReceiver,newIntentFilter(Intent.ACTION_BATTERY_CHANGED));

五、CPU資訊

Java代碼

publicString[]getCpuInfo(){

Stringstr1="/proc/cpuinfo";

Stringstr2="";

String[]cpuInfo={"",""};

String[]arrayOfString;

try{

FileReaderfr=newFileReader(str1);

BufferedReaderlocalBufferedReader=newBufferedReader(fr,8192);

str2=localBufferedReader.readLine();

arrayOfString=str2.split("\\s+");

for(inti=2;i<arrayOfString.length;i++){

cpuInfo[0]=cpuInfo[0]+arrayOfString[i]+"";

}

str2=localBufferedReader.readLine();

arrayOfString=str2.split("\\s+");

cpuInfo[1]+=arrayOfString[2];

localBufferedReader.close();

}catch(IOExceptione){

}

returncpuInfo;

}

/proc/cpuinfo檔案中第一行是CPU的型號,第二行是CPU的頻率,可以通過讀檔案,讀取這些資料!

最後貼一個格式化資料的方法:

Java代碼

publicStringformatSize(longsize){

Stringsuffix=null;

floatfSize=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.DecimalFormatdf=newjava.text.DecimalFormat("#0.00");

StringBuilderresultBuffer=newStringBuilder(df.format(fSize));

if(suffix!=null)

resultBuffer.append(suffix);

returnresultBuffer.toString();

}

保留兩位小數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.