您的位置:首页 > 其它

安卓学习笔记之读取内存信息

2016-04-01 18:45 351 查看

安卓学习笔记之读取内存信息

通过context.getSystemService(Context.ACTIVITY_SERVICE)获得ActivityManager对象

通过ActivityManager对象获得相关信息

获取正在运行的app个数

/**
* 获取正在运行的app个数
* @return
*/
public static int  getRunningAppCount(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int count = am.getRunningAppProcesses().size();
return count;
}


获取手机可用内存信息 单位是bit

/**
* 获取手机可用内存信息 单位是bit
* @param context
*/
public static long getFreeMemoryInfo(Context context){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo outInfo = new MemoryInfo();
am.getMemoryInfo(outInfo );
long availMem = outInfo.availMem;
//      long totalMem = outInfo.totalMem;  //低版本手机不可用
return availMem;
}


获取总的可用内存 单位是bit

从/proc/meminfo目录中读取信息

MemTotal: 1031016 kB

MemFree: 13548 kB

MemShared: 0 kB

Buffers: 98064 kB

Cached: 692320 kB

SwapCached: 2244 kB

Active: 563112 kB

Inact_dirty: 309584 kB

Inact_clean: 79508 kB

Inact_target: 190440 kB

HighTotal: 130992 kB

HighFree: 1876 kB

LowTotal: 900024 kB

LowFree: 11672 kB

SwapTotal: 1052248 kB

SwapFree: 1043908 kB

Committed_AS: 332340 kB

/**
* 获取总的可用内存 单位是bit
* @return
*/
public static long getTotalMemoryInfo() {
File file = new File("/proc/meminfo");
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String buff;
while ((buff = br.readLine()) != null) {
if (buff.startsWith("MemTotal")) {
String[] s = buff.split(" +");
return Long.parseLong(s[1])*1024;  // 获得的buff单位是kb 故要乘以1024转成b
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
}


获取正在运行的应用信息

public static List<AppInfo> getRunningAppInfos(Context context) {
List<AppInfo> runningAppInfos = new ArrayList<AppInfo>();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> infos =am.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : infos) {
String processName = runningAppProcessInfo.processName;
//获取应用占用内存信息  这个里面一共只有一个数据
android.os.Debug.MemoryInfo[]  mem = am.getProcessMemoryInfo(new int[]{runningAppProcessInfo.pid});
//          Log.e("------>", mem[0]+"----");
// 占用的内存
int memorySize = mem[0].getTotalPrivateDirty() * 1024;  //单位kb 需乘以1024

PackageManager pm = context.getPackageManager();
try {
ApplicationInfo appInfo = pm.getApplicationInfo(processName,0);
String packageName = appInfo.packageName;
String appName = (String) appInfo.loadLabel(pm);
Drawable icon = appInfo.loadIcon(pm);
boolean isUserApp = (appInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0?true:false;
AppInfo info = new AppInfo(appName, packageName, icon, isUserApp, memorySize);
//              Log.e("-------->", "appName:"+appName+"packageName:"+packageName+"memorySize:"+memorySize);
runningAppInfos.add(info);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}

return runningAppInfos;
}


判断服务是否在后台运行

/**
* 判断服务是否在后台运行
* @param context
* @param serviceName
* @return
*/
public static boolean isRunning(Context context, String serviceName) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = activityManager.getRunningServices(100); // 获取最多100条运行的服务
for (RunningServiceInfo runningServiceInfo : infos) {
String currentService = runningServiceInfo.service.getClassName();
if (serviceName.equals(currentService)) {       //存在当前service
return true;
}
//          System.out.println(runningServiceInfo.service.getClassName());
}
return false;
}


AppInfo类

package com.yu.bean;

import android.R.drawable;
import android.graphics.drawable.Drawable;

public class AppInfo {
/**
* 应用名字
*/
private String appName;
/**
* 应用包名
*/
private String packageName;
/**
* 包内的className
*/
private String className;
/**
* 应用大小
*/
private String appSize;
/**
* 应用图标
*/
private Drawable icon;
/**
* 应用存放位置
*/
private String location;
/**
* 文件目录
*/
private String sourceDir;
/**
* 是否为用户应用 是则为true
*/
private boolean isUserApp;
/**
* 占用内存大小
*/
private int memorySize;
/**
* 是否选中
*/
private boolean isChecked;

public AppInfo(String appName, String packageName, String className,
String appSize, Drawable icon, String location, String sourceDir,
boolean isUserApp) {
super();
this.appName = appName;
this.packageName = packageName;
this.className = className;
this.appSize = appSize;
this.icon = icon;
this.location = location;
this.sourceDir = sourceDir;
this.isUserApp = isUserApp;
}

public AppInfo(String appName, String packageName, Drawable icon,
boolean isUserApp, int memorySize) {
super();
this.appName = appName;
this.packageName = packageName;
this.icon = icon;
this.isUserApp = isUserApp;
this.memorySize = memorySize;
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}

public int getMemorySize() {
return memorySize;
}

public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
}

public String getAppName() {
return appName;
}

public void setAppName(String appName) {
this.appName = appName;
}

public String getPackageName() {
return packageName;
}

public void setPackageName(String packageName) {
this.packageName = packageName;
}

public String getClassName() {
return className;
}

public void setClassName(String className) {
this.className = className;
}

public String getAppSize() {
return appSize;
}

public void setAppSize(String appSize) {
this.appSize = appSize;
}

public Drawable getIcon() {
return icon;
}

public void setIcon(Drawable icon) {
this.icon = icon;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getSourceDir() {
return sourceDir;
}

public void setSourceDir(String sourceDir) {
this.sourceDir = sourceDir;
}

public boolean isUserApp() {
return isUserApp;
}

public void setUserApp(boolean isUserApp) {
this.isUserApp = isUserApp;
}

@Override
public String toString() {
return "AppInfo [appName=" + appName + ", packageName=" + packageName
+ ", className=" + className + ", appSize=" + appSize
+ ", icon=" + icon + ", location=" + location + ", sourceDir="
+ sourceDir + ", isUserApp=" + isUserApp + "]";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: