Android adb方式获取手机总内存和可用内存信息
package ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends AppCompatActivity {
TextView tv1 = null;
TextView tv2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(.activity_main);
tv1 = findViewById(.system_memory1);
tv2 = findViewById(.system_memory2);
//第一种方式:
("第一种方式:手机总内存: " + this.getTotalMemory() + ", " + "可用内存: "
+ this.getAvailMemory());
//第二种方法:手用java的反射机制来获取手机的内存的一些信息。
show();
}
private String getAvailMemory() {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
(mi);
//; 当前系统的可用内存
return (getBaseContext(), );// 将获取的内存大小规格化
}
private String getTotalMemory() {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = ();// 读取meminfo第一行,系统总内存大小
arrayOfString = ("\\s+");
for (String num : arrayOfString) {
(str2, num + "\t");
}
initial_memory = (arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
();
} catch (IOException e) {
}
return (getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化
}
public void show(){
Method _readProclines = null;
try {
Class procClass;
procClass = ("");
Class parameterTypes[]= new Class[] {, String[].class, long[].class };
_readProclines = ("readProcLines", parameterTypes);
Object arglist[] = new Object[3];
final String[] mMemInfoFields = new String[] {"MemTotal:",
"MemFree:", "Buffers:", "Cached:"};
long[] mMemInfoSizes = new long[];
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<; i++) {
("GetFreeMem", mMemInfoFields[i]+" : "+mMemInfoSizes[i]/1024);
("第二种方式:手机总内存: " + this.getTotalMemory() + ", " + "可用内存: "
+ this.getAvailMemory());
}
}
} catch (Exception e) {
();
}
}
}