下面列出了怎么用android.app.ActivityManager.MemoryInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* watch for memory usage and heap size per seconds
*/
public void run() {
mTimerTask = new TimerTask() {
@Override
public void run() {
MemoryInfo memoryInfo = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(memoryInfo);
Runtime runtime = Runtime.getRuntime();
String msg = String.format("free:%s%% %sKB total:%sKB max:%sKB ", runtime.freeMemory() * 100f / runtime.totalMemory(),
runtime.freeMemory(), runtime.totalMemory() / 1024, runtime.maxMemory() / 1024);
msg += String.format("native: free:%sKB total:%sKB max:%sKB", android.os.Debug.getNativeHeapFreeSize() / 1024,
android.os.Debug.getNativeHeapAllocatedSize() / 1024, android.os.Debug.getNativeHeapSize() / 1024);
msg += String.format("| availMem:%sKB", memoryInfo.availMem / 1024);
Log.d("memory", msg);
}
};
mTimer = new Timer();
mTimer.schedule(mTimerTask, 1000, 1000);
}
public void run() {
mTimerTask = new TimerTask() {
@Override
public void run() {
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
Runtime runtime = Runtime.getRuntime();
String s = String.format("free:%s%% %sKB total:%sKB max:%sKB ", runtime.freeMemory() * 100f / runtime.totalMemory(), runtime.freeMemory(), runtime.totalMemory() / 1024,
runtime.maxMemory() / 1024);
// s += String.format("native: free:%sKB total:%sKB max:%sKB", android.os.Debug.getNativeHeapFreeSize() / 1024, android.os.Debug.getNativeHeapAllocatedSize() / 1024,
// android.os.Debug.getNativeHeapSize() / 1024);
// s += String.format("| availMem:%sKB", mi.availMem / 1024);
Log.d("memory", s);
}
};
mTimer = new Timer();
mTimer.schedule(mTimerTask, 1000, 1000);
}
public static Long getAvailMemory(Context cx) {// 获取android当前可用内存大小
if (cx == null) {
return 0L;
}
ActivityManager am = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
LogUtil.i(TAG, "Available memory: " + mi.availMem);
// mi.availMem; 当前系统的可用内存
return mi.availMem / BYTES_PER_MEGA;// 将获取的内存大小规格化
}
public static Long getTotalMemory(Context cx) {// 获取android全部内存大小
if (cx == null) {
return 0L;
}
ActivityManager am = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
LogUtil.i(TAG, "Total memory: " + mi.totalMem);
// mi.totalMem; 当前系统的全部内存
return mi.totalMem / BYTES_PER_MEGA;// 将获取的内存大小规格化
}
/**
* 获取总内存数据
* @return
*/
private Long getTotalMemory() {
if (activityManager == null) {
return 0L;
}
MemoryInfo info = new MemoryInfo();
activityManager.getMemoryInfo(info);
return info.totalMem / BYTES_PER_MEGA;
}
void updateStatus() {
final MemoryInfo mem_info = new ActivityManager.MemoryInfo();
final ActivityManager systemService = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);
systemService.getMemoryInfo(mem_info);
rightStatus.setText(String.format("Available memory: %s B", Util.nf.format((mem_info.availMem)) + "/" + Util.nf.format(mem_info.totalMem)));
//numProc_label.setText("Number of processes: " + display_process.size());
selectionStatusTV.setText(selectedInList1.size() + "/" + lpinfo.size() + "/" + display_process.size());
adapter.notifyDataSetChanged();
}
/**
* 获取设备的可用内存大小
*
* @param cxt 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
/**
* 获取设备的可用内存大小
*
* @param context 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private long getSizeTotalRAM(boolean isTotal) {
long sizeInBytes = 1000;
MemoryInfo mi = new MemoryInfo();
activityManager.getMemoryInfo(mi);
if(isTotal) {
try {
if(Utils.hasJellyBean()){
long totalMegs = mi.totalMem;
sizeInBytes = totalMegs;
}
else{
RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r");
String load = reader.readLine();
String[] totrm = load.split(" kB");
String[] trm = totrm[0].split(" ");
sizeInBytes=Long.parseLong(trm[trm.length-1]);
sizeInBytes=sizeInBytes*1024;
reader.close();
}
}
catch (Exception e) { }
}
else{
long availableMegs = mi.availMem;
sizeInBytes = availableMegs;
}
return sizeInBytes;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private long getSizeTotalRAM(boolean isTotal) {
long sizeInBytes = 1000;
MemoryInfo mi = new MemoryInfo();
activityManager.getMemoryInfo(mi);
if(isTotal) {
try {
if(Utils.hasJellyBean()){
long totalMegs = mi.totalMem;
sizeInBytes = totalMegs;
}
else{
RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r");
String load = reader.readLine();
String[] totrm = load.split(" kB");
String[] trm = totrm[0].split(" ");
sizeInBytes=Long.parseLong(trm[trm.length-1]);
sizeInBytes=sizeInBytes*1024;
reader.close();
}
}
catch (Exception e) { }
}
else{
long availableMegs = mi.availMem;
sizeInBytes = availableMegs;
}
return sizeInBytes;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private long getSizeTotalRAM(boolean isTotal) {
long sizeInBytes = 1000;
MemoryInfo mi = new MemoryInfo();
activityManager.getMemoryInfo(mi);
if(isTotal) {
try {
if(Utils.hasJellyBean()){
long totalMegs = mi.totalMem;
sizeInBytes = totalMegs;
}
else{
RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r");
String load = reader.readLine();
String[] totrm = load.split(" kB");
String[] trm = totrm[0].split(" ");
sizeInBytes=Long.parseLong(trm[trm.length-1]);
sizeInBytes=sizeInBytes*1024;
reader.close();
}
}
catch (Exception e) { }
}
else{
long availableMegs = mi.availMem;
sizeInBytes = availableMegs;
}
return sizeInBytes;
}
/**
* 获取设备的可用内存大小
*
* @param cxt 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
private static long getRamSizeAvailable(Context context) {
long ramSize = 0;
ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo memInfo = new MemoryInfo();
actManager.getMemoryInfo(memInfo);
ramSize = memInfo.availMem / (1024 * 1024);
return ramSize;
}
/**
* 描述:获取可用内存.
*/
public static long getAvailMemory(Context context) {
// 获取android当前可用内存大小
ActivityManager activityManager
= (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// 当前系统可用内存 ,将获得的内存大小规格化
return memoryInfo.availMem;
}
/**
* 获取设备的可用内存大小
*
* @param cxt
* 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
/**
* 得到当前可用内存大小
*
* @param context
* @return
*/
public static long getAvailMemory(Context context) {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;// 将获取的内存大小规格化
}
/**
* 获取设备的可用内存大小
*
* @param cxt 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
public long GetSurplusMemory() {
info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
long MemorySize = info.availMem;
MemorySurPlus = (float) MemorySize / 1024 / 1024;
return MemorySize;
}
/**
* 获取设备的可用内存大小
*
* @param cxt
* 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context cxt) {
ActivityManager am = (ActivityManager) cxt
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
protected AndroidCompatibility(AndroidLauncher androidLauncher) {
super(androidLauncher, Application.ApplicationType.Android);
this.androidLauncher = androidLauncher;
modLoader = new AndroidModLoader(this);
activityManager = (ActivityManager) androidLauncher.getSystemService(Activity.ACTIVITY_SERVICE);
memoryInfo = new MemoryInfo();
}
/**
* 获取可用的手机内存
* @param context 上下文
* @return
*/
public static long getAvailRAM(Context context){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo outInfo = new MemoryInfo();
am.getMemoryInfo(outInfo);
return outInfo.availMem;
}
public Integer getFreeRAM()
{
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
return (int) (mi.availMem / 1048576L);
}
public static long getAvailMemory(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
public static long getMemUnused(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService("activity");
MemoryInfo memoryInfo = new MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.availMem / 1024;
}
public static long calculateFreeRamInBytes(Context context) {
MemoryInfo mi = new MemoryInfo();
((ActivityManager) context.getSystemService("activity")).getMemoryInfo(mi);
return mi.availMem;
}
public static int GetRam(boolean total, Context context) {
MemoryInfo mMemoryInfo = new MemoryInfo();
((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(mMemoryInfo);
return (int)((total ? mMemoryInfo.totalMem : mMemoryInfo.availMem) >> 20); // log2(1024*1024) = 20
}
public static long getAvailMemory(Context context) {
ActivityManager activityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem / (1024 * 1024);
}
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public String getDeviceInfo() {
try {
if (activityManager == null) {
return jsonError("ActivityManager not set. Cannot retrieve RAM info.");
}
if (connectivityManager == null) {
return jsonError("ConnectivityManager not set. Cannot retrieve network info.");
}
String versionName = parent.getPackageManager()
.getPackageInfo(parent.getPackageName(), 0)
.versionName;
JSONObject appObject = new JSONObject();
appObject.put("version", versionName);
String androidVersion = Build.VERSION.RELEASE;
int osApiLevel = Build.VERSION.SDK_INT;
String osVersion = System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")";
JSONObject softwareObject = new JSONObject();
softwareObject
.put("androidVersion", androidVersion)
.put("osApiLevel", osApiLevel)
.put("osVersion", osVersion);
String device = Build.DEVICE;
String model = Build.MODEL;
String manufacturer = Build.BRAND;
String hardware = Build.HARDWARE;
Map<String, String> cpuInfo = getCPUInfo();
JSONObject hardwareObject = new JSONObject();
hardwareObject
.put("device", device)
.put("model", model)
.put("manufacturer", manufacturer)
.put("hardware", hardware)
.put("cpuInfo", new JSONObject(cpuInfo));
File dataDirectory = Environment.getDataDirectory();
StatFs dataDirectoryStat = new StatFs(dataDirectory.getPath());
long dataDirectoryBlockSize = dataDirectoryStat.getBlockSizeLong();
long dataDirectoryAvailableBlocks = dataDirectoryStat.getAvailableBlocksLong();
long dataDirectoryTotalBlocks = dataDirectoryStat.getBlockCountLong();
long freeMemorySize = dataDirectoryAvailableBlocks * dataDirectoryBlockSize;
long totalMemorySize = dataDirectoryTotalBlocks * dataDirectoryBlockSize;
JSONObject storageObject = new JSONObject();
storageObject
.put("free", freeMemorySize)
.put("total", totalMemorySize);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalRAMSize = memoryInfo.totalMem;
long freeRAMSize = memoryInfo.availMem;
long thresholdRAM = memoryInfo.threshold;
JSONObject ramObject = new JSONObject();
ramObject
.put("free", freeRAMSize)
.put("total", totalRAMSize)
.put("threshold", thresholdRAM);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
JSONObject networkObject = new JSONObject();
if (netInfo != null && Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
int downSpeed = networkCapabilities.getLinkDownstreamBandwidthKbps();
int upSpeed = networkCapabilities.getLinkUpstreamBandwidthKbps();
networkObject
.put("downSpeed", downSpeed)
.put("upSpeed", upSpeed);
}
return new JSONObject()
.put("app", appObject)
.put("software", softwareObject)
.put("hardware", hardwareObject)
.put("storage", storageObject)
.put("ram", ramObject)
.put("network", networkObject)
.toString();
} catch(Exception ex) {
return jsonError("Problem fetching device info: ", ex);
}
}
/**
* Calculates and returns the amount of free RAM in bytes.
*
* @param context used to acquire the necessary resources for calculating free RAM
* @return Amount of free RAM in bytes
*/
public static long calculateFreeRamInBytes(Context context) {
final MemoryInfo mi = new MemoryInfo();
((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(mi);
return mi.availMem;
}