Android System Information Retrieval

Source: Internet
Author: User

I. Memory (RAM ):

The total memory size information of Android is stored in the/proc/meminfo file of the system. You can obtain this information by reading this file:

JavaCode

Publicvoidgettotalmemory (){

Stringstr1 = "/proc/meminfo ";

Stringstr2 = "";

Try {

Filereaderfr = newfilereader (str1 );

Bufferedreaderlocalbufferedreader = newbufferedreader (FR, 8192 );

While (str2 = localbufferedreader. Readline ())! = NULL ){

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

}

} Catch (io1_tione ){

}

}

The running information is as follows:

Java code

05-3008: 05: 14.807: INFO/-systeminfo-(1519): --- memtotal: 203476kb

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: 13720.kb

05-3008: 05: 14.807: INFO/-systeminfo-(1519): --- inactive: 56kb

05-3008: 05: 14.807: INFO/-systeminfo-(1519): --- swaptotal: 65528kb

05-3008: 05: 14.817: INFO/-systeminfo-(1519): --- swapfree: 65366kb

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

The first line is the total memory size (that is, the size of ram that users can use )!

To obtain the size of the remaining memory (RAM:

Java code

Publiclonggetavailmemory (){

Activitymanageram = (activitymanager) mcontext. getsystemservice (context. activity_service );

Activitymanager. memoryinfomi = newactivitymanager. memoryinfo ();

Am. getmemoryinfo (MI );

Returnmi. availmem;

}

Ii. Rom size

Java code

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; // The total size.

Rominfo [1] = bsize * availblocks; // available size

Returnrominfo;

}

Pay attention to the type, otherwise there will be overflow after multiplication.

Iii. sdcard size

Java code

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; // total size

Sdcardinfo [1] = bsize * availblocks; // available size

}

Returnsdcardinfo;

}

Pay attention to the type, otherwise there will be overflow after multiplication.

Iv. battery power

Java code

Privatebroadcastreceiverbatteryreceiver = newbroadcastreceiver (){

@ Override

Publicvoidonreceive (contextcontext, intentintent ){

Intlevel = intent. getintextra ("level", 0 );

// Level plus % is the current power

}

};

Then register in the oncreate () method of activity

Java code

1. registerreceiver (batteryreceiver, newintentfilter (intent. action_battery_changed ));

Registerreceiver (batteryreceiver, newintentfilter (intent. action_battery_changed ));

5. CPU Information

Java code

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 (io1_tione ){

}

Returncpuinfo;

}

The first line in the/proc/cpuinfo file is the CPU model, and the second line is the CPU frequency. You can read the data by reading the file!

Finally, paste a method to format the data:

Java code

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 ();

}

Retain two decimal places.

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.