一、RAM,ROM简单介绍
RAM为运行内存,比如,360手机助手的悬浮窗,经常提示的运行内存超过80%之类的,指的都是运行内存。一般大小为几个G。

ROM为存储数据的内存,比如,爱奇艺APP在视频页面显示的“总空间31.6G,剩余28.8G”,指的是ROM。一般大小几十G,几百G都有。ROM越大,可存储的视频,文件,音乐等越多。

 

二、Android 获取RAM,ROM的方法
1. 布局:很简单,就两个TextView

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView” />

<TextView
android:id=”@+id/textView2″
android:layout_marginTop=”10dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView” />

</LinearLayout>
2. java代码中对应的方法及说明:

public class MainActivity extends Activity{
private TextView tv1;
private TextView tv2;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory_test);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);

//获取运行内存的信息
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo info = new MemoryInfo();
manager.getMemoryInfo(info);
StringBuilder sb = new StringBuilder();
sb.append(“可用RAM:”);
sb.append(info.availMem + “B”);
sb.append(“,总RAM:”);
sb.append(info.totalMem + “B”);
sb.append(“\r\n”);
sb.append(Formatter.formatFileSize(getBaseContext(), info.availMem));
sb.append(“,”);
LogUtil.print(“totalMem:” + info.totalMem);
sb.append(Formatter.formatFileSize(getBaseContext(), info.totalMem));
tv1.setText(sb);

sb.setLength(0);
//获取ROM内存信息
//调用该类来获取磁盘信息(而getDataDirectory就是内部存储)
final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
long totalCounts = statFs.getBlockCountLong();//总共的block数
long availableCounts = statFs.getAvailableBlocksLong() ; //获取可用的block数
long size = statFs.getBlockSizeLong(); //每格所占的大小,一般是4KB==
long availROMSize = availableCounts * size;//可用内部存储大小
long totalROMSize = totalCounts *size; //内部存储总大小
sb.append(“可用block数:” + availableCounts);
sb.append(“block总数:” + totalCounts);
sb.append(“\r\n”);
sb.append(” 每个block大小:” + size);
sb.append(“\r\n”);
sb.append(” 可用ROM:” + availROMSize + “B”);
sb.append(” 总ROM:” + totalROMSize + “B”);
tv2.setText(sb);

}

}
3. 效果展示

%title插图%num