获取android手机总内存和可用内存的一些方法

   网上有很多方法可以获取内存的方法,我收集了一下具体如下,可以参考这位老兄的博客。

第一种方法:

http://blog.csdn.net/sxwyf248/archive/2010/11/02/5981251.aspx

第二种方法:

是我在一个论坛里找到的,是一个高手用java的反射机制来获取手机的内存的一些信息。

具体代码如下。

public class GetFreeMem extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Method _readProclines = null; try { Class procClass; procClass = Class.forName("android.os.Process"); Class parameterTypes[]= new Class[] {String.class, String[].class, long[].class }; _readProclines = procClass.getMethod("readProcLines", parameterTypes); Object arglist[] = new Object[3]; final String[] mMemInfoFields = new String[] {"MemTotal:", "MemFree:", "Buffers:", "Cached:"}; long[] mMemInfoSizes = new long[mMemInfoFields.length]; mMemInfoSizes[0] = 30; mMemInfoSizes[1] = -30; arglist[0] = new String("/proc/meminfo"); arglist[1] = mMemInfoFields; arglist[2] = mMemInfoSizes; if(_readProclines!=null){ _readProclines.invoke(null, arglist); for (int i=0; i<mMemInfoSizes.length; i++) { Log.d("GetFreeMem", mMemInfoFields[i]+" : "+mMemInfoSizes[i]/1024); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } 

 

效果如图:

你可能感兴趣的:(android,String,object,null,Class,手机)