Android Development Study Notes (12) Get available system memory

Source: Internet
Author: User

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:

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.