package com.luo.safemobile; import android.annotation.TargetApi; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.text.format.Formatter; import android.util.Log; import android.widget.TextView; public class AppMangerActivity extends Activity { private TextView tv_avail_rom; private TextView tv_avail_sd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_app_manger); tv_avail_rom=(TextView)findViewById(R.id.tv_avail_rom); tv_avail_sd=(TextView)findViewById(R.id.tv_avail_sd); long sdSize =0; sdSize = getAvailSpace(Environment.getExternalStorageDirectory().getAbsolutePath()); long romSize=getAvailSpace(Environment.getDataDirectory().getAbsolutePath());
//现在手机都不在支持sd扩充了,设置成手机内存和sd卡内存是一样的大小 tv_avail_sd.setText("sd卡的可用空间:"+ Formatter.formatFileSize(this,sdSize)); tv_avail_rom.setText("内存可用空间:"+Formatter.formatFileSize(this,romSize)); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private long getAvailSpace(String path){ StatFs statFs=new StatFs(path); long count=statFs.getAvailableBlocksLong(); long size=statFs.getBlockSizeLong(); return count*size; }}