Android obtains memory information

Source: Internet
Author: User

 

<P> </p> <p>

Import java. io. BufferedReader;

Import java. io. FileReader; </p> <p> import android. app. Activity;

Import android. app. ActivityManager;

Import android. OS. Bundle;

Import android. OS. Handler;

Import android. OS. Message;

Import android. widget. TextView; </p> <p> public class SystemInfo extends Activity {

Private TextView textView, textView2;

ActivityManager am;

ActivityManager. MemoryInfo memoryInfo;

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. sysinfo );

TextView = (TextView) findViewById (R. id. sys_textView1 );

TextView2 = (TextView) findViewById (R. id. sys_textView2 );

Am = (ActivityManager) getSystemService (ACTIVITY_SERVICE );

MemoryInfo = new ActivityManager. MemoryInfo ();

Am. getMemoryInfo (memoryInfo );

TextView2.setText ("maximum local memory:" + GetTotalMemory () + "M ");

UpdateMemory Update = new updatemememory ();

Update. start ();

}

Class UpdateMemory extends Thread {

Public void run (){

While (true ){

Message message = new Message ();

// Message. obj = GetAvailableMemory (memoryInfo. availMem );

Message. what = 1;

Handler. sendMessage (message );

Try {

Thread. sleep (1000 );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

}

}

Handler handler = new Handler () {</p> <p> public void handleMessage (Message msg ){

// TODO Auto-generated method stub

If (msg. what = 1 ){

TextView. setText ("memory available on the local machine:" + GetAvailableMemory (memoryInfo. availMem) + "M ");

}

Super. handleMessage (msg );

}

};

Private Double GetTotalMemory (){

String str1 = "/proc/meminfo ";

String str2;

Double memory = 0.0;

Try {

FileReader r = new FileReader (str1 );

BufferedReader bufferedRead = new BufferedReader (r, 8192 );

Str2 = bufferedRead. readLine ();

System. err. println ("str2 --------->" + str2 );

// ArrayOfString = str2.split ("// s + ");

/// String str3 = str2.trim ();

// System. out. println ("----------------------->" + str3 );

String str4 = str2.substring (str2.length ()-9, str2.length ()-3 );

// System. out. println (str4 + "" + str4.length ());

Memory = Double. parseDouble (str4)/1000;

} Catch (Exception e ){

// TODO: handle exception

}

Return memory;

}

Private double GetAvailableMemory (long num)

{

Double M;

M = (num/1024)/1024;

Return M;

}

} </P>

 

 

From Viagra ~ YZ

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.