设为首页 加入收藏

TOP

获取Android各类系统相关信息的接口实现代码(一)
2014-11-24 07:23:47 来源: 作者: 【 】 浏览:5
Tags:获取 Android 各类 系统 相关 信息 接口 实现 代码

/**
* 获取系统中所有安装包信息
*/
public String getAllPackages(Context context)
{
String appList = "";
List packages = context.getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo : packages)
{
// 这里可以控制是否需要获取系统级别的package
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
{
appList += packageInfo.applicationInfo.loadLabel(getPackageManager()).toString() + ",";
}
}

return appList.substring(0, appList.length()-1);
}

/**
* 获取系统内存信息
*/
public String getSysMemoryInfo(Context context)
{


long freeMem = 0;
long totalMem = 0;

ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);

freeMem = memoryInfo.availMem/1024/1024;

//读取指定文件信息
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();
totalMem = Integer.parseInt(content)/1024;



return freeMem+" "+totalMem;
}


/**
* 获取CPU信息
*/
private String getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String cpuInfo=""; //1-cpu型号 //2-cpu频率
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo = arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo =cpuInfo + arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
return cpuInfo;
}

/**
* 获取MAC地址
*/
private String getMacAddress(){
String result = "";
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
// Log.i(TAG, "macAdd:" + result);
return result;
}

/**
* 获取其他手机信息
*/
private String getInfo() {
TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String imei = mTm.getDeviceId();
String imsi = mTm.getSubscriberId();
String mtype = android.os.Build.MODEL; // 手机型号
String numer = mTm.getLine1Number(); // 手机号码

return imei + " " + imsi + " " + mtype + " " + numer;
}

/**
* 获取屏幕的分辨率
*/
private String getWeithAndHeight(){
//这种方式在service中无法使用,
DisplayMetrics dm = new DisplayMetrics();

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 4.2.1短信SMS常用接口整理 下一篇Android入门开发之 Handler使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)