package com.aiven.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import com.wy.framework.util.EView;
/**
*
* @author Aiven
*
*/
@EView(Layout = R.layout.main2)
public class MainActivity extends BaseActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = (TextView) findViewById(R.id.myTest);
mTextView.setText("总内存:" + getmem_TOLAL() + " 剩余内存:"
+ getAvailMemory() + " " + getSdkMessage()+" "+getOutSdkMessage());
}
// 获得手机自身总内存
public String getmem_TOLAL() {
long mTotal;
// /proc/meminfo读出的内核信息进行解释
String path = "/proc/meminfo";
String content = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path), 8);
String line;
if ((line = br.readLine()) != null) {
content = line;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// beginIndex
int begin = content.indexOf(':');
// endIndex
int end = content.indexOf('k');
// 截取字符串信息
content = content.substring(begin + 1, end).trim();
mTotal = Integer.parseInt(content) * 1024;
return Formatter.formatFileSize(getBaseContext(), mTotal);
}
/**
* 获取当前手机自身可用内存
* @return
*/
private String getAvailMemory() {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// mi.availMem; 当前系统的可用内存
return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
}
/**
* 获取内置SDK信息
* @return
*/
private String getSdkMessage() {
/** 获取存储卡路径 */
File sdcardDir = Environment.getExternalStorageDirectory();
/** StatFs 看文件系统空间使用情况 */
StatFs statFs = new StatFs(sdcardDir.getPath());
/** Block 的 size */
long blockSize = statFs.getBlockSize();
/** 总 Block 数量 */
long totalBlocks = statFs.getBlockCount();
/** 已使用的 Block 数量 */
long availableBlocks = statFs.getAvailableBlocks();
File[] list = sdcardDir.listFiles();
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].getName() + " | "
+ list[i].getAbsolutePath());
}
return "SD卡存储信息:总空间->"
+ Formatter.formatFileSize(getBaseContext(), totalBlocks
* blockSize)
+ " 剩余空间:"
+ Formatter.formatFileSize(getBaseContext(), availableBlocks
* blockSize);
}
/**
* 获取外部扩展存储卡信息, 可能会有问题
* @return
*/
private String getOutSdkMessage() {
/** 获取存储卡路径 */
File sdcardDir = new File("/mnt");
File[] list = sdcardDir.listFiles();
String path=null;
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
if (list[i].getName().toLowerCase().contains("ext")
&& list[i].getName().toLowerCase().contains("sd")) {
path=list[i].getAbsolutePath();
break;
}
}
}
if(path==null){
return "不存在外部扩展卡";
}
/** StatFs 看文件系统空间使用情况 */
StatFs statFs = new StatFs(path);
/** Block 的 size */
long blockSize = statFs.getBlockSize();
/** 总 Block 数量 */
long totalBlocks = statFs.getBlockCount();
/** 已使用的 Block 数量 */
long availableBlocks = statFs.getAvailableBlocks();
return "外部存储卡:总空间->"
+ Formatter.formatFileSize(getBaseContext(), totalBlocks
* blockSize)
+ " 剩余空间:"
+ Formatter.formatFileSize(getBaseContext(), availableBlocks
* blockSize);
}
}