类android.app.ActivityManager.MemoryInfo源码实例Demo

下面列出了怎么用android.app.ActivityManager.MemoryInfo的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: Utils   文件: AppWatcher.java
/**
 * 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);
}
 
源代码2 项目: cube-sdk   文件: SystemWatcher.java
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);
}
 
源代码3 项目: SoloPi   文件: MemoryTools.java
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;// 将获取的内存大小规格化
}
 
源代码4 项目: SoloPi   文件: MemoryTools.java
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;// 将获取的内存大小规格化
}
 
源代码5 项目: SoloPi   文件: MemoryTools.java
/**
 * 获取总内存数据
 * @return
 */
private Long getTotalMemory() {
	if (activityManager == null) {
		return 0L;
	}

	MemoryInfo info = new MemoryInfo();

	activityManager.getMemoryInfo(info);

	return info.totalMem / BYTES_PER_MEGA;
}
 
源代码6 项目: PowerFileExplorer   文件: ProcessFragment.java
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();
}
 
源代码7 项目: FriendBook   文件: SystemTool.java
/**
 * 获取设备的可用内存大小
 *
 * @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));
}
 
源代码8 项目: SmartChart   文件: AppUtils.java
/**
 * 获取设备的可用内存大小
 *
 * @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));
}
 
源代码9 项目: FireFiles   文件: StorageUtils.java
@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;
}
 
源代码10 项目: FireFiles   文件: StorageUtils.java
@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;
}
 
源代码11 项目: FireFiles   文件: StorageUtils.java
@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;
}
 
源代码12 项目: Common   文件: SystemTool.java
/**
 * 获取设备的可用内存大小
 *
 * @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));
}
 
源代码13 项目: pandroid   文件: SystemUtils.java
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;
}
 
源代码14 项目: MemoryCleaner   文件: AppUtils.java
/**
 * 描述:获取可用内存.
 */
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;
}
 
源代码15 项目: VideoMeeting   文件: SystemUtils.java
/**
 * 获取设备的可用内存大小
 * 
 * @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));
}
 
源代码16 项目: BalloonPerformer   文件: PhoneMemoryUtil.java
/**
 * 得到当前可用内存大小
 * 
 * @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;// 将获取的内存大小规格化
}
 
源代码17 项目: Lay-s   文件: SystemTool.java
/**
 * 获取设备的可用内存大小
 *
 * @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;
}
 
源代码19 项目: KJFrameForAndroid   文件: SystemTool.java
/**
 * 获取设备的可用内存大小
 * 
 * @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));
}
 
源代码20 项目: Cubes   文件: AndroidCompatibility.java
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();
}
 
源代码21 项目: weixin   文件: SystemInfoUtils.java
/**
 * 获取可用的手机内存
 * @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;
}
 
源代码22 项目: Kernel-Tuner   文件: TaskManager.java
public Integer getFreeRAM()
{
	MemoryInfo mi = new MemoryInfo();
	ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	activityManager.getMemoryInfo(mi);
       return (int) (mi.availMem / 1048576L);
}
 
源代码23 项目: FimiX8-RE   文件: AbAppUtil.java
public static long getAvailMemory(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
    MemoryInfo memoryInfo = new MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);
    return memoryInfo.availMem;
}
 
源代码24 项目: letv   文件: MemoryInfoUtil.java
public static long getMemUnused(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService("activity");
    MemoryInfo memoryInfo = new MemoryInfo();
    am.getMemoryInfo(memoryInfo);
    return memoryInfo.availMem / 1024;
}
 
源代码25 项目: letv   文件: CommonUtils.java
public static long calculateFreeRamInBytes(Context context) {
    MemoryInfo mi = new MemoryInfo();
    ((ActivityManager) context.getSystemService("activity")).getMemoryInfo(mi);
    return mi.availMem;
}
 
源代码26 项目: KA27   文件: Ram.java
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
}
 
源代码27 项目: mobile-manager-tool   文件: StorageUtil.java
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);
}
 
源代码28 项目: medic-android   文件: MedicAndroidJavascript.java
@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);
	}
}
 
源代码29 项目: firebase-android-sdk   文件: CommonUtils.java
/**
 * 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;
}
 
 类所在包
 同包方法