To obtain the available memory of a mobile phone, you must first obtain the system service information,
Activitymanager myactivitymanager = (activitymanager) getsystemservice (activity. activity_service );
Then obtain the memoryinfo type object.
Activitymanager. memoryinfo = new activitymanager. memoryinfo ();
Then, use the getmemoryinfo (memoryinfo) method to obtain the available memory of the system. This method saves the memory size on the memoryinfo object.
Myactivitymanager. getmemoryinfo (memoryinfo );
Then, the availmem value on the memoryinfo object is
Long memsize = memoryinfo. availmem;
Convert the character type to the MB format.
String leftmemsize = formatter. formatfilesize (getbasecontext (), memsize );
Public static string formatfilesize (context,
Long number) added in API level 3
Formats a content size to be in the form of bytes, kilobytes, megabytes, etc
Parameters
Context |
Context to use to load the localized units |
Number |
Size value to be formatted |
Returns
- Formatted string with the number
The first parameter is the context, and the second parameter is the size of Long-type files to be converted. Finally, a string similar to 22kb, 52 bytes, and 22 MB is returned.
Java file mainactivity. Java
Package COM. xujin. availablemem; import android. app. activity; import android. app. activitymanager; import android. OS. bundle; import android. text. format. formatter; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private activitymanager myactivitymanager; private textview leftmem; private button refresh; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); leftmem = (textview) findviewbyid (R. id. avamem); refresh = (button) findviewbyid (R. id. refresh); // obtain system service information. myactivitymanager = (activitymanager) getsystemservice (activity. activity_service); updatememinfo (); refresh. setonclicklistener (New onclicklistener () {public void onclick (View Source) {updatememinfo () ;}) ;}// update available memory information public void updatememinfo () {// get the memoryinfo object activitymanager. memoryinfo = new activitymanager. memoryinfo (); // obtain the available memory of the system and save it to the memoryinfo object myactivitymanager. getmemoryinfo (memoryinfo); long memsize = memoryinfo. availmem; // character type conversion string leftmemsize = formatter. formatfilesize (getbasecontext (), memsize); leftmem. settext (leftmemsize );}}
XML file, activity_main.xml
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: text = "available system memory:"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: Id = "@ + ID/avamem"/> </linearlayout> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "refresh" Android: Id = "@ + ID/refresh"/> </linearlayout>
Final result: