2012年12月28日星期五

讀取內存信息, MemoryInfo

我們可以通過 android.app.ActivityManager.MemoryInfo 讀取內存信息.

讀取內存信息, MemoryInfo


package com.example.androidmemoryinfo;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);
  
  TextView tv = new TextView(this);
  setContentView(tv);
  
  ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
  MemoryInfo memoryInfo = new MemoryInfo();
  activityManager.getMemoryInfo(memoryInfo);
  
  String info = 
   "available memory : " + String.valueOf(memoryInfo.availMem) + "\n"
   + "currently be in a low memory : " + String.valueOf(memoryInfo.lowMemory) + "\n"
   + "threshold low memory : " + String.valueOf(memoryInfo.threshold) + "\n"
   + "total memory accessible by the kernel : " + String.valueOf(memoryInfo.totalMem);
  
  tv.setText(info);
 }

}


沒有留言:

發佈留言